views:

30

answers:

1

I'm looking to add transactional support to my DB engine and providing to Abstract Transaction Handling down to passing in Guids with the DB Action Command. The DB engine would run similar to:

private static Database DB;
public static Dictionary<Guid,DBTransaction> Transactions = new ...()
public static void DoDBAction(string cmdstring,List<Parameter> parameters,Guid TransactionGuid)
{
  DBCommand cmd = BuildCommand(cmdstring,parameters);
  if(Transactions.ContainsKey(TransactionGuid))
    cmd.Transaction = Transactions[TransactionGuid];
  DB.ExecuteScalar(cmd);
}
public static BuildCommand(string cmd, List<Parameter> parameters)
{
   // Create DB command from EntLib Database and assign parameters
}
public static Guid BeginTransaction()
{
   // creates new Transaction adding it to "Transactions" and opens a new connection
}
public static Guid Commit(Guid g)
{
   // Commits Transaction and removes it from "Transactions" and closes connection 
}
public static Guid Rollback(Guid g)
{
   // Rolls back Transaction and removes it from "Transactions" and closes connection
}

The Calling system would run similar to:

Guid g
try
{
  g = DBEngine.BeginTransaction()
  DBEngine.DoDBAction(cmdstring1, parameters,g)
  // do some other stuff
  DBEngine.DoDBAction(cmdstring2, parameters2,g)
  // sit here and wait for a response from other item
  DBEngine.DoDBAction(cmdstring3, parameters3,g)
  DBEngine.Commit(g)
}
catch(Exception){ DBEngine.Rollback(g);}

Does this interfere with .NET connection pooling (other than a connection be accidently left open)?

Will EntLib keep the connection open until the commit or rollback?

A: 

The connection will be kept open until a commit or rollback. It is the transaction that is keeping the connection open.

It will not affect connection pooling, other than a connection held by a transaction will not be returned to the connection pool.

I would recommend that you look at the .net TransactionScope. This may be able to meet your needs, without you writing any of this custom code.

Shiraz Bhaiji
Thanks for your answer. TransactionScope wont work because there are needs to expose the Begin and Commit functions to Clients that also connect via web services (allowing for transaction support over various non sequential stateless calls).
Marty Trenouth