创建数据库:
create database transaction1;
use transaction1;
create table account(id int primary key auto_increment,
username varchar(30),
money int);
insert into account(username,money) values('fly',80);
insert into account(username,money) values('wang',100);
导入jar包
- 核心:4+1
- aop : 4 (aop联盟、spring aop、aspectj规范、spring aspect)
- 数据库:2 (jdbc/tx)
- 驱动:mysql
- 连接池:c3p0
创建DAO:
package com.fly.transaction.dao;
public interface AccountDao {
void out(String user,int money);
void in(String user,int money);
}
package com.fly.transaction.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class TransactionDao extends JdbcDaoSupport implements AccountDao{
@Override
public void out(String user, int money) {
this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,user);
}
@Override
public void in(String user, int money) {
this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,user);
}
}
创建Service:
package com.fly.transaction.service;
public interface TransactionService {
void transfer(String outer, String inner, int money);
}
package com.fly.transaction.service;
import com.fly.transaction.dao.TransactionDao;
public class TransactionServiceImp implements TransactionService {
private TransactionDao dao;
public void setTransactionDao(TransactionDao dao) {
this.dao = dao;
}
@Override
public void transfer(String outer, String inner, int money) {
dao.out(outer, money);
dao.in(inner, money);
}
}
配置文件applicationContext:
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1 datasource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transaction1"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 这里注意id不能乱写,最好写TransactionDao首字母小写即transactionDao -->
<bean id="transactionDao" class="com.fly.transaction.dao.TransactionDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3 service -->
<bean id="accountService" class="com.fly.transaction.service.TransactionServiceImp">
<property name="transactionDao" ref="transactionDao"></property>
</bean>
</beans>
测试类:
package com.fly.transaction.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fly.transaction.service.TransactionServiceImp;
public class TestDemo {
@Test
public void test(){
String xmlPath = "appcationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
TransactionServiceImp bean = (TransactionServiceImp) context.getBean("accountService");
bean.transfer("fly", "wang", 10);
}
}
工厂bean 生成代理:半自动
spring提供 管理事务的代理工厂bean TransactionProxyFactoryBean
1.getBean() 获得代理对象
2.spring 配置一个代理
spring配置
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1 datasource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transaction1"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 2 dao -->
<bean id="transactionDao" class="com.fly.transaction.dao.TransactionDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4 service 代理对象
4.1 proxyInterfaces 接口
4.2 target 目标类
4.3 transactionManager 事务管理器
4.4 transactionAttributes 事务属性(事务详情)
prop.key :确定哪些方法使用当前事务配置
prop.text:用于配置事务详情
格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
传播行为 隔离级别 是否只读 异常回滚 异常提交
例如:
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 默认传播行为,和隔离级别
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 只读
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop> 有异常扔提交
-->
<!-- 3 service -->
<bean id="proxyAccountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces" value="com.fly.transaction.service.TransactionService"></property>
<property name="target" ref="transactionServiceImp"></property>
<property name="transactionManager" ref="txManager"></property>
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
<!-- 5 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionServiceImp" class="com.fly.transaction.service.TransactionServiceImp">
<property name="transactionDao" ref="transactionDao"></property>
</bean>
</beans>
再次测试:
package com.fly.transaction.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fly.transaction.service.TransactionService;
public class TestDemo {
@Test
public void test(){
String xmlPath = "appcationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
// 接口
TransactionService bean = (TransactionService) context.getBean("proxyAccountService");
bean.transfer("fly", "wang", 10);
}
}
AOP 配置基于xml
在spring xml 配置aop 自动生成代理,进行事务的管理
1.配置管理器
2.配置事务详情
3.配置aop
<!-- 4 事务管理 -->
<!-- 4.1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 事务详情(事务通知) , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/>
</aop:config>
AOP配置基于注解
1.配置事务管理器,将并事务管理器交予spring
2.在目标类或目标方法添加注解即可 @Transactional
spring配置
<!-- 4 事务管理 -->
<!-- 4.1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 将管理器交予spring
* transaction-manager 配置事务管理器
* proxy-target-class
true : 底层强制使用cglib 代理
-->
<tx:annotation-driven transaction-manager="txManager"/>
service 层
@Transactional
public class AccountServiceImpl implements AccountService {
事务详情配置
@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {