-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Transaction Rollback and persisted object.
PostPosted: Wed Jul 09, 2008 4:04 am 
Newbie

Joined: Wed Jul 09, 2008 3:42 am
Posts: 2
I would like to ask about transaction Rollbacking in NHibernate. I try to insert an object into some table. Insert is done in transaction. After the insert which is OK, I call Rollback. The transaction went ok, the object is no longer in database, however the object is still persisted in the NHibernate session. Is there a way to synchronize the object state in NHibernate session and database?

In the following example the Update() does not work, and Refresh throws an axception that object with this id doesn't exist in the database.

I would be greatfull for any hint.

Hibernate version: 1.2.1 GA

Code between sessionFactory.openSession() and session.close():
Code:
//There are two tables - Person and Company, Company have a list of Person. We clear all the tables
           ClearPersonAndCompanyTables();

            ICompanyDao companyDao = testPlatform.daoFactory.GetCompanyDao();
            IApplicationContext ctx = ContextRegistry.GetContext();
            INHibernateSessionManager sessionManager = (INHibernateSessionManager)ctx.GetObject("SpringNHibernateSessionManager");

            //Begin a transaction, insert company, and then do a rollback
            sessionManager.BeginTransaction();
            logger.Debug("Company.ID before save: " + testPlatform.Company2.Id);
            testPlatform.Company2 = companyDao.Save(testPlatform.Company2);
            logger.Debug("Company.ID before rollback: " + testPlatform.Company2.Id);

            sessionManager.RollbackTransaction();
            logger.Debug("Company.ID after rollback: " + testPlatform.Company2.Id);
            //sessionManager.GetSession().Flush();
            //sessionManager.GetSession().Update(testPlatform.Company2);
            //sessionManager.GetSession().Refresh(testPlatform.Company2);


Name and version of the database you are using:
SQL Server 2005 Express


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 9:48 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I think the issue could be that you are trying to continue to use the Session once you have performed a rollback and/or commit.

Typically I believe the best-practices for NHibernate (and other persistence frameworks) is to...
    start a session
    read in/register persistent objects within the session
    -- do work (which may or may not mess with the persistent objects)--
    either rollback or commit the session
    dispose of the session


I would suggest you download the help file for NHibernate and read the
Transactions and Concurrency section to get a better understanding of how NHibernate manages Transactions and session state.

This example comes from the documentation
Code:
// foo is an instance loaded by a previous Session
foo.Property = "bar";
session = factory.OpenSession();
transaction = session.BeginTransaction();
session.SaveOrUpdate(foo);
session.Flush();
transaction.Commit();
session.Close();


In my applications I use a lightweight disposable wrapper around my calls to the NHibernate framework. On dispose, my wrapper performs a rollback (if Commit was not called) and the session is closed.

This this lightweight wrapper will throw an exception if I attempt to continue to use a session after I've either commited or rolledback.

My code would then look like:
Code:
using (IDataAccessSession session = DataAccessSessionFactory.Instance.Begin(Databases.MyDb))
{
      Counterparty counterparty = GetCounterparty(session, toUpdate.Name);
      counterparty.Name = "-updated";
      session.Update(counterparty);
      session.Complete();
}


Hope this helps

-Greg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 5:42 am 
Newbie

Joined: Wed Jul 09, 2008 3:42 am
Posts: 2
Thank to for your answer.

Yes, I understand that I should dispose this session. However I have objects that are depending on that session (need it for lazy-loading).

I found a solution and call the Evict(object) method to exclude the object from session persisted object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.