Transaction is commiting even with rollback

103 views
Skip to first unread message

graffitiMSX

unread,
Oct 23, 2012, 7:03:58 AM10/23/12
to fluent-n...@googlegroups.com
Hi folks, I am doing several operations inside a Transaction Scope, when a failure occurs then it is redirected to the Rollback but the pre-failure saves are committed to db.

My code is essentialy this:

if (!_session.IsOpen) _session.SessionFactory.OpenSession();
using (_session.BeginTransaction())
{
try
{
Remittance remittance = new Remittance();
...
_remittanceDao.SaveOrUpdate(remittance);
TripSent tripSent = new TripSent();
try
{
TripSent tripSent = CreatTripSent();
_tripSentDao.Save(tripSent);
}
catch (Exception ex)
{
throw new Exception(String.Format("Erro criando TripSent com índice {0}", i), ex);
}
Entity.TransactionFile eTransactionFile = new Entity.TransactionFile();
...
_transactionFileDao.SaveOrUpdate(eTransactionFile);
_session.Transaction.Commit();
processOk = true;
}
catch
{
_session.Transaction.Rollback();
throw;
}
}

The error is occuring in the line _tripSentDao.Save(tripSent); and, even after the Rollback I check the DB and the _remittanceDao.SaveOrUpdate(remittance); is there.

Any help?

Rudolf

Oskar Berggren

unread,
Oct 23, 2012, 7:14:49 AM10/23/12
to fluent-n...@googlegroups.com
I don't see any TransactionScope. Where are you disposing the session? Be aware that if you continue to use the session instead of disposing or clearing it (or open a new transaction) immediately after rollback, you run the risk of the session performing SQL INSERT/UPDATE outside of the transaction. A session should be discarded "quickly" when an error occurs.

Note that you don't need to rollback explicitly as long as you ensure the transaction is disposed. It will then rollback unless it has been expressly committed.

/Oskar



2012/10/23 graffitiMSX <rudolf...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/4ONRBEksw98J.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

graffitiMSX

unread,
Oct 23, 2012, 7:45:30 AM10/23/12
to fluent-n...@googlegroups.com
Sorry, I wrote "Transaction Scope" when I meant to say only "The Scope of one Transaction".

I am getting the session that is already open, and not disposing it. I will rewrite the code and try to use correctly the TransactionScope, and also dispose the session right after the Transaction.

Thanks

Rudolf

Oskar Berggren

unread,
Oct 23, 2012, 7:48:22 AM10/23/12
to fluent-n...@googlegroups.com
Just to be clear, I did NOT imply that have to use TransactionScope. Specifically, if you do use it, for best results you should still use the NHibernate transaction also.

/Oskar



2012/10/23 graffitiMSX <rudolf...@gmail.com>
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/jyOcjU-FBN8J.

gleb Chermennov

unread,
Oct 23, 2012, 9:23:19 AM10/23/12
to fluent-n...@googlegroups.com
I thought when doing session.Save/session.SaveOrUpdate, transaction management is handled by NHibernate (e.g. it will commit if no exceptions and rollback otherwise). Am I wrong?

вторник, 23 октября 2012 г., 15:48:45 UTC+4 пользователь Oskar Berggren написал:

Oskar Berggren

unread,
Oct 23, 2012, 10:11:02 AM10/23/12
to fluent-n...@googlegroups.com
NHibernate's Save/SaveOrUpdate() has nothing do to with transaction handling. IF those methods result in immediate SQL and there is no surrounding transaction, the database server itself will use automatic transactions for each batch - which may result in a partially updated database.

This is different compared to SaveChanges() from Entity Framework, which will create a TransactionScope internally - this is possibly only because Entity Framework has no automatic flushing of dirty state to the database.

/Oskar



2012/10/23 gleb Chermennov <thebitt...@gmail.com>
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/V5KKDY_5D8wJ.
Reply all
Reply to author
Forward
0 new messages