简体   繁体   English

@transaction-回滚失败

[英]@transaction - roll back failing

@Override
    @Transactional(rollbackFor = { RuntimeException.class, Exception.class}, propagation = Propagation.REQUIRED)
    public String upload(ObjectVO vo) throws CustomException {
    .......
    }

In this service, I am inserting in to two tables. 在此服务中,我要插入两个表。 If there is an Exception while processing data (like mandatory field check) for the second table which supposed to get inserted after the first table, should it roll back the data inserted in the first table in the same transaction? 如果在处理应该在第一个表之后插入的第二个表的数据(例如强制字段检查)时发生异常,是否应该回滚在同一事务中插入到第一个表中的数据? In my case I am not getting it rolledback. 就我而言,我不会回滚它。 What is the expected behaviour? 预期的行为是什么? (And yes, I am not catching the exception, its a custom exception which is included in rollbackFor clause, and is being thrown) (是的,我没有捕捉到异常,它是一个自定义异常,它包含在rollbackFor子句中并被抛出)
( I am using hibernate ) The DAO Layer does getSession().save(entity); I am using hibernate )DAO层执行getSession().save(entity); ( getSession() returns currentSession) getSession()返回currentSession)

(So the data in the first table remains) (因此,第一个表中的数据仍然保留)

The tables are not related. 这些表无关。

<tx:annotation-driven/>

<bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        lazy-init="true">
        <property name="dataSource" ref="dataSource" />
    </bean>

Are you sure the proxy actually apply on the method invocation ? 您确定代理实际上适用于方法调用吗?

Assuming you use classic dynamic proxies (not aspectJ ones) can you ensure that the method is called from outside of its class ? 假设您使用经典的动态代理(而不是AspectJ的代理),可以确保从该类的外部调用该方法吗?

[ edit ] If you're using hibernate switch your transaction manager to HibernateTransactionManager [ 编辑 ]如果您使用的是休眠模式,请将您的事务管理器切换到HibernateTransactionManager

First : As suggested , first enable the log by adding the following in your log4j 首先:按照建议,首先通过在log4j中添加以下内容来启用日志

log4j.category.org.springframework=ALL

Second : if you are calling upload method from another method in the same Servcie class ( say xxxMethod) and the xxxMethod is not annotated by Transaction. 第二:如果您要从同一Servcie类中的另一个方法(例如xxxMethod)调用上载方法,并且XXXMethod未由Transaction注释。 then there wont be any rollback. 那么就不会有任何回滚。 because calls between the method to method in proxy is not handled by Spring context. 因为代理上下文中的方法与方法之间的调用不是由Spring上下文处理的。 thus it wont be able to wrap the transaction. 因此它将无法包装交易。

if the Second is not valid, please enable the log verify the log. 如果秒无效,请启用日志并验证日志。 your logs should show when the transaction starts and when it ends and what are methods are added to the transaction context. 您的日志应显示事务何时开始,何时结束以及向事务上下文中添加了哪些方法。

Like below 像下面

DEBUG org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'saveDomain' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''


2014-04-04 10:25:24,276 [main] TRACE org.springframework.orm.jpa.JpaTransactionManager - Triggering beforeCompletion synchronization
2014-04-04 10:25:24,276 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Initiating transaction rollback

And so 所以

Note** , if you DAO Layer also annotated with Transaction and with propagation rule as Propagation.REQUIRES_NEW - the Dao method considered as individual UoW . 注意**,如果您的DAO层也用Transaction和传播规则标注为Propagation.REQUIRES_NEW则将Dao方法视为单独的UoW。 So throwing exception from service layer would not rollback the previous transaction. 因此,从服务层引发异常不会回滚先前的事务。

In general - it would be advise to keep the transaction in one layer and preferably with Service - Layer 一般而言-建议将交易保持在一层,最好使用服务层-

I remember I also had such a problem. 我记得我也有这样的问题。 My problem was connected with a proper configuration. 我的问题与正确的配置有关。 For instance: Did you specify in the TransactionManager declaration something like this: 例如:您是否在TransactionManager声明中指定了以下内容:

transactionManager.setRollbackOnCommitFailure(true); transactionManager.setRollbackOnCommitFailure(真);

you may also use a debugger and catch the execution in your implementation of TransactionManager. 您还可以使用调试器,并在TransactionManager的实现中捕获执行。 You have there a method like rollback and you will see what is the reason that this operation is ommited. 您有一种类似回滚的方法,您将看到省略此操作的原因是什么。

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

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