Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot commit and flush hibernate transaction

 
Henry Resheto
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh and baseDao is simply some bean that returns me current session
 
francesco umani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should split your test method in more methods marked Transactional without mixing direct and undirect control on transaction (baseDao.getCurrentSession().getTransaction().commit();)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic