简体   繁体   中英

Transaction not work when use spring4 @Transactional

Transaction not work when use spring4 @Transactional and jdbctemplate!! Please help me

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 code

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 code

    @Controller("")
public class Tcontroller {

    @Autowired TService ts; 

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

}

the tt() throw a exception,but Transaction No rollback!

today,i found this

[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)

proxy create fail ?

I've got the same problem when I was throwing null at tests. It appears to be @Transaction works with an instance or subclass of RuntimeException only.

Which Exception is not. 在此处输入图片说明

A transaction is always rolled back if it encounters a unchecked runtime exception. Your problem is probably caused by the fact that you caught the exception and then re-throw it. It's quite unnecessary to do such a thing.

Section 16.5.3 : In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. ( Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

Read the official doc .

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