简体   繁体   中英

Transaction does not rollback in hibernate

I have a problem with rollback transaction. Below I wrote some of the configuration of beans. I do 2 SQL-queries: delete and update. And when UPDATE generates exception (constraint of foreign ket), the first query (DELETE) does not rollback. Could anyone please tell me where the problem is? I wrote only some of the configuration for sake of clarity, so if it's needed more information please let me know. Thanks in adnvance!

CONTEXT:

I have DAO layer with method removeUser:

public void removeUser(final Long id) {
        getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                executeUpdate("delete from table1 where user_id = ?", session, id);
                executeUpdate("update table2 set user_id = null where user_id = ?", session, id);
                return null;
            }

            private void executeUpdate(String queryString, Session session, Long... params) {
                SQLQuery query = session.createSQLQuery(queryString);

                for (int paramIndex = 0; paramIndex < params.length; paramIndex++) {
                    Long param = params[paramIndex];
                    query.setParameter(paramIndex, param);
                }

                query.executeUpdate();
            }
        });
}

This method is called from within service:

public void removeUser(Long id) {
        userDao.removeUser(id);
}

This service is configured via spring:

<bean name="adminUserService" parent="txProxyServiceTemplate">
   ... setting properties ...     
</bean>

<bean id="txProxyServiceTemplate" abstract="true"
          class="com.xalmiento.desknet.ui.server.service.transaction.GWTTransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <props>
                <prop key="remove*">PROPAGATION_NESTED</prop>
            </props>
        </property>
</bean>

<bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="nestedTransactionAllowed" value="true"/>
</bean>

Try with

<prop key="remove*">PROPAGATION_REQUIRED</prop>

I don't think all the database server does support the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM