Spring _事务
来源:互联网 发布:淘宝上电脑主机 编辑:程序博客网 时间:2024/05/16 08:15
事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制.而且主要变化的是代理机制。
第一种,使用tx标签方式
第二种,使用代理方式:
<!-- 第二种配置事务的方式 ,代理--><bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="del*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property></bean><bean id="userDao" parent="transactionProxy"> <property name="target"> <!-- 用bean代替ref的方式--> <bean class="com.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </property></bean>
第三种,使用拦截器:
<!-- 第三种配置事务的方式,拦截器 (不常用)--><bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="del*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property></bean><bean id="proxyFactory" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> <property name="beanNames"> <list> <value>*Dao</value> </list> </property></bean>事务传播属性:
PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
编程式事务
编程式即采用注解的方式,需要注意的是,使用注解的方式需要在Spring的配置文件中加入一句话:<context:annotation-config />,其作用是开启注解的方式。具体配置如下:
<!--开启注解方式--><context:annotation-config /><!-- 配置sessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation"> <value>classpath:config/hibernate.cfg.xml</value> </property> <property name="packagesToScan"> <list> <value>com.entity</value> </list> </property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 第四种配置事务的方式,注解 --><tx:annotation-driven transaction-manager="transactionManager"/>
0 0
- Spring _事务
- Spring 事务传递教程_有实例
- 配置Spring数据库事务_备忘
- Spring与Hibernate集成_声明式事务
- (笔记)Spring实战_事务管理(3)_在Spring中的编码事务
- (笔记)Spring实战_事务管理(1)_理解事务
- (笔记)Spring实战_事务管理(2)_选择事务管理器
- (笔记)Spring实战_事务管理(4)_声明式事务
- 事务:spring事务支持
- 事务:spring事务支持
- spring 事务,事务特性
- Spring事务
- Spring 事务
- Spring事务
- spring事务
- Spring事务
- spring 事务
- spring 事务
- 公式达人必杀技,你会几招?
- 如何设计一个优秀的数据库
- 判别式模型与生成式模型
- 面试算法——宝石比重量
- hdu4975(最大流判环)
- Spring _事务
- (修改)IplImage, CvMat,cv::Mat之间的转换
- Tomcat工程发布到linux系统上
- JavaScript中的setMonth()顺延问题及解决方法
- 实现数组作为函数参数传递,切返回值也是数组
- packstack --allinone无法成功启动的问题
- 如何设计数据库
- 第六周项目26-成员函数,友元函数与一般函数的区别
- 对图像高频信号和低频信号的理解