I have two databases on the same instance of SQL Server
In 'DatabaseA' I have a stored procedure which calls C# code (CLR assembly in SQL) to unpack data and after unpacking writes the unpacked data onto DatabaseB's tables (ScanTracking, Strings, Numerics)
The stored procedure when executed manually works just fine.
However when I run the stored procedure via trigger on a table present in DatabaseA. I get the following error
'Transaction Context is used by another session'
The full details of the error is as follows:
Msg 6522, Level 16, State 1, Procedure ExecuteDeSerializeBulk, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "ExecuteDeSerializeBulk":
System.Data.SqlClient.SqlException: Transaction context in use by another session.
System.Data.SqlClient.SqlException:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.PropagateTransactionCookie(Byte[] cookie)
at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnecti...
The statement has been terminated.
I did some research and found the following links
Transaction context in use by another session
I am not using a Linked server or anything complicated, both my databases are on the same server.
As a workaround I reverted to SQL Agent job which runs every 5 minutes and unpacks data onto DatabaseB this works fine. However my mind is telling me that i am not finding a solution to the problem with the trigger and working around it using a SQL Agent job.
Any thoughts or suggestions will greatly help.