Transaction disposed without explicit rollback / commit

Hibernate Profiler has detected a transaction that was disposed without calling either commit() or rollback().

This is a common bug, forgetting to call the transaction commit() method before closing the session, which results in an implicit rollback. For example:

session.beginTransaction();
try {
    Post post = session.get(Post.class, 5);
    post.addComment(comment);
}
finally {
    session.close();// bug, implicit rollback goes here
}

Instead of the code above, we should have written:

session.beginTransaction();
try {
    var post = session.get(Post.class, 5);
    post.addComment(comment);
    session.getTransaction().commit();
}
finally {
    session.close();
}

This piece of code will work and save the new comment. If you want to rollback the transaction, it is recommended that you would do so using an explicit Rollback, since that would be much easier to understand when reading the code late.

Last update: 10/25/2009 3:55:15 PM