I have a Oracle database and I'm using the Oracle.ManagedDataAccess
.
In some cases I will need to do actions in a single transactions, but often not.
I'm not sure what the best way to handle DbConnection
objects within a single TransactionScope
.
I could inject a DbConnection
into the repositories and then even use LifetimePerScope
to ensure they all get the same DbConnection
instance. But is that a smart move, is it ok to .Open()
the connection once.
using (var scope = _lifetimeScope.BeginLifetimeScope())
{
var connection = scope.Resolve<IDbConnection>();
var personRepo = scope.Resolve<IPersonRepository>();
var workRepo = scope.Resolve<IWorkRepository>();
connection.Open();
var transaction = connection.BeginTransaction()
personRepo.DeleteById(someId);
workRepo.DeleteByPersonId(someId);
transaction.Commit();
}
This would force me to always use a LifetimeScope
, even if not using a Transaction, and open the connection outside the repository method.
Are TransactionScopes dependent on a single connection or can I open multiple connections (how does the connectionPool handle that while a transaction is open?) within the same transaction?
I'm a total outsider to DbConnections and all that so I might be totally misunderstanding the best way to use TransactionScope and DbConnections.