zoukankan      html  css  js  c++  java
  • Spring中的Jdbc事务管理

      Spring提供了对事务的声明式事务管理,只需要在配置文件中做一些配置,即可把操作纳入到事务管理当中,解除了和代码的耦合。

      Spring声明式事务管理,核心实现就是基于Aop。

      Spring声明式事务管理是粗粒度的事务控制,只能给整个方法应用事务,不可以对方法的某几行应用事务。

         Spring声明式事务管理器类:

                   Jdbc技术:DataSourceTransactionManager

                   Hibernate技术:HibernateTransactionManager

    1.xml方式声明事务

      引入tx名称空间

    <?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: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/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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///student"/>
            <property name="user" value="root"/>
            <property name="password" value="juaner767"/>
            <property name="initialPoolSize" value="3"/>
            <property name="maxPoolSize" value="6"/>
            <property name="maxStatements" value="100"/>
            <property name="acquireIncrement" value="2"/>
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg ref="dataSource"/>
        </bean>
        <bean id="studentDao" class="com.juaner.spring.tx.StudentDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
        </bean>
        <bean id="studentService" class="com.juaner.spring.tx.StudentService">
            <property name="studentDao" ref="studentDao"/>
        </bean>
        <!--spring声明式事务管理控制-->
        <!--配置事务管理器类-->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--配置事务增强(如何管理事务,只读、读写...)-->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*save*" read-only="false"/>
                <tx:method name="get*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        <!--aop配置,拦截哪些方法(切入点表达式,拦截上面的事务增强)-->
        <aop:config>
            <aop:pointcut id="pt" expression="execution(* com.juaner.spring.tx.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
        </aop:config>
    </beans>

    2.注解方式声明事务

    开启注解扫描

        <!--事务管理器类-->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--开启注解扫描-->
        <context:component-scan base-package="com.juaner.spring.tx"/>
    
        <!--注解方式实现事务-->
        <tx:annotation-driven transaction-manager="txManager"/>

    使用注解

        @Transactional
        public void save(Student student){
            this.studentDao.save(student);
            int i = 1/0;
            this.studentDao.save(student);
        }

    如果@Transactional

    应用到方法上,该方法使用事务;

    应用到类上,类的方法使用事务,

    定义到父类上,执行父类的方法时使用事务;

    3.事务属性

        @Transactional(
                readOnly = false, //读写事务
                timeout = -1 ,     //事务的超时时间,-1为无限制
                noRollbackFor = ArithmeticException.class, //遇到指定的异常不回滚
                isolation = Isolation.DEFAULT, //事务的隔离级别,此处使用后端数据库的默认隔离级别
                propagation = Propagation.REQUIRED //事务的传播行为
        )

    其中,事务的传播行为propagation

    Propagation.REQUIRED,业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到该事务,否则为自己创建一个新的事务。

    Propagation.REQUIRES_NEW,不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法已经运行在一个事务中,则原有事务会被挂起,新的事务会被创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

  • 相关阅读:
    问题:oracle if;结果:Oracle IF语句的使用
    问题:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
    问题:oracle decode;结果:oracle中的decode的使用
    问题:只能在执行 Render() 的过程中调用 RegisterForEventValidation;结果:只能在执行 Render() 的过程中调用 RegisterForEventValidation
    问题:oracle long 与 clob;结果:long类型比clob到底差在什么地方?
    问题:oracle 字符串转换成日期;结果:[oracle] to_date() 与 to_char() 日期和字符串转换
    问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别
    问题:C#根据生日计算属相;结果:C#实现根据年份计算生肖属相的方法
    po dto vo bo
    eclipse中自动加载源码的方法
  • 原文地址:https://www.cnblogs.com/juaner767/p/5596047.html
Copyright © 2011-2022 走看看