JayPea
JayPea

Reputation: 9711

Hibernate pattern for transaction reuse

I have a base method that I'm writing in order to not repeat the same hibernate session/transaction logic over and over. It's fairly simple, but there's a specific issue that I'm not sure can be solved with this approach.

Imagine that you have a User entity and a Permission entity. If a request is made to save a user along with its matching permissions, then I think that it would make sense to perform both operations in a single transaction, since being able to save only one of those entities could be considered data corruption. For example, failing to save the user's permissions would warrant a rollback on previously inserted user data.

I made the following method to allow generic hibernate operations that could work with the current transaction if it were necessary, although I now think that in its current form it won't work since calling session.beginTransaction(); will probably return a new transaction even if the previous hasn't been commited (is this the case?). Suppose that I changed it in order to have it return the current session and transaction if it was specified that there would be more operations for the current transaction, do you think it would work? Would it be advisable to do something like this, or would you recommend a change of approach? Thanks

protected <T> void baseOperation(Class<T> entityClass, List<T> instances, BaseHibernateDAO.Operations operation, boolean isLastOperation) throws Exception
{
    Session session = null;
    Transaction transaction = null;
    boolean caughtException = false;

    //get session from factory
    session = HibernateSessionFactory.getSession();

    try
    {
        //get current transaction
        transaction = session.beginTransaction();

        for (Object instance : instances) //perform operation on all instances
        {
            log.debug(String.format("Will perform %s operation on %s instance.", operation.name(), entityClass.getName()));

            switch (operation) //perform requested operation
            {
                case SAVE:
                    session.save(instance);
                    break;
                case UPDATE:
                    session.update(instance);
                    break;
                case SAVEORUPDATE:
                    session.saveOrUpdate(instance);
                    break;
                case DELETE:
                    session.saveOrUpdate(instance);
                    break;
            }

            log.debug(String.format("%s operation on %s instance was succesful.", operation.name(), entityClass.getName()));
        }

        session.flush(); //synchronize

        if (isLastOperation) //if this is the last operation of the transaction
        {
            transaction.commit();
            log.debug("Transaction commited succesfully.");
        }
    }
    catch (Exception e) //error occurred
    {
        caughtException = true;

        //roll-back if transaction exists
        if (transaction != null)
        {
            transaction.rollback();
        }

        //log and re-throw
        log.error("An error occurred during transaction operation.", e);
        throw e;
    }
    finally //cleanup tasks
    {
        if (isLastOperation || caughtException) //close session if there are no more pending operations or if an error occurred
        {
            HibernateSessionFactory.closeSession();
        }
    }
}

Upvotes: 1

Views: 3743

Answers (1)

Ryan Stewart
Ryan Stewart

Reputation: 128949

"Advisable" would be to stop trying to rewrite code that's already been written, debugged, dragged through the mud, debugged more, and deployed thousands of times. I.e, the issues and considerations you're encountering have been encountered and overcome before, and the solutions are proven. Further, having been extensively used and improved, they require much less effort to use than what you're putting into your custom solution. Check out Spring's Hibernate support, especially "Implementing DAOs based on plain Hibernate 3 API" and "Declarative transaction demarcation". For further reading, there's a whole chapter on transaction management.

I have a sample project on github where you can see a very simple example of using Spring to manage Hibernate Sessions and transactions in the context of a webapp (using Spring MVC).

Update: For those who come along later, so they don't have to dig through the comments:

There are three general ways to use Spring's transaction handling: declaratively defining which methods are transactional with XML, declaratively annotating methods as @Transactional, or programmatically using TransactionTemplate.

Upvotes: 7

Related Questions