Search code examples
javahibernatehibernate-5.x

Transaction closing syntax compatible with hibernate 4 and 5


I have a transaction closing syntax working for a long time in a webapp. It's running hibernate 4.3.10.Final:

if(t!=null && !t.wasCommitted()) t.rollback();
if(s!=null) s.close();
// t is a org.hibernate.Transaction, s is a org.hibernate.Session

Now this webapp will run in and updated environment, with hibernate 5.1.10.Final. This current code gives me an java.lang.NoSuchMethodError: org.hibernate.Transaction.wasCommitted()Z exception.

Is it possible to update this code and make it runnable in both hibernate versions? If not, how should I change it so it works with hibernate 5?


Solution

  • It is possible to check your hibernate version using org.hibernate.Version.getVersionString(), but it's far from trivial (and not recommended) to have 2 different versions of hibernate libs in your project.

    I'm branching a new version of the webapp and updating hibernate in this version.