HibernateDaoSupport, transaction not rollback

I play with Spring + Hibernate and some β€œmanual” transaction management with PostgreSQL. I would like to try this and understand how it works before moving on 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 the saveUser function here, I assume that saving the new user will be canceled. However, by going to the psql command line, the user will be saved in the table.

Why is this not a rollback? What do I need to configure for transactions this way?

Change a bit more debugging seems to indicate that getHibernateTemplate () is using a different session than getSession () returns (?)

Change code to

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

and the transaction is indeed returning. But I still don't understand why hibernateTemplate will use / create a new session.

+5
2

spring ( ):

a) JDBC autocommit = true - beginTransaction() rollback();

b) spring 3, , SessionFactory.getSession() Hibernate, - spring. - spring , , , , , ?

, , - AOP , @Transactional (readOnly = false | true) ? spring , , , -

<tx:annotation-driven />

. 10 13 spring ORM :

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

, spring 3, spring Framework , Spring -proxied SessionFactory bean DAO - HibernateDaoSupport. SessionFactory, Hibernate Hibernate. ( HibernateDaoSupport SessionFactory , .)

+2

JavaDoc HibernateDaoSupport.getSession(), , , . , HibernateDaoSupport.

, getHibernateTemplate(). getSession() getSession(), , HibernateTemplate, .

, , .

EDIT:

, ... . , , , ​​-. HibernateDaoSupport , , . .

+2

All Articles