Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will Hibernate also rollback its data objects?

 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In hibernate, if we do some value changes on the hibernate data objects and trying to update them into database by calling session.update method. If at this point of time, the database exception occurs, we will call Transaction.rollback. This, I believe, will roll back the database to the original state, but will it also roll back the data objects to the old state? If it doesn�t, it means the data objects and database are in an inconsistent state, right?

Suppose these hibernate data objects are first loaded into the memory using hibernate when the system starts up, and remianed in the memory until the system is shut down or exceptionally crashed.

Thanks.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick test.
try printing this object in the exception clause.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well most likely your code would catch the exception, in the catch have the transaction rolled back, then in the finally clause you would close the session, thereby making the reference to the object detached, but the object state you ahve should still contain the changes that was made on the GUI, or code, however you changed it. At this point your application should decide whether it needs to keep that state or reload the object in a new session.

Mark
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I will store all the hibernate data objects in my own cache when the application starts up, which means they are no longer transient ones. So it seems it's not a good practice now to cache the hibernate objects ourselves, doesn't it?

One more thing, I don't think when the database exceptions occur, reload the data objects is a good approach, because if there's a database exception, most likely it is a connection problem or the database went dark. (For other exceptions like constraint exceptions, normally we shouldn't let it happen, right? We should check all the time to avoid such exceptions, which means the rest of database exceptions are deadly.)

So if that deadly database exception happens, reload also won't help, right? So I need to either shut down my application or rollback the data objects myself, right? If the shut down of the application is not possible, so the only choice I got is rollback the data objects myself. But it's quite a disaster for me. I need to implement a rollback mechanism.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
At this point your application should decide whether it needs to keep that state or reload the object in a new session.

Mark


Exactly. May be that might be one of the usages of the refresh(Object obj) method in hibernate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic