spring事务管理机制
来源:互联网 发布:淘宝鬼脚七微信号 编辑:程序博客网 时间:2024/06/05 03:51
spring的事务管理是一种策略模式;spring仅在更高的层次抽象出一个事务管理的接口PlatFormTransactionManager,根据应用的持久层框架来决定使用那种实现,不同的实现类则代表不同的事务管理策略。
优势:
1、spring采用面向接口编程的方式决定了应用可以灵活的切换事务管理策略。
2、不与具体的事务API耦合提高应用的可扩展性。
3、使用声明式事务时,无需写任何事务相关代码,代码量少,开发效率高。
spring的事务管理分为两种:编程式事务和声明式事务
编程式事务:
需要在程序中手动获取事务管理器PlatFormTransactionManager的实例并通过getTransaction()获取事务,然后通过commit(TransactionStatus status)来提交事务或者通过rollback(TransactionStatus status)回滚事务。
声明式事务:
声明式事务是基于AOP实现的,在业务方法开始织入获取事务代码,业务方法执行完毕后织入提交或回滚事务代码。
只需要配置事务管理器PlatFormTransactionManager,配置事务方法,开启aop代理即可,需要添加事务的方法按照配置命名即可。
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 组件扫描器 --><context:component-scan base-package="com.zhjg.tx.*.impl" /><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver" /><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_tx" /><property name="user" value="root" /><property name="password" value="root" /></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="dataSource" /></property></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 方法一:这种方式配置简单,但需要在方法上添加注解,当事务方法太多时不方便 --><!-- 根据Transactional注解自动生成事务代理 --><!-- <tx:annotation-driven transaction-manager="transactionManager"/> --><!-- 推荐使用 --><!-- 配置事务增强处理bean并指定事务管理器 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /><tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" /><tx:method name="mod*" propagation="REQUIRED" rollback-for="Exception" /><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><!-- 配置事务代理确保事务增强处理能在合适的点被织入 --><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* com.zhjg.tx.dao.impl.*.*(..))" /></aop:config></beans>
测试demo
0 0
- Spring的事务管理机制
- Spring 事务管理机制
- Spring的事务管理机制
- spring 事务管理机制
- Spring的事务管理机制
- spring事务管理机制
- spring---事务管理机制
- spring事务管理机制
- Spring 事务管理机制概述
- Spring 事务管理机制概述
- Spring 事务管理机制概述
- Spring的事务管理机制
- Spring 事务管理机制概述
- Spring的事务管理机制
- Spring 的Hibernate事务管理机制
- Spring 的Hibernate事务管理机制 .
- Spring 的Hibernate事务管理机制
- Spring的事务管理机制和传播机制
- *[Lintcode]带环链表 II
- Struts2 常用的常量配置
- linux常用命令集
- java中的泛型详解
- 老生常谈之UITableView的性能优化
- spring事务管理机制
- html 盒子模型基础(文档流,浮动,页面布局)(五)
- Spring基础知识汇总
- 设置session失效的几种方法
- 微信支付
- JDK源码阅读——LinkedList
- thinkphp核心源码注释|Log.class.php
- app上架后搜不到解决办法
- mysql order by 中文排序 错乱问题