I have read a few posts on this topic: e.g., here and here
In my case, I am certain that all transactions are committed and that all exceptions are handled with a rollback.
Currently, I am building the model layer for a web app (Spring/Hibernate/Postgres). I am using junit to rigorously test as I build. For some reason, on one delete test I repeatedly got the "nested transaction not supported" exception.
Session session=HibernateDAO.getSessionFactory().getCurrentSession();
try
{
session.getTransaction().begin();
List<TestUnitType> result = session.createQuery("from TestUnitType").list();
TestUnitType itemToDelete = result.get(0);
session.delete(itemToDelete);
session.getTransaction().commit();
} catch (Exception e)
{
TestSS.getLogger().error(e.toString(), e);
session.getTransaction().rollback();
fail(e.toString());
}
The exception is thrown when calling the transaction prior to the query. The previous method definitely closed the transaction as it is couched in the same syntax as this method (and I carefully debugged).
I resolved the issue by creating a kludge method and using it to get the session:
public static Session resetCurrentSession()
{
Session oldsession = getSessionFactory().getCurrentSession();
if(oldsession.isOpen())
{
oldsession.close();
}
return getSessionFactory().getCurrentSession();
}
All unit tests now run and pass, the data in the database is clean and I see no side effects of my kludge. Nonetheless, I am uncomfortable using a workaround in unit tests. No fix is safe if the programmer does not know the cause of the problem behind it.
can someone advise on the possible cause of the exception?