2

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 ?

xwei_chen
  • 65
  • 6

2 Answers2

1

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. enter image description here

ottercoder
  • 852
  • 1
  • 12
  • 31
0

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.

Christopher Yang
  • 3,769
  • 4
  • 30
  • 27
  • Just read your edited question. First, your dao method throws a java.lang.Exception but your code is not throwing any checked exception, neither is jdbcTemplate. Why declare it? Second why add rollbackFor=Exception.class? If the only exception possible in this method is a RuntimeException, there's no need to add this. Take away both unnecessary statements and try again. It'll make your code look a whole lot cleaner and hopefully give you the rollback that you wanted. – Christopher Yang May 14 '16 at 22:29