当前位置:Gxlcms > 数据库问题 > Spring中的Jdbc事务管理

Spring中的Jdbc事务管理

时间:2021-07-01 10:21:17 帮助过:4人阅读

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

 

Spring中的Jdbc事务管理

标签:

人气教程排行