java - two transaction issue with Hibernate 3 -
in our application using hibernate 3. faced 1 issue in transactions. have code in following manner
method1() try{ tx = session.begintransaction(); //new object needs stored session.save(object); // code generates exception tx.commit(); // exception occured before commit processing }catch(exception ex){ e.printstacktrace(); throw e; }finally{ if(tx!=null && tx.wasnotcommited()){ tx.rollback(); } }
i have method same this, called after method in case of exception thrown method.
session same across both of method.
now experiencing if 2nd method executes , commits hibernate transaction, stores data has been saved in 1st method don't want.
i can not share exact code, prepared 1 test method similar issue. database server mysql.
package com.opshub.jobs.core; import org.hibernate.session; import org.hibernate.transaction; import com.opshub.dao.core.company; import com.opshub.utils.hibernate.hibernatesessionfactory; public class test { public static void main(string[] args) throws exception{ session session = hibernatesessionfactory.opensession(); try{ transaction tx = session.begintransaction(); try{ company company = new company(); company.setname("gaurav"); company.setcompanydetails("it company"); company.setcompanyaddress("india"); session.save(company); if(true) throw new exception("now go finally"); tx.commit(); }catch(exception e){ throw e; }finally{ tx.rollback(); } }finally{ transaction tx = session.begintransaction(); tx.begin(); company company = new company(); company.setname("gaurav 1"); company.setcompanydetails("it company"); company.setcompanyaddress("india"); session.save(company); // if(true) // throw new exception("now go finally"); tx.commit(); } } }
any appreciated.
finally found solution issue. before starting transaction/operations can use session.clear() removes pending save/update/delete operation session.
so in rollback, add session.save().
thanks responses.
Comments
Post a Comment