Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate transaction not successfully started

Tags:

java

hibernate

Consider this simple Hibernate scenario:

session = getHibernateSession(); tx = session.beginTransaction(); SomeObject o = (SomeObject) session.get(SomeObject.class, objectId); tx.commit(); 

This code produces the following exception:

org.hibernate.TransactionException: Transaction not successfully started     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)     at com.bigco.package.Clazz.getSomeData(Clazz.java:1234) 

What's going on?

like image 995
Yuval Adam Avatar asked Mar 01 '11 13:03

Yuval Adam


1 Answers

Well, it looks like once we reach the tx.commit() line, the transaction has already been committed. My only guess is that Hibernate already commits the transaction when get()ing the object.

The fix for this is simple:

// commit only if tx still hasn't been committed yet (by hibernate) if (!tx.wasCommitted())     tx.commit(); 
like image 90
Yuval Adam Avatar answered Oct 02 '22 15:10

Yuval Adam