简体   繁体   English

事务不会在休眠中回滚

[英]Transaction does not rollback in hibernate

I have a problem with rollback transaction. 我对回滚事务有问题。 Below I wrote some of the configuration of beans. 下面我写了一些bean的配置。 I do 2 SQL-queries: delete and update. 我执行2条SQL查询:删除和更新。 And when UPDATE generates exception (constraint of foreign ket), the first query (DELETE) does not rollback. 并且当UPDATE生成异常(外部ket的约束)时,第一个查询(DELETE)不会回滚。 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: 我有带方法removeUser的DAO层:

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: 该服务是通过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. 我认为并不是所有的数据库服务器都支持事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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