Joined: Fri Feb 27, 2009 7:14 pm Posts: 1
|
Need help with Hibernate? Read this first:
We have DAO class which does multiple inser/update in single transaction.If one of the insert/update fails it doesn't rollback the earlier succesful insert.
Suppose object 1 is inserted successfully and object 2 gets stale object exception the tx.rollback doesn't object 1 from database .Changes gets commited even though auto commit mode is false.
Please HELP!!!
public void saveUpdate(List list, Session session) throws Exception
FlushMode flushMode = session.getFlushMode();
try {
session.connection().setAutoCommit(false);
logger.info(" auto commit mode : " + session.connection().getAutoCommit());
} catch (HibernateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Transaction tx = null;
try {
session.setFlushMode(FlushMode.COMMIT);
tx = session.beginTransaction();
for(Object obj: list){
session.saveOrUpdate(obj);
}
session.flush();
tx.commit();
tx = null;
} catch (RuntimeException e) {
if(tx.isActive()){
tx.rollback();
session.close();
session = HibernateUtil.getNewSession();
}
throw e;
} finally{
if(session != null){
try {
session.connection().setAutoCommit(true);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setFlushMode(flushMode);
}
}
}
|
|