Spring中的Jdbc事务管理

柔光的暖阳◎ 2021-12-16 02:09 323阅读 0赞

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

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

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

Spring声明式事务管理器类:

Jdbc技术:DataSourceTransactionManager

Hibernate技术:HibernateTransactionManager

1.xml方式声明事务

  引入tx名称空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd">
  12. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  13. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  14. <property name="jdbcUrl" value="jdbc:mysql:///student"/>
  15. <property name="user" value="root"/>
  16. <property name="password" value="juaner767"/>
  17. <property name="initialPoolSize" value="3"/>
  18. <property name="maxPoolSize" value="6"/>
  19. <property name="maxStatements" value="100"/>
  20. <property name="acquireIncrement" value="2"/>
  21. </bean>
  22. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  23. <constructor-arg ref="dataSource"/>
  24. </bean>
  25. <bean id="studentDao" class="com.juaner.spring.tx.StudentDao">
  26. <property name="jdbcTemplate" ref="jdbcTemplate"/>
  27. </bean>
  28. <bean id="studentService" class="com.juaner.spring.tx.StudentService">
  29. <property name="studentDao" ref="studentDao"/>
  30. </bean>
  31. <!--spring声明式事务管理控制-->
  32. <!--配置事务管理器类-->
  33. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  34. <property name="dataSource" ref="dataSource"/>
  35. </bean>
  36. <!--配置事务增强(如何管理事务,只读、读写...)-->
  37. <tx:advice id="txAdvice" transaction-manager="txManager">
  38. <tx:attributes>
  39. <tx:method name="*save*" read-only="false"/>
  40. <tx:method name="get*" read-only="true"/>
  41. </tx:attributes>
  42. </tx:advice>
  43. <!--aop配置,拦截哪些方法(切入点表达式,拦截上面的事务增强)-->
  44. <aop:config>
  45. <aop:pointcut id="pt" expression="execution(* com.juaner.spring.tx.*.*(..))"/>
  46. <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
  47. </aop:config>
  48. </beans>

2.注解方式声明事务

开启注解扫描

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

使用注解

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

如果@Transactional

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

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

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

3.事务属性

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

其中,事务的传播行为propagation

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

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

转载于:https://www.cnblogs.com/juaner767/p/5596047.html

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring事务管理

    事务的回顾 1、什么是事务? 事务:逻辑上的一组操作,组成这组操作的各个语句,要么全都成功,要么全都失败。 -------------------- 2、事务的特性

    相关 Spring事务管理

    一、什么是事务 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用。例如网上购物:购物者的账户减少相应的数额和商品库存减少相应

    相关 Spring事务管理

    事务的概念 事务代表逻辑上的一组操作要么一起成功, 要么一起失败. 可以想一下转账的例子. 事务的特性 ACID 原子性: 事务是不可分割的. 一致性:

    相关 SpringJdbc事务管理

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