我是Spring交易的新手.我使用Spring 3.2.2和MySQL 5.5.20(InnoDB).我可以在日志文件中看到它确实回滚了,但是在数据库中,记录仍然被更新为9.我错过了什么?谢谢.
config.xml:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="xxx" />
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="hello" class="com.xol.oss.HelloService">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
Run Code Online (Sandbox Code Playgroud)
Java代码:
public void setDataSource(BasicDataSource dataSource) {
this.dataSource = dataSource;
}
@Transactional
public void getData() {
Connection con=null;
try {
con = dataSource.getConnection();
Statement stat = con.createStatement();
stat.executeUpdate("update testdata set foo=9 where id=1");
throw new RuntimeException("an Exception for test");
} …Run Code Online (Sandbox Code Playgroud)