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.