Enlisting in a transaction

Posts   
 
    
Bezbos
User
Posts: 2
Joined: 07-May-2007
# Posted on: 07-Aug-2007 11:11:55   

Hi

I am working as a developer on an application using LlblGen and the .Net 2.0 transaction model.

The application avoids distributed transactions by using a DataAccessAdapter-factory that ensures that only one adapter is created per thread. The problem is now that the factory does not always create a new adapter, but might return an existing adapter - this in turn creates problem with the TransactionScope model, as an adapter is registered to a transaction upon creation of the adapter.

Is there any way to manually register the adapter with a transaction. I know that a TransActionResourceManager class is used to enlist to transactions (upon creation of the adapter) in the DataAccessAdapterBase-class, but regretfully this manager-class is declared Internal and can as a result not be used.

Best regards Esben

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 07-Aug-2007 18:11:57   

I'm not quite sure I follow what you want to do. The adapter returned by your factory might already be an existing one, namely one which is used by the same thread previously, correct? and you now want to enlist that adapter with an existing transactionscope, is that correct?

When an adapter is created, it checks if there's a TransactionScope available and if so, it creates a new resource manager and enlists that one in the scope.

I don't really see the benefit of caching adapter instances. Creating an adapter instance is very fast, as it doesn't do anything fancy when it is instantiated (the first time you instantiate one, it will get the configfile performance hit, but that's only once).

Frans Bouma | Lead developer LLBLGen Pro
Bezbos
User
Posts: 2
Joined: 07-May-2007
# Posted on: 08-Aug-2007 10:24:07   

Adapter instances are not cached due to performance, but to ensure that transactions are not promoted - this is in turn done by using only one adapter per thread. For a further description I refer to the thread:

http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8793

Written by another developer on the team.

To examplify the problem at hand:

//A adapter is created - but not enlisted in a transaction
using(IDataAccessAdapter adapter1 = DPSDataAccessAdapterFactory.GetAdapter())
{
      ...   (read from db)
    
       //A transaction scope is created, but the call to the factory returns the already 
       //created adapter and as a result the adapter is not enlisted in the transaction
      using(TransactionScope scope = new TransactionScope())
      using(IDataAccessAdapter adapter2 = DPSDataAccessAdapterFactory.GetAdapter())
     {
          ... (write to db)
     }
}

The idea would then be to manually enlist the adapter in the transaction upon the second call to the factory.

I hope this clarifies the problem.

Esben

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 08-Aug-2007 20:54:58   

How can a transaction be promoted if you use INSIDE a transaction scope a new adapter? The adapter itself doesn't start a new System.Transactions transaction, it uses an existing one.

The only reason a transaction gets promoted is if there are two or more concurrent connections within the same scope (indirectly or not). This can only happen if you start multiple adapters in the same thread and open multiple connections. I don't see how you would want to do that.

There's another problem: the setup you're suggesting will have a problem cleaning up the transaction objects from the adapter object which stays in scope.

(the link you posted misses the threadid btw wink )

Frans Bouma | Lead developer LLBLGen Pro