- 事务的回滚
- 在这里我用一个数据库的用户扣钱,加钱的例子:
我随便建了一个数据库,里面有两个用户:
- 在这里我用一个数据库的用户扣钱,加钱的例子:
然后在pom.xml里面写上一个数据库资源文件的操作:`
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 这里是数据库资源文件的位置 -->
<context:property-placeholder location="classpath:properties/jdbc.properties"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="${jdbcDriver}" />
<property name="url" value="${jdbcUrl}" />
<property name="username" value="${jdbcUsername}" />
<property name="password" value="${jdbcPassword}" />
</bean>
</beans>
然后一个main方法:
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("classpath:lesson04/prop.xml");
DataSource bean = (DataSource) cac.getBean("dataSource");
Connection c = bean.getConnection();
try {
String sql1 = "UPDATE money SET lmoney=lmoney-50 WHERE id=1";
String sql2 = "UPDATE money SET lmoney=lmoney+50 WHERE id=2";
c.setAutoCommit(false);// 设置手动提交
c.createStatement().execute(sql1);// 自动提交
int i = 5/0;
c.createStatement().execute(sql2);
c.commit();
} catch (Exception e) {
// TODO: handle exception
c.rollback();
}
还是上面那个例子,注解版在来一发:
xml文件代码
有点多
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描注解 -->
<context:component-scan
base-package="com.ps.lesson04"></context:component-scan>
<context:property-placeholder
location="lesson04/jdbc.properties"></context:property-placeholder>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbcDriver}" />
<property name="url" value="${jdbcUrl}" />
<property name="username" value="${jdbcUsername}" />
<property name="password" value="${jdbcPassword}" />
</bean>
<!-- jdbc的模板类 通常用于操作 增删查改 注入数据源 -->
<bean id="jdbc"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事物管理器 用于提交和回滚 -->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<aop:config >
<!-- 切点一般都是拦截所有的service包 所有接口 所有方法 -->
<aop:pointcut expression="execution(* com.ps.lesson04.*.transfer*(..))" id="aop"/>
<aop:advisor pointcut-ref="aop" advice-ref="txAdvice"/>
</aop:config>
<!-- 通知 一般数据库的sql操作异常都是操作型异常
所有定义的事务 运行时异常回滚 检查异常不会回滚
rollback-for="你要回滚的检查异常"
rollback-for="java.lang.Exception" >>>>>代表所有的检查异常会回滚
no-rollback-for="什么样的运行时异常不回滚"
no-rollback-for="java.lang.RuntimeException" >>>>>代表所有的运行异常不会回滚
-->
<tx:advice id="txAdvice" transaction-manager="tm">
<tx:attributes>
<!-- propagation:事务的传播特性
read-only:禁止事务默认false
-->
<tx:method name="transfer*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED"/>
<!-- 除了上面所有定义的方法 以外都不用事物 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>