spring 事务管理让我费解的地方
在用spring mvc时,用到了spring事务管理
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"><value>${jdbc.driver}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="maxActive"><value>${maxActive}</value></property>
<property name="maxWait"><value>${maxWait}</value></property>
<property name="maxIdle"><value>${maxIdle}</value></property>
<property name="removeAbandoned"><value>${removeAbandoned}</value></property>
<property name="removeAbandonedTimeout"><value>${removeAbandonedTimeout}</value></property>
<property name= "validationQuery"><value>${validationQuery}</value></property>
<property name= "defaultAutoCommit"><value>true</value></property>
</bean>
<!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务管理器 p:dataSource-ref="dataSource" />-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethods" expression=" execution(* app.com.*.service..*.*(..))" />*
<aop:advisor pointcut-ref="serviceMethods" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="batchUpdate" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
上面事务配置我理解的意思是:通过spring aop配置让工程service包下的所有以add、edit、save、update等开头的方法对其自动的事务提交、回滚
在service包下有个方法我是这样写的:
package app.com.test.service;
@Repository
public class TestServiceImpl implements TestService {
@Autowired
private TestJdbcDAO testJdbcDAO;
public void saveTest() throws Exception{
String sql1="insert into ATTACHINFO(id,name) values(1,'test1')";
testJdbcDAO.update(sql1);
String sql2="insert into ATTACHINFO(id1,name) values(2,'test2')";//错误sql语句
testJdbcDAO.update(sql2);
}
执行这个方法中,sql1是正确的,sql2是错误的,执行完在数据库中看到第一条数据插入进去了。我理解的是两条数据都不会入库,会进行事务回滚。
这挺让我费解的,求大神指教