准备工作
<?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 https://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " jdbc.properties" />
< context: component-scan base-package = " com.beyondzy.spring" > </ context: component-scan>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " username" value = " ${user}" > </ property>
< property name = " password" value = " ${password}" > </ property>
< property name = " url" value = " ${url}" > </ property>
< property name = " driverClassName" value = " ${driverName}" > </ property>
</ bean>
< bean id = " template" class = " org.springframework.transaction.support.TransactionTemplate" >
< property name = " transactionManager" ref = " transactionManager" > </ property>
</ bean>
</ beans>
使用默认方式进行事务管理
public void transfer ( String to , String from, int money) {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition ( ) ;
TransactionStatus transactionStatus = manager. getTransaction ( definition) ;
String sql_to = "update user set money = money - ? where name = ?" ;
String sql_from = "update user set money = money + ? where name = ?" ;
template. update ( sql_to, money, to ) ;
template. update ( sql_from, money, from) ;
manager. rollback ( transactionStatus) ;
}
使用模块的方式进行事务管理
public void transfer1 ( String to , String from, int money) {
transactionTemplate. execute ( new TransactionCallbackWithoutResult ( ) {
@Override
protected void doInTransactionWithoutResult ( TransactionStatus transactionStatus) {
String sql_to = "update user set money = money - ? where name = ?" ;
String sql_from = "update user set money = money + ? where name = ?" ;
template. update ( sql_to, money, to ) ;
template. update ( sql_from, money, from) ;
}
} ) ;
}
aop进行事务管理
实现MethodInterceptor,创建通知类
package com. beyondzy. spring. aop ;
import org. aopalliance. aop. Advice ;
import org. aopalliance. intercept. MethodInterceptor ;
import org. aopalliance. intercept. MethodInvocation ;
import org. springframework. transaction. PlatformTransactionManager ;
import org. springframework. transaction. TransactionStatus ;
import org. springframework. transaction. support. DefaultTransactionDefinition ;
public class TxAdvice implements MethodInterceptor {
private PlatformTransactionManager manager;
public void setManager ( PlatformTransactionManager manager) {
this . manager = manager;
}
@Override
public Object invoke ( MethodInvocation methodInvocation) throws Throwable {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition ( ) ;
TransactionStatus transactionStatus = manager. getTransaction ( definition) ;
Object returnval = null ;
try {
returnval = methodInvocation. getMethod ( ) . invoke ( methodInvocation. getThis ( ) , methodInvocation. getMethod ( ) ) ;
} catch ( Exception e) {
System . out. println ( e. getMessage ( ) ) ;
}
manager. rollback ( transactionStatus) ;
return returnval;
}
}
配置事务
< bean id = " txAdvice" class = " com.beyondzy.spring.aop.TxAdvice" >
< property name = " manager" ref = " transactionManager" > </ property>
</ bean>
< aop: config>
< aop: pointcut id = " " expression = " " />
< aop: advisor pointcut = " " advice-ref = " txAdvice" > </ aop: advisor>
</ aop: config>