Sharing a transaction and a connection between multiple objects  
Author Message
Muhammad Adel





PostPosted: Architecture General, Sharing a transaction and a connection between multiple objects Top

I have two objects which do some operations on the database. these operations should be done in one transaction (all or nothing), so they should share the same transaction object and the same connection object too.
I was wondering what is the best design pattern to use in this situation. any thoughts



Architecture3  
 
 
ALFKI





PostPosted: Architecture General, Sharing a transaction and a connection between multiple objects Top

You want Unit of Work:

http://www.martinfowler.com/eaaCatalog/unitOfWork.html



 
 
Arnon Rotem Gal Oz





PostPosted: Architecture General, Sharing a transaction and a connection between multiple objects Top

Are these objects running in the same process on the same thread how are the connected in time (is one invoked by the other are both invoked by a third are there other objects that shouldn't be part of the transaction ) etc.

Arnon



 
 
hspc





PostPosted: Architecture General, Sharing a transaction and a connection between multiple objects Top

fAssuming you are not going to use COM+ or Enterprise services..
You can use the singletone pattern..Each class gets the connection by calling a factory function that returns the connection which can have a transaction started..
The connection can be stored as a static member in case the application is not multithreaded by nature.. for example..If your components are called from an ASP.NET webservice, storing the connection as a static member will make it shared between all clients .. And in this case all objects from different threads (web service request) will be enrolled in the same trasnaction.
In this case you can store the connection in the HTTPContext.