This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes EJB and other Java EE Technologies and the fly likes Cannot commit and flush hibernate transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Cannot commit and flush hibernate transaction" Watch "Cannot commit and flush hibernate transaction" New topic
Author

Cannot commit and flush hibernate transaction

Henry Resheto
Greenhorn

Joined: Aug 16, 2013
Posts: 2
I am using Hibernate and Spring:
I have a unit test like that:
@Test
@Transactional(propagation=Propagation.REQUIRED)
public void testFoo () throws Exception {
Process process = this.bar(); // This calls private method that returns entity object
baseDao.getCurrentSession().getTransaction().commit();
baseDao.getCurrentSession().flush();
this.bar2(process); // This calls another private method
}
Here is the problem: both methods bar() and bar2() get called and execute fine and do what I want them to do, but when I exit method testFoo I gen an exception from a hibernate: "org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started"
My goal is to save to DB all changes done by method bar() before calling method bar2() what is the proper way to do that?

Henry Resheto
Greenhorn

Joined: Aug 16, 2013
Posts: 2
oh and baseDao is simply some bean that returns me current session
francesco umani
Greenhorn

Joined: Aug 07, 2013
Posts: 27
You should split your test method in more methods marked Transactional without mixing direct and undirect control on transaction (baseDao.getCurrentSession().getTransaction().commit();)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot commit and flush hibernate transaction