spring事务配置文件,xml

spring中的事务,基于springAOP,需要配置jdbc配置数据源
配置顺序:
1.定义好jdbc数据源
2.配置tx:advice事务通知
3.配置切入点aop:config

<?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 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">
    <!-- Spring的事务管理机制
            Spring事务管理高层抽象主要包括3个接口,Spring的事务主要是由他们共同完成的:
                PlatformTransactionManager: 事务管理器—主要用于平台相关事务的管理
                TransactionDefinition:	     事务定义信息(隔离、传播、超时、只读)—通过配置如何进行事务管理。
                TransactionStatus:          事务具体运行状态—事务管理过程中,每个时间点事务的状态信息

            Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现
                Spring JDBC或iBatis 进行持久化数据时使用    - org.springframework.jdbc.datasource.DataSourceTransactionManager

            TransactionDefinition   事务定义信息(隔离、传播、超时、只读)
                事务的隔离级别IsolationLevel
                    DEFAULT	        使用后端数据库默认的隔离级别(spring中的的选择项)
                    READ_UNCOMMITED	允许你读取还未提交的改变了的数据。可能导致脏、幻、不可重复读
                    READ_COMMITTED	允许在并发事务已经提交后读取。可防止脏读,但幻读和 不可重复读仍可发生
                    REPEATABLE_READ	对相同字段的多次读取是一致的,除非数据被事务本身改变。可防止脏、不可重复读,但幻读仍可能发生。
                    SERIALIZABLE	完全服从ACID的隔离级别,确保不发生脏、幻、不可重复读。这在所有的隔离级别中是最慢的,它是典型的通过完全锁定在事务中涉及的数据表来完成的。
                事务的传播行为PropagationBehavior
                    PROPAGATION_REQUIRED	    支持当前事务,如果不存在 就新建一个
                    PROPAGATION_SUPPORTS	    支持当前事务,如果不存在,就不使用事务
                    PROPAGATION_MANDATORY	    支持当前事务,如果不存在,抛出异常
                    PROPAGATION_REQUIRES_NEW	如果有事务存在,挂起当前事务,创建一个新的事务
                    PROPAGATION_NOT_SUPPORTED	以非事务方式运行,如果有事务存在,挂起当前事务
                    PROPAGATION_NEVER 	        以非事务方式运行,如果有事务存在,抛出异常
                    PROPAGATION_NESTED	        如果当前事务存在,则嵌套事务执行只对DataSourceTransactionManager 起效
                Timeout:获取超时时间(事务的有效期)
                isReadOnly 是否只读(保存、更新、删除—对数据进行操作-变成可读写的,查询-设置这个属性为true,只能读不能写),事务管理器能够根据这个返回值进行优化

            TransactionStatus 事务状态
                hasSavepoint():     判断是否有保留点
                isCompleted():      判断事务是否结束
                isNewTransaction(): 判断当前事务是否是新开的一个事务。
                isRollbackOnly():   判断事务是否只能回滚
                setRollbackOnly():  设置事务是否回滚

        Spring事务管理两种方式
            1: 编程式的事务管理 - 通过TransactionTemplate手动管理事务
            2: 使用XML或注解配置 - 声明式事务
                Spring的声明式事务是通过AOP实现的(环绕通知)
                    开发中经常使用(代码侵入性最小)- 推荐使用!
                    不需要修改业务层的代码,也不需要手动添加transactionManager.commit(),
                        也不需要手动添加transactionManager.rollback(),更不需要使用try...catch...
    -->

    <!-- spring事务
            1: xml
            2: 注解
    -->
    <!-- 第一步:定义具体的平台事务管理器(DataSource事务管理器)
         第二步:定义通知,通知中要处理的就是事务
             配置事务的属性定义
             isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1"  默认值
                rollback-for:遇到哪些异常就回滚,其他的都不回滚
			    no-rollback-for:遇到哪些异常不回滚,其他的都回滚。和上面互斥的
         第三步:配置切入点,让通知关联切入点,即事务控制业务层的方法
     -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1"/>
            <tx:method name="add*"/>
            <tx:method name="save*"/>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="edit*"/>
            <tx:method name="delete*"/>
            <tx:method name="remove*"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.igeek.spring.jdbc.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>
    -->


    <!-- spring事务管理
            2: 注解
    -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值