Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - Do I really need to rollback failed read-only transaction?

I've just started learning Hibernate and I use the following pattern (from documentation) for every transaction:

private Session session;
private Transaction transaction;

protected List selectAll(Class clazz) throws HibernateException {
    List objects = null;
    try {
        session = MyHibernateHelper.getSessionFactory().openSession();
        transaction = session.beginTransaction();

        // SELECT ALL
        objects = session.createCriteria(clazz).list();

        transaction.commit();
    } catch (HibernateException exc) {
        if (transaction != null) transaction.rollback();
        throw exc;
    } finally {
        session.close();
    }
    return objects;
}

I can accept that every operation should be wrapped in a transaction. But it seems to me strange and unnecessary to rollback select, in case it fails.
I think I can safely remove catch block from the above example. And from any read-only operation. Am I right?

like image 538
naXa Avatar asked Dec 10 '14 14:12

naXa


People also ask

What happens if transaction rollback fails?

If a rollback fails, then you would have a serious problem. The reliability of the database cannot be guaranteed. In other words; you probably have some sort of corruption in your transaction log and will end up with an inconsistent database.

Do we need @transactional for read operation?

If you mark method as @Transactional(readonly=true) , you'll dictate whether it's actually possible to write into DB in scope of this transaction. If your architecture is cumbersome and some team members may choose to put modification query where it's not expected, this flag will point you to the problematic place.

What is the use of rollback in hibernate?

rollback() , Hibernate rolls-back the database transaction. Database handles rollback, thus removing newly created object.

What is a best practice to mark transaction as read only when code doesn't write anything to the database?

Select a uniqueanswer. Correct Answer:The current method must start a new transaction and run within its owntransaction.


1 Answers

Consider Hibernate has to work with all kinds of databases for all kinds of situations. What they're giving you in their documentation is how to use Hibernate from any supported database. And they try to support a lot of databases. Some of these databases might handle cleaning up failed transactions without needing an explicit rollback, but Hibernate wants to make sure it covers everybody.

Also consider having a read-only transaction fail is going to be extremely rare in real life. Once it does happen there isn't going to be much for the rollback to do, but what it does do -- releasing locks as Marc B points out -- is not something you want to skip. So what you're advocating is a very tiny optimization (based on how infrequently that path gets exercised) which could have bad consequences in some circumstances. Having a lock not get released is the kind of bad consequence that doesn't manifest until some other transaction fails or hangs, making it very hard to track down the cause. The lock not getting released could cause deadlocks so the performance/availability impact could be dire. Also this would be a pain to test for, if there is a problem it would be likely to be missed until there's a crisis in a production environment. On multiple counts it's the kind of thing you want to avoid.

The biggest problem here is that there's a lot of boilerplate code to include for every transaction. My suggestion is to either implement some utility methods using callbacks or the Command pattern (see Bauer and King's book Java Persistence with Hibernate for examples), or adopt a framework. There is an argument against frameworks that they obscure implementation details and make learning harder, but you've already had the learning experience of seeing how these pieces are put together. There are mature frameworks like Seam or Spring that will handle this for you.

like image 62
Nathan Hughes Avatar answered Oct 08 '22 14:10

Nathan Hughes