JavaWeb学习:Spring的事务管理
2021-03-11 03:28
                         标签:多个   upd   定义   nsa   big   位置   round   声明式   demo1    一、Spring的事务管理的API   ①、PlatformTransactionManager:平台事务管理器       ②、TransactionDefinition         :事务定义信息     事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读   ③、TransactionStatus:事务的状态     事务状态:用于记录在事务管理过程中,事务的状态的对象。 二、事务管理的API的关系   Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。 三、Spring的事务的传播行为   Spring中提供了七中事务的传播行为       保证多个操作不在同一个事务中 四、Spring的事务管理   ①、搭建Spring的事务管理的环境     Ⅰ、创建Service的接口和实现类     Ⅱ、创建DAO的接口和实现类     Ⅲ、配置Service和DAO交给Spring管理     Ⅳ、配置连接池     Ⅴ、在DAO注入Jdbc的模板(注入的是dataSource的原因extends JdbcDaoSupport ,查看JdbcDaoSupport 的代码有setDataSource方法)     Ⅵ、编写测试方法 五、Spring的事务管理   ①、一类:编程式事务(需要手动编写代码)--了解     Ⅰ、配置平台事务管理器     Ⅱ、Spring提供了事务管理的模板类     Ⅲ、业务层(Service)需要使用事务,将书屋管理模板类注入到业务层   ②、二类:声明式事务管理(通过配置实现)---底层原理:AOP(面向切面)     在方法上通过增强的方式添加事务处理,此事务增强是比较特殊的增强方式。     Ⅰ、XML方式的声明式事务管理       1、引入aop的开发包       2、配置事务管理器       3、配置增强 在使用       4、AOP的配置     Ⅱ、注解方式的声明式事务管理       1、引入aop的开发包       2、配置事务管理器       3、开启注解事务       4、在业务层添加注解   JavaWeb学习:Spring的事务管理 标签:多个   upd   定义   nsa   big   位置   round   声明式   demo1    原文地址:https://www.cnblogs.com/WarBlog/p/14134544.html
public void 方法A(){
  xxDao.save();
  yyDao.update();        
}
public void 方法b(){
  方法A();
  zzDao.del();
  ttDao.update();        
}
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
    this.accountDao = accountDao;
    }
    @Override
    public void transfer(String from, String to, BigDecimal money) {
    accountDao.outMoney(from, money);
    accountDao.inMoney(to, money);
    }
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public void outMoney(String from, BigDecimal money) {
    this.getJdbcTemplate().update("update account set money=money - ? where name = ?", money, from);
    }
    @Override
    public void inMoney(String to, BigDecimal money) {
    this.getJdbcTemplate().update("update account set money=money + ? where name = ?", money, to);
    }
}
    bean id="accountService" class="com.xxx.spring.transaction.demo1.AccountServiceImpl">
        property name="accountDao" ref="accountDao">property>
    bean>
    bean id="accountDao" class="com.xxx.spring.transaction.demo1.AccountDaoImpl">
        
    bean>
    context:property-placeholder location="classpath:jdbc.properties" />
    bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        property name="driverClass" value="${jdbc.driverClass}" />
        property name="jdbcUrl" value="${jdbc.url}" />
        property name="user" value="${jdbc.username}" />
        property name="password" value="${jdbc.password}" />
    bean>
    bean id="accountDao" class="com.xxx.spring.transaction.demo1.AccountDaoImpl">
        property name="dataSource" ref="dataSource">property>
    bean>
@SpringJUnitConfig(locations = "classpath:tx.xml")
public class SpringTxDemo {
    @Autowired
    private AccountService accountService;
    @Test
    public void demo1() {
    accountService.transfer("zhangsan", "lisi", new BigDecimal(1000.00));
    }
}
    bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource"/>
    bean>
    bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        property name="transactionManager" class="transactionManager"/>
    bean>
    bean id="accountService" class="com.xxx.spring.transaction.demo1.AccountServiceImpl">
        property name="accountDao" ref="accountDao">property>
        
        property name="transactionTemplate" ref="transactionTemplate"/>
    bean>
    bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource"/>
    bean>
    
    tx:advice id="txadvice" transaction-manager="transactionManager">
        tx:attributes>
            
            
            tx:method name="*" propagation="REQUIRED" />
        tx:attributes>
    tx:advice>
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 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    aop:config>
        
        aop:pointcut expression="execution(* com.xxx.spring.transaction.demo1.AccountServiceImpl.*(..))" id="pointcut1"/>
        
        aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
        
        
    aop:config>
    bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource" />
    bean>
    tx:annotation-driven transaction-manager="transactionManager"/>
@Transactional
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
    this.accountDao = accountDao;
    }
    
    @Override
    public void transfer(String from, String to, BigDecimal money) {
    accountDao.outMoney(from, money);
    int d = 1 / 0;
    accountDao.inMoney(to, money);
    }
}
文章标题:JavaWeb学习:Spring的事务管理
文章链接:http://soscw.com/index.php/essay/63012.html