SQL SERVER. Автономные транзакции. Autonomous transaction.
При реализации процесса логирования произведенных операций возникает вопрос о необходимости сам процесс записи в лог проводить в автономной транзакции. В СУБД Oracle есть такой механизм вида:
pragma autonomous_transaction;
СУБД MS SQL Server не поддерживает автономные транзакции, но есть 3 пути решения заданной задачи:
1. Вызов SQLCLR - процедуры в транзакции.
2. Использование табличной переменной ( table variable ).
3. Вызов хранимой процедуры через Linked Server к этому же серверу со значением "True" параметров RPC и RPC Out.
2. Использование табличной переменной ( table variable ).
3. Вызов хранимой процедуры через Linked Server к этому же серверу со значением "True" параметров RPC и RPC Out.
Очень хорошо про способы осуществить автономную транзакцию описаны в статье http://blogs.msdn.com/b/sqlprogrammability/archive/2008/08/22/how-to-create-an-autonomous-transaction-in-sql-server-2008.aspx.
Только здесь не раскрыт вариант автономной транзакции с помощью SQLCLR. Его и постараюсь здесь привести в пример.
Подготовка сервера для выполнения CLR - процедур:
SP__CONFIGURE 'SHOW ADVANCED OPTIONS', 1;
GO
RECONFIGURE;
GO
SP__CONFIGURE 'CLR ENABLED', 0;
GO
RECONFIGURE;
GO
В среде Visual Studio C# создаем новый проект типа Библиотека классов (ClassLibrary):
SP__CONFIGURE 'SHOW ADVANCED OPTIONS', 1;
GO
RECONFIGURE;
GO
SP__CONFIGURE 'CLR ENABLED', 0;
GO
RECONFIGURE;
GO
В среде Visual Studio C# создаем новый проект типа Библиотека классов (ClassLibrary):
If you are looking for alternative ways of creating autonomous transaction on SQL 2008 or 2005, you have these options:
- Loopback connection from SQLCLR procedure to start a new transaction. Compared to more rigid structure need of loopback linked server, SQLCLR is more flexible and gives you more control over how you want to handle interaction with database. If the logic of the autonomous transaction includes computational tasks, it's one of SQLCLR's strengths to provide performance gain as extra benefit.
- Using table variable to save data within transaction. Table variables are not affected by transaction rollback thus serve as temporary buffer for transaction data. Once transaction is done, you can dump data out of table variable to a permanent table. Table variables have limited scope and are less flexible. Usually they would also be slower due to lack of index/statistics. However, it does offer you a pure TSQL option with no need to create anything new.
- Loopback connection from Extended Stored Procedures.
Комментарии
Отправить комментарий