Spring的事务管理:

Spring的事务管理:

手动编码的方式完成事务管理:

需要事务管理器:真正管理事务对象.
* Spring提供了事务管理的模板(工具类.)

第一步:注册事务管理器:

<!-- 配置事务管理器 -->
    <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" ref="transactionManager"/>
    </bean>

第三步:在业务层注入模板类:(模板类管理事务)

<!-- 业务层类 -->
    <bean id="accountService" class="cn.itcast.spring3.demo1.AccountServiceImpl">
        <!-- 在业务层注入Dao -->
        <property name="accountDao" ref="accountDao"/>
        <!-- 在业务层注入事务的管理模板 -->
        <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>

第四步:在业务层代码上使用模板:

public void transfer(final String from, final String to, final Double money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                accountDao.out(from, money);
                int d = 1 / 0;
                accountDao.in(to, money);
            }
        });
    }

手动编码方式缺点:
* 代码量增加,代码有侵入性.

声明式事务管理:(原始方式)

基于TransactionProxyFactoryBean.
导入:aop相应jar包.

第一步:注册平台事务管理器:

<!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

第二步:创建业务层代理对象:

<!-- 配置生成代理对象 -->
    <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!-- 目标对象 -->
        <property name="target" ref="accountService"/>
        <!-- 注入事务管理器 -->
        <property name="transactionManager" ref="transactionManager"/>
        <!-- 事务的属性设置 -->
        <property name="transactionAttributes">
            <props>
            //key:service中的方法
                <prop key="transfer">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

第三步:编写测试类:
* 千万注意:注入代理对象

@Autowired
@Qualifier("accountServiceProxy")
private AccountService accountService;




<prop key="transfer">PROPAGATION_REQUIRED</prop>
prop格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
顺序:传播行为、隔离级别、事务是否只读、发生哪些异常可以回滚事务(所有的异常都回滚)、发生了哪些异常不回滚.
  • 缺点:就是需要为每一个管理事务的类生成代理.需要为每个类都需要进行配置.

声明式事务管理:(自动代理.基于切面 )

第一步:导入相应jar包.
* aspectj

第二步:引入相应约束:
* aop、tx约束.

<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 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.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 id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

第四步:定义增强(事务管理)

<!-- 定义一个增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 增强(事务)的属性的配置 -->
        <tx:attributes>
            <!-- 
                isolation:DEFAULT   :事务的隔离级别.
                propagation         :事务的传播行为.
                read-only           :false.不是只读
                timeout             :-1
                no-rollback-for     :发生哪些异常不回滚
                rollback-for        :发生哪些异常回滚事务
             -->
            <tx:method name="transfer"/>
        </tx:attributes>
    </tx:advice>

第五步:定义aop的配置(切点和通知的组合)

<!-- aop配置定义切面和切点的信息 -->
    <aop:config>
        <!-- 定义切点:哪些类的哪些方法应用增强 -->
        <aop:pointcut expression="execution(* cn.itcast.spring3.demo3.AccountService+.*(..))" id="mypointcut"/>
        <!-- 定义切面: -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
    </aop:config>

第六步:编写测试类:
* 注入Service对象,不需要注入代理对象(生成这个类的时候,已经是代理对象.)

基于注解的事务管理:

第一步:事务管理器:

<!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

第二步:注解事务:

<!-- 开启注解的事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

第三步:在Service上使用注解

@Transactional
* 注解中有属性值:
* isolation
* propagation
* readOnly
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值