Spring与Hibernate整合(二)
来源:互联网 发布:北京师范大学 知乎 编辑:程序博客网 时间:2024/06/15 04:51
2.事务的四种配置方式
由于事务管理器只需要引用一下sessionFactory,然后供事务调用,所以它就两句话:
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 这个name的sessionFactory是实际存在的不可以更改,ref的sessionFactory是与上面的sessionFactory的id对应,可以更改 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
1)使用tx标签声明事务
<!-- 对事务的描述:tx标签 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <!-- propagation是值事务隔离级别,rollback-for是指定异常回滚,no-rollback-for是指定异常不回滚 --> <tx:method name="add*" propagation="REQUIRED"rollback-for="Exception"/> <tx:method name="modify*" propagation="REQUIRED"no-rollback-for="RuntimeException"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 指定哪些方法使用事务,并引入事务描述 --> <aop:config> <aop:pointcut expression="execution(* com.dao.*.*(..))" id="daoMethod"></aop:pointcut> <aop:advisor advice-ref="txadvice" pointcut-ref="daoMethod"/></aop:config>
2)使用代理声明事务
<!-- 第二种配置事务的方式,代理 --> <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,+RuntimeException</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="userDao" parent="transactionProxy"> <property name="target"ref="userDao"></property></bean>
3)使用拦截器声明事务
<!-- 第三种配置事务的方式:拦截器 --> <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</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>
4)使用注解声明事务
<!-- 第四种配置事务的方法:注解 --><!--启用注解 --><context:annotation-config/><tx:annotation-driven transaction-manager="transactionManager"/>
该方法对Dao的实现有要求,必须使用注解而不是映射文件。
- Spring与Hibernate整合(二)
- SSH整合(二)-Spring整合Hibernate
- ssh整合二(spring整合hibernate)
- 【Spring】Spring与Hibernate整合(十六)
- SSH整合学习笔记之spring与hibernate整合(二)--C3P0
- Spring+hibernate+springmvc 整合(二)
- Struts1+Hibernate+Spring整合(二)
- SpringMVC+Hibernate+Spring整合实例(二)
- SpringMVC+Hibernate+Spring整合实例(二)
- SpringMVC+Hibernate+Spring整合实例(二)
- SpringMVC+Hibernate+Spring整合实例(二)
- SpringMVC+Hibernate+Spring整合实例(二)
- spring与hibernate整合
- Spring与Hibernate整合
- hibernate 与spring 整合
- Spring与Hibernate整合
- spring与hibernate整合
- Spring 与Hibernate 整合
- 产品设计5个减少
- My top ten list for a successful test automation strategy
- SGU 107 987654321 problem
- 决心入门者:S3C2410bootloader 之2410Init.s详解二
- android系统PS2全键盘驱动(中)-使用input_event实现
- Spring与Hibernate整合(二)
- Solaris系统/proc/目录简介
- Struts使用convention-plugin实现零配置
- 从ramdisk根文件系统启动Linux成功,及使用initramfs启动linux
- 如何使用Lex和Yacc工具(2)---Parser Generator+VC6.0配置
- C语言I/O学习笔记(1)stdin,stdout和stderr以及重定向
- 删除文件夹
- GCRetractableSectionController
- 如何把Win7在C盘的用户文件夹转移到其他分区