使用spring4 @Transactional时,事务不起作用

时间:2016-05-12 15:41:53

标签: java spring transactions jdbctemplate spring-transactions

使用spring4 @Transactional和jdbctemplate时交易不起作用!!请帮帮我

application.xml中

 <context:component-scan base-package="com.test" />

<bean id="txtManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<tx:annotation-driven transaction-manager="txtManager"   proxy-target-class="true"/>

java代码

package com.test;

 @Component

public class TService {

@Autowired JdbcTemplate jdbcTemplate;

@Transactional(rollbackFor = Exception.class)
public void tt() throws Exception{

        jdbcTemplate.update("insert into t_test values(1)");
        jdbcTemplate.update("insert into t_test(dd) values(1)");

    }

}

控制器代码

    @Controller("")
public class Tcontroller {

    @Autowired TService ts; 

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public @ResponseBody Object all() throws Exception {
        ts.tt();
        return "";
    }

}

tt()抛出异常,但是事务没有回滚!

今天,我发现了这个

[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:833) - Unable to apply any optimisations to advised method: public void com.test.TService.tt() throws java.lang.Exception
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:797) - Found 'equals' method: public boolean java.lang.Object.equals(java.lang.Object)
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:833) - Unable to apply any optimisations to advised method: public java.lang.String java.lang.Object.toString()
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:802) - Found 'hashCode' method: public native int java.lang.Object.hashCode()
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:833) - Unable to apply any optimisations to advised method: protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:791) - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)
[DEBUG] 23:36:19,729 org.springframework.aop.framework.CglibAopProxy (accept:791) - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)

代理创建失败?

2 个答案:

答案 0 :(得分:1)

当我在测试时抛出null时,我遇到了同样的问题。它似乎是@Transaction只与RuntimeException的实例或子类一起使用。

哪种例外不是。  enter image description here

答案 1 :(得分:0)

如果事务遇到未经检查的运行时异常,则始终回滚该事务。您的问题可能是由于您捕获异常然后重新抛出它而引起的。做这样的事情是没必要的。

  

第16.5.3节:在其默认配置中,是Spring Framework的事务   基础结构代码仅标记用于回滚的事务   运行时,未经检查的异常;也就是抛出异常的时候   是RuntimeException的实例或子类。 (错误也会 -   默认情况下 - 导致回滚)。检查抛出的异常   从事务方法不会导致默认回滚   配置。

阅读official doc