-->
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.  [ 6 posts ] 
Author Message
 Post subject: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 8:24 am 
Newbie

Joined: Wed Jun 22, 2011 8:06 am
Posts: 3
Hello,

I have a strange issue and I do not find the right query to google for.

following situation - ORACLE Database - Sequences as ID generator.
Record "OrderHeader" contains a Set of "OrderLines" as childs.
While saving the header object, all lines were updated to the database to, I do not need to save the lines. (thats great!)

Now I have a new header record, containing some lines.
I use "getSession().saveOrUpdate(order);" to save the header record including the lines.
The header could be saved by the database.
But while saving one line, the database runs to an exception (in the current case, a not null field was null)

In my code, I do a rollback after that.
That means, the header object does not exists anylonger in the database.
But since the database needed to insert the header to the table in order to beeing able to insert the lines in the table, I seems that the header object is marked as persistent.

If I run the code "getSession().saveOrUpdate(order);" I get no exception again, because the hibernate believes, the object is persistent. But that is not right, due to the rollback, the object is not persistent.


My Question:
what can be done, in order to mark the object as transient, after the rollback.

Here is some type of code I have:

Code:
   public boolean saveOrder(PurOrderHeader order) {
      
      PurchaseOrderDao orderDao = new PurchaseOrderDao();
      
      try {
         orderDao.setSession(em.getSession());
         em.begin();

         orderDao.saveOrder(order);
         
         em.commit();
      } catch (Exception e) {
         em.rollback();
         log.error("Failed to save purchase order.", e);
         return false;
      }
      return true;
   }

/// and the dao:

   public void saveOrder(PurOrderHeader order) {      
      getSession().saveOrUpdate(order);
   }



After the rollback, my new object has got an Id (generated by the sequence), even if I set this to null back, the object seems to not be saved. My only explaination to that is: The hibernate believes that the header object is persistent and so it believes there is either nothing to be done or it can do an update.

Best Regards
and thank you for your assistance

Alexander


Top
 Profile  
 
 Post subject: Re: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 9:29 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 8:18 am
Posts: 35
when you rollback the transaction, i think the object is detached but not transient.


Top
 Profile  
 
 Post subject: Re: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 9:34 am 
Newbie

Joined: Wed Jun 22, 2011 8:06 am
Posts: 3
After all I could find out:

TRANSIENT: pojo does not exists in database
PERSISTENT: pojo has been added to inserted / updated to database, session is not closed
DETACHED: pojo exists in database

After a rollback, any insert into the database is removed, thus the object does not exists in the database anylonger.
But in my case, the hibernate handles the object as allready existing in the database, but that is not the right way.


Top
 Profile  
 
 Post subject: Re: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 9:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When an exception happens it is no longer "safe" to continue using the session. Entities that have been used in the session may be left in an inconsistent state (eg. the order object has got an id that doesn't exists in the db). You'll need to close and discard the session and possible also entities that was used with it. See http://docs.jboss.org/hibernate/core/3. ... exceptions for more information.

I'm not sure exactly what you want to do after the first exception, but if you want to re-try parts of the failed transaction, you'll at least need a new session and probably also new entity objects.


Top
 Profile  
 
 Post subject: Re: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 10:00 am 
Newbie

Joined: Wed Jun 22, 2011 8:06 am
Posts: 3
After the exception is raised, as you see in the code, I rollback the session.
On the database everything is fine.

In my programm I return the message "Unexpected Exception raised" to the user.
The user sees that there was an error, but the programm still has the record object.

Then the user tries again to save his entries, so the code for saveOrder is called again.
This time, I got no exception, I looks like everything is fine, but the record is not inserted into the database.
This is, because the hibernate believes, that the record is detached/persistent and tries some update.

If the hibernate would assume correctly, that the record is transient, then it would try to insert again and running into the same exception.

I would like to implement something, that my programm can try to handle the situation.


Top
 Profile  
 
 Post subject: Re: instance is handled as persistent after rollback
PostPosted: Wed Jun 22, 2011 10:06 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The safest thing is to throw away the objects that you used in the failed transaction and create new ones for the second try.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.