使用spring4時Transaction不起作用@Transactional和jdbctemplate !!請幫我使用spring4時事務不起作用@Transactional
的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)
代理創建失敗?
你在哪裏調用tt()方法。舉個例子 –
參見:http://stackoverflow.com/a/21188820/1199132 –
讓我猜你還有一個'DispatcherServlet',它有一個不同的配置文件,它也包含一個''...... –