Spring提供的Hibernate申明式事务管理有两种办法
a) 配合使用org.springframework.transaction.interceptor.TransactionInterceptor和org.springframework.orm.hibernate.HibernateTransactionManager,下面是spring reference的例子
代码: |
<beans> ... <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean> <bean id="myTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="myTransactionManager"/> </property> <property name="transactionAttributeSource"> <value> product.ProductService.increasePrice*=PROPAGATION_REQUIRED product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY </value> </property> </bean> <bean id="myProductServiceTarget" class="product.ProductServiceImpl"> <property name="productDao"> <ref bean="myProductDao"/> </property> </bean> <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>product.ProductService</value> </property> <property name="target"> <ref local="myProductServiceTarget<"/> </property> <property name="interceptorNames"> <list> <value>myTransactionInterceptor</value> </list> </property> </bean> </beans> |
HibernateInterceptor和事务无关,它的用途在javadocs中描述如下:
引用: |
This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already was a pre-bound Session (e.g. from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply takes part in it. |
b)使用TransactionProxyFactoryBean,下面是Spring Reference中的例子
代码: |
<beans> ... <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean> <bean id="myProductServiceTarget" class="product.ProductServiceImpl"> <property name="productDao"> <ref bean="myProductDao"/> </property> </bean> <bean id="myProductService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="myTransactionManager"/> </property> <property name="target"> <ref bean="myProductServiceTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="increasePrice*">PROPAGATION_REQUIRED</prop> <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop> </props> </property> </bean> </beans> |
在没有其他AOP interceptor情况下,使用TransactionProxyFactoryBean是比较方便的。
事务划分一般是的业务层,而不是在DAO一层。
2.代理工厂返回的是接口AddressDao的应用,通过接口最终调用target的方法。
3.TransactionDefinition定义了所有的事务属性
分享到:
相关推荐
spring3,hibernate4 配置声明式事务管理(annotation方式)
spring3hibernate4注解式事务管理
Spring与Hibernate集成声明式事务.ppt
本资源主要是结合博文 Spring+Hibernate: 声明式事务 http://blog.csdn.net/ManagementAndJava/article/details/78050519 的源代码,主要是讲解了spring4.3和hibernate5.2.11集成实现原理;
两个项目,一个项目是基于spring jdbc实现的分布式事务,一个是基于spring hibernate的分布式事务,hibernate项目里的applicationContext2.xml是基于mysql和mssql, applicationContext3.xml基于两个mssql, ...
NULL 博文链接:https://quicker.iteye.com/blog/674029
spring整合hibernate实现事务处理 1.spring整合hibernate实现事务处理-注解方式 2.spring整合hibernate实现事务处理-XML方式
Spring+Hibernate 使用TransactionInterceptor声明式事务配置
声明式事务控制,spring2.5+hibernate3集成源码,附带有数据库脚本,可以直接导入到项目的工作空间。欢迎下载
Spring+Hibernate注解声明式事务实例
spring mvc hibernate整合,采用MyEclipse自动生成包和配置文件,jar包可能有多余,但是不影响整体效果。服务器tomcat。数据库mysql。
Spring将事务管理分成了两类: * 编程式事务管理 * 手动编写代码进行事务管理.(很少使用) * 声明式事务管理: * 基于TransactionProxyFactoryBean的方式.(很少使用) * 需要为每个进行事务管理的类,配置一个...
spring1.2申明式事务.txtspring1.2申明式事务.txtspring1.2申明式事务.txtspring1.2申明式事务.txtspring1.2申明式事务.txt
一个简单的spring+hibernate+mysql 事务实例
spring-hibernate.jar
Spring2.5+hibernate搞定分布式事务Spring2.5+hibernate搞定分Spring2.5+hibernate搞定分布式事务布式事务 第三部分
HibernateSHibernateSpring数据库的事务HibernateSpring数据库的事务HibernateSpring数据库的事务pring数据库的事务
spring3、 hibernate4 配置声明式事务管理(annotation方式)
spring与hibernate的整合 spring事务的配置