spring事务管理
2021-02-14 13:18
标签:interface 建模 info UNC 详情 config getbean aci 概念 1、事务 (1)概念 一组业务操作,要求要么全不成功,要么全部不成功 (2)特性(ACID) 原子性、一致性、隔离性、持久性 (3)隔离问题 脏读 不可重复读 幻读 (4)隔离级别 read uncommitted:读未提交,存在三个问题 read committed:读已提交,解决了脏读,存在两个问题 repeatable read:可重复读,解决脏读、不可重复度、存在一个问题 serializable:串行化,都解决了,单事务 (5)savePoint(保存点) 记录操作的当前位置,之后可以回滚到指定指定的位置(可以回滚一部分) 2、spring的事务管理 (1)导包 tx: 事务管理器: 第一个jdbc的时候使用,第二个整合hibernate的时候使用 (2)三个顶级接口 (3)传播行为 第一个为默认值 3、手动管理事务 (1)创建表 (2)导入jar包 核心:4+1 aop:aop联盟、spring aop、aspectj规范、spring aspect 数据库:jdbc、tx 连接池:C3P0 (3)创建Dao层和Service层的接口和接口的实现类: Dao层: Service层: 在实现类里面制造异常,用于校验 (4)配置文件: (5)测试: 在出现异常后不管是转账方还是收款方,金额均未发生变化 4、aop配置基于xml (1)创建Service层和Dao层以及接口和接口的实现类: Service层: Dao层: (2)配置文件: (3)测试类: 没有异常发生的时候能够保证转账金额的减少量和收款人的金额的增加量是一样的,当有异常发生时,转账方和收款方的金额都不会发生变化。 5、注解方式 (1)Dao层和Service层的接口和接口的实现类: Dao层: Service层: (2)配置文件: (3)测试类: spring事务管理 标签:interface 建模 info UNC 详情 config getbean aci 概念 原文地址:https://www.cnblogs.com/zhai1997/p/12722124.htmlpublic interface AccountDao {
void out(String outer,Integer money);
void in(String inner,Integer money);
}
public interface AccountService {
void transfer(String outer,String inner,Integer money);
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
@Override
public void transfer(final String outer,final String inner,final Integer money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.out(outer, money);
int i=1/0;
accountDao.in(inner, money);
}
});
}
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao=accountDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate=transactionTemplate;
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}
public interface AccountService {
void transfer(String outer,String inner,Integer money);
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
@Override
public void transfer( String outer, String inner, Integer money) {
accountDao.out(outer, money);
int num=1/0;
accountDao.in(inner, money);
}
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao=accountDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate=transactionTemplate;
}
}
public interface AccountDao {
void out(String outer,Integer money);
void in(String inner,Integer money);
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
@Override
public void out(String outer, Integer money) {
this.getJdbcTemplate().update("update transfer set money=money-? where username=?",money,outer);
}
@Override
public void in(String inner, Integer money) {
this.getJdbcTemplate().update("update transfer set money=money+? where username=?",money,inner);
}
}
xml version="1.0" encoding="UTF-8"?>
beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
property name="driverClass" value="com.mysql.jdbc.Driver">property>
property name="jdbcUrl" value="jdbc:mysql:///bank_transfer">property>
property name="user" value="root">property>
property name="password" value="root">property>
bean>
bean id="accountDao" class="pers.zhb.dao.AccountDaoImpl">
property name="dataSource" ref="dataSource">property>
bean>
bean id="accountService" class="pers.zhb.service.AccountServiceImpl">
property name="accountDao" ref="accountDao">property>
bean>
bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource">property>
bean>
tx:advice id="txAdvice" transaction-manager="txManager">
tx:attributes>
tx:method name="transfer()" propagation="REQUIRED" isolation="DEFAULT"/>
tx:attributes>
tx:advice>
aop:config>
aop:advisor advice-ref="txAdvice" pointcut="execution(* pers.zhb.service..*.*(..))">aop:advisor>
aop:config>
beans>
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}
public interface AccountDao {
void out(String outer,Integer money);
void in(String inner,Integer money);
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
@Override
public void out(String outer, Integer money) {
this.getJdbcTemplate().update("update transfer set money=money-? where username=?",money,outer);
}
@Override
public void in(String inner, Integer money) {
this.getJdbcTemplate().update("update transfer set money=money+? where username=?",money,inner);
}
}
public interface AccountService {
void transfer(String outer,String inner,Integer money);
}
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
@Override
public void transfer( String outer, String inner, Integer money) {
accountDao.out(outer, money);
accountDao.in(inner, money);
}
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao=accountDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate=transactionTemplate;
}
}
xml version="1.0" encoding="UTF-8"?>
beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
property name="driverClass" value="com.mysql.jdbc.Driver">property>
property name="jdbcUrl" value="jdbc:mysql:///bank_transfer">property>
property name="user" value="root">property>
property name="password" value="root">property>
bean>
bean id="accountDao" class="pers.zhb.dao.AccountDaoImpl">
property name="dataSource" ref="dataSource">property>
bean>
bean id="accountService" class="pers.zhb.service.AccountServiceImpl">
property name="accountDao" ref="accountDao">property>
bean>
bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource">property>
bean>
tx:annotation-driven transaction-manager="txManager">tx:annotation-driven>
beans>
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}