• spring4声明式事务—02 xml配置方式


    1.配置普通的 controller,service ,dao 的bean.

    <!-- 配置 dao ,service -->
        <bean id="bookShopDao" class="com.liujl.spring.tx.xml.BookShopDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        
        <bean id="bookShopService" class="com.liujl.spring.tx.xml.serivice.impl.BookShopServiceImpl">
            <property name="bookShopDao" ref="bookShopDao"></property>
        </bean>
        
        <bean id="cashier" class="com.liujl.spring.tx.xml.serivice.impl.CashierImpl">
            <property name="bookShopService" ref="bookShopService"></property>
        </bean>

    2.配置事务管理器bean

    <!-- 配置事务管理器 hibernate、jpa都是类似的这样配 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

    3.引入 tx 命名空间

    xmlns:tx="http://www.springframework.org/schema/tx"

    4.配置事务各个属性(方法名可以使用通配符*)

    <!-- 配置事务属性 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="purchase" propagation="REQUIRED"/>
                <tx:method name="checkout" propagation="REQUIRED"/>
                <tx:method name="get*" read-only="true"/>            
                <tx:method name="find*" read-only="true"/>            
            </tx:attributes>
        </tx:advice>

    5.引入aop命名空间

    xmlns:aop="http://www.springframework.org/schema/aop"

    6.配置事务的切点,并把事务切点和事务属性关联起来

    <!-- 配置事务的切点,并把事务切点和事务属性不关联起来 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.liujl.spring.tx.xml.serivice.*.*(..))" id="txPointCut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
        </aop:config>
     事务的属性:  
        1.使用 propagation 声明事务的传播属性,默认即 REQUIRED 即被包含在上面的事务中,放弃自己处理事务
        REQUIRES_NEW 以本方法为执行单位,开启一个新事务(外部事务在方法执行前后被挂起)
        
        2.使用 isolation 指定事务的隔离级别,最常用的取值为 READ_COMMITTED 读与提交
    
        3.默认情况下Spring 的声明式事务对所有的运行时一样长进行回滚。
        也可以对应的属性指定配置,通常情况下取默认值即可。使用 noRollbackFor 指定不回滚的异常,其他的类似
    
        4.使用 readOnly 指定事务是否为只读,表示这个事务只读取数据但不更新数据,
        这样可以帮助数据库引擎优化事务。若真的是一个只读取数据库值得方法,应设置readOnly=true
        
        5.使用 timeout 指定强制回滚之前事务可以占用的时间
  • 相关阅读:
    win10自带邮箱应用无法查看qq邮箱应用解决办法
    Ubuntu紫色背景颜色代码
    VMware中对Linux虚拟机的网络配置静态IP的配置
    CentOS 7在VMware 12中共享文件看不见的问题?
    C++中让人忽视的左值和右值
    C++ allocator类学习理解
    C++11新特性 -----> 右值引用 &&
    重新认识new
    关于C++中nothrow的某某某
    stopPropagation, preventDefault 和 return false 的区别
  • 原文地址:https://www.cnblogs.com/qq-757617012/p/4295376.html
Copyright © 2020-2023  润新知