Understanding Hibernate Transaction rollbacks.
Hibernate does not have object level rollbacks. This means that the rollbacks and commits are always at the sql level.
method{
do{t = session.transaction;
try{
t.begintransaction
----code----
VO p = new VO();
session.save(p); // db insert not yet called
---code---
t.comit;
}catch{
t.rollback;
}
}
}//method end
Assume that an ID is generated by a custom class that i have built which basically reads a database table for id and updates the same at the same time.
Lets say there is an exception before the commit is called, then the above does not work as the t.rollback does not rollback the saved object in memory. There is no way to rollback that saved object in memory. I repeat roll back is always SQL level roll back, meaning that any sql fired between a transaction will be rolled back.
Moreover the next call to session.flush() still inserts the Object as the tx.rollback is just a sql level rollback and not object level.
To correct that i did this.
method{
do{t = session.transaction;
try{
t.begintransaction
----code----
VO p = new VO();
session.save(p); // db insert not yet called
session.flush(); // db insert forced, Hence sql query fired.
---code---
t.comit;
}catch{
t.rollback;
}
}
}//method end
Here the object was not inserted when session.flush was called next time at some other place in the code. The above code did the real rollback as needed.
Conclusion from Hibernate team in the forum.
The Hibernate Session is a unit of work. After you have committed or rolled back, you should close the session and get a new one for the next unit of work.
Sherman
In short there must be a new session (session.close()), after every commit or rollback and the old session must be discarded.
Please refer here :
http://forum.hibernate.org/viewtopic.php?t=932970
0 Comments:
Post a Comment
<< Home