Why is HIbernateTransactionManager required in Spring? Why is HIbernateTransactionManager required in Spring? spring spring

Why is HIbernateTransactionManager required in Spring?


What Spring allows to do, thanks to AOP, is to use declarative transactions, like you could do with EJBs.

Instead of doing

public void doSomething() {    Session sess = factory.openSession();    Transaction tx = null;    try {        tx = sess.beginTransaction();        // do some work        ...        tx.commit();    }    catch (RuntimeException e) {        if (tx != null) tx.rollback();        throw e; // or display error message    }    finally {        sess.close();    }}

You simply do

@Transactionalpublic void doSomething() {    // do some work}

Which is much more readable, more maintainable, less cumbersome, and safer since Spring handles the transactional logic for you. That's why a transaction manager is needed: to tell Spring how it should handle the transactions for you. Because it can also use the same declarative model but use JPA transactions, or JTA transactions.

This is well described in the Spring documentation.