Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateDaoSupport , transaction is not rolled back

I'm playing around with Spring + Hibernate and some "manual" transaction management with PostgreSQL I'd like to try this out and understand how this works before moving to aop based transaction management.

@Repository
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

    @Override
    public void saveUser(User u) {
        Transaction tx = getSession().beginTransaction();
        getHibernateTemplate().saveOrUpdate(u);
        tx.rollback();

    }
}

Calling saveUser here, I'd assume that saving a new User will be rolled back. However, moving to a psql command line, the user is saved in the table.

Why isn't this rolled back, What do I have to configure to do transactions this way ?

Edit; a bit more debugging seems to indicate getHibernateTemplate() uses a different session than what getSession() returns (?)

Changing the code to

Transaction tx = getSession().beginTransaction();
getSession().persist(u);
tx.rollback();

and the transaction does get rolled back. But I still don't get why the hibernateTemplate would use/create a new session..

like image 706
Anonym Avatar asked Oct 14 '22 17:10

Anonym


1 Answers

A couple of possibilities spring to mind (no pun intended):

a) Your JDBC driver defaults to autocommit=true and is somehow ignoring the beginTransaction() and rollback() calls;

b) If you're using Spring 3, I believe that SessionFactory.getSession() returns the Hibernate Session object wrapped by a Spring proxy. The Spring proxy is set up on the Session in part to handle transaction management, and maybe it's possible that it is interfering with your manual transaction calls?

While you can certainly use AOP-scoped proxies for transaction management, why not use the @Transactional(readOnly=false|true) annotation on your service layer methods? In your Spring config file for your service layer methods, all you need to do to make this work is to add

<tx:annotation-driven />

See chapters 10 and 13 of the Spring Reference Documentation on Transaction Management and ORM Data Access, respectively:

http://static.springsource.org/spring/docs/3.0.x/reference/index.html

Finally, if you're using Spring 3, you can eliminate references to the Spring Framework in your code by injecting the Spring-proxied SessionFactory bean into your DAO code - no more need to use HibernateDaoSupport. Just inject the SessionFactory, get the current Session, and use Hibernate according to the Hibernate examples. (You can combine both HibernateDaoSupport and plain SessionFactory-based Hibernate code in the same application, if required.)

like image 135
BobG Avatar answered Oct 18 '22 04:10

BobG