Reputation: 51
I want to know if there is any issue by creating several transactions on a single session, like this:
using (var session = factory.OpenSession())
{
using (var trans1 = session.BeginTransaction())
{
.....
trans1.commit();
}
using (var trans2 = session.BeginTransaction())
{
.....
trans2.commit();
}
using (var trans3 = session.BeginTransaction())
{
.....
trans3.commit();
}
using (var trans = session.BeginTransaction())
{
.....
// trans1.commit();
}
}
is that possible or must I open a new session object per transaction?
Thanks for your help.
Upvotes: 3
Views: 3408
Reputation: 1
One thind I´d further like to add to this discussion, is that NHibernate cannot handle overlapping Transactions, even if they come from different Sessions (global DB Server and local DB file for example). So for Synchronization you always need to create a real Instance of your Objects and cannot pass them by Query.
Upvotes: 0
Reputation: 49261
No issues although it's unusual. Bear in mind that the session should be discarded if any of the transactions rolls back.
Upvotes: 0
Reputation: 33
It is not usual to have multiple transactions on a single session even in NHibernate. Also from personal experience I can say it is not a good idea to reuse sessions in any other situation.
I recommend to keep the workflow as simple as possible to avoid any side-effects:
Upvotes: 1
Reputation: 1558
The most common strategy in a web environment is to have session per request.
When it cones to transactions, it really depends on your use-case.
What you are doing is fine, or otherwise nhibernate wouldn't separate session from transaction.
but yet again, it depends on your business case situation.
I suggest you to wrap the session.BeginTransaction()
with IDisposable
, so on Dispose
you make sure to commit transaction .
Upvotes: 0
Reputation: 13380
Yes what you are doing is just fine.
What nhibernate does not support are multiple nested transactions.
Upvotes: 2