Spring事务管理
1.导入jar包
spring-tx.jar,spring-orm.jar,spring-jdbc.jar
2.Spring提供了三个接口:
PlatfromTransactionManager:平台事务管理
transactionStatus getT然saction(transaction Definition);获取一个事务
commit();提交
rollback();回滚
TransactionDefinition:事务的定义
事务的传播行为:两个方法之间有一个调用了另个一,可能同时存在俩个方法都有事务,该如何使用事务的方法,为传播行为
事物的隔离级别:isolation_defalult
TractionStatus:事务的状态
是否有保存点
<!-- 添加事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
基于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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 单独的注入jdbcTemplate 将数据源给他-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- dao中注入jdbcTempl-->
<bean id="accountDao" class="com.oracle.daoImpl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="accountService" class="com.oracle.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 添加事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
1.编程式事务 淘汰了
2.声明式事务 aop
我们要把事务放到目标方法上 transfer 必须用 aop -->
<!-- 切面类, 把类中的方法变成 增强呢 ? -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="myPointCut" expression="execution(* com.oracle.serviceImpl.*.* (..))"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"></aop:advisor>
</aop:config>
</beans>
package com.oracle.pojo;
public class Account {
private Integer aid;
private String userName;
private Integer balance;
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", userName='" + userName + '\'' +
", balance=" + balance +
'}';
}
}
package com.oracle.daoImpl;
import com.oracle.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* spring框架的 jdbcTemplate
*/
public class AccountDaoImpl implements AccountDao {
JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void outer(String outer, Integer money) {
String sql = "update account set balance = balance - ? where userName = ?";
jdbcTemplate.update(sql,money,outer);
}
@Override
public void inner(String inner, Integer money) {
String sql = "update account set balance = balance + ? where userName = ?";
jdbcTemplate.update(sql,money,inner);
}
}
package com.oracle.serviceImpl;
import com.oracle.dao.AccountDao;
import com.oracle.service.AccountService;
public class AccountServiceImpl implements AccountService {
//setter方法注入
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, Integer money) {
accountDao.outer(outer,money);
int i = 1/0;
accountDao.inner(inner,money);
}
}
package com.oracle.test;
import com.oracle.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestTx {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService bean = context.getBean(AccountService.class);
bean.transfer("张三","李四",3000);
}
}
基于注解开发
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描器-->
<context:component-scan base-package="com.oracle"/>
<!-- 注解驱动 -->
<context:annotation-config/>
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
package com.oracle.pojo;
public class Account {
private Integer aid;
private String userName;
private Integer balance;
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", userName='" + userName + '\'' +
", balance=" + balance +
'}';
}
}
package com.oracle.daoImpl;
import com.alibaba.druid.pool.DruidDataSource;
import com.oracle.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
* spring框架的 jdbcTemplate
*/
@Repository
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Autowired
public AccountDaoImpl(DruidDataSource dataSource){
super.setDataSource(dataSource);
}
@Override
public void outer(String outer, Integer money) {
String sql = "update account set balance = balance - ? where userName = ?";
this.getJdbcTemplate().update(sql,money,outer);
}
@Override
public void inner(String inner, Integer money) {
String sql = "update account set balance = balance + ? where userName = ?";
this.getJdbcTemplate().update(sql,money,inner);
}
}
package com.oracle.serviceImpl;
import com.oracle.dao.AccountDao;
import com.oracle.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.beans.Transient;
@Service
public class AccountServiceImpl implements AccountService {
//setter方法注入
@Autowired
private AccountDao accountDao;
@Override
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
public void transfer(String outer, String inner, Integer money) {
accountDao.outer(outer,money);
int i = 1/0;
accountDao.inner(inner,money);
}
}
package com.oracle.test;
import com.oracle.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestTx {
@Autowired
private AccountService accountService;
@Test
public void test1(){
accountService.transfer("张三","李四",1000);
}
}