spring事务_注解
2016-07-21 14:25
429 查看
在spring配置文件中引入<tx:>命名空间:
REQUIRED:业务方法需要在一个容器里运行。如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="true"><!-- 开启基于注解的配置 -->
<context:annotation-config/><!-- 基于注解的组件扫描 -->
<context:component-scan base-package="com.surekam"/><!-- 基于注解的事务支持 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/><!-- 针对一个单独的Hibernate SessionFactory的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <!--<property name="defaultTimeout"/>--> <!--<property name="rollbackOnCommitFailure"/>--> </bean><!-- 事务、安全-拦截基础类 -->
<bean id="baseProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager" /> <!--property name="proxyTargetClass" value="true"/--> <property name="transactionAttributeSource" ref="namingTransactionAttribute" /> </bean><!-- 定义拦截方法名称 -->
<bean id="namingTransactionAttribute" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> <property name="properties"> <props> <prop key="add*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop> <prop key="create*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop> <prop key="new*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop> <prop key="deploy*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop> <prop key="insert*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop> </props> </property> </bean>service里方法:
REQUIRED:业务方法需要在一个容器里运行。如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务。
@Transactional(propagation=Propagation.REQUIRED) public List findTsDf(){ String hql = "from TuoshouDianfei d where 1=1"; return dao.find(hql); }参考:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- redis的导入导出需要特别注意的地方
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 空间两大拓展功能 引爆企业发展需求
- 空间支持多子网站 有效降低建站成本
- 奔月互联免费50m/5g/asp/ftp/可绑米空间
- 三维免费空间提供多款免费ASP空间(1G)
- 使用C#实现读取系统配置文件的代码实例讲解
- 空间页面CSS说明
- Powershell Profiles配置文件的存放位置介绍
- PowerShell入门教程之创建和使用配置文件实例
- SERV-U 的配置文件详解
- 最省空间的计数器