Could not obtain transaction-synchronized Session for current thread
2015-10-26 17:01
501 查看
用Junit做单元测试时,报了这个错:Could not obtain transaction-synchronized Session for current thread
原因是没有为组件加入事务控制
解决的方法有两种
1.使用注解的方式
在spring-context.xml文件中加入<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />,
然后在@service组件或者@Repository组件中加入@Transactional注解,spring就会自动为@Transactional注解的类定义事务
2.利用spring的aop面向切面的功能实现事务的传播
在spring-context.xml文件中加入如下配置:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="set*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="query*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="select*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txNeed" expression="<span style="color:#FF0000;">execution(* com.watson.web.service..*.*(..))</span>"/>
<aop:advisor pointcut-ref="txNeed" advice-ref="txAdvice"/>
</aop:config>
红色部分为定义通知织入的横切面,也就是横切点,会扫描service包下面的(包括子包)所有类的所有方法,为扫描到的方法织入事务。
原因是没有为组件加入事务控制
解决的方法有两种
1.使用注解的方式
在spring-context.xml文件中加入<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />,
然后在@service组件或者@Repository组件中加入@Transactional注解,spring就会自动为@Transactional注解的类定义事务
2.利用spring的aop面向切面的功能实现事务的传播
在spring-context.xml文件中加入如下配置:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="set*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="query*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="select*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txNeed" expression="<span style="color:#FF0000;">execution(* com.watson.web.service..*.*(..))</span>"/>
<aop:advisor pointcut-ref="txNeed" advice-ref="txAdvice"/>
</aop:config>
红色部分为定义通知织入的横切面,也就是横切点,会扫描service包下面的(包括子包)所有类的所有方法,为扫描到的方法织入事务。
相关文章推荐
- Windows 2008 R2 AD系列四:Domain User权限运行软件
- 使用行为树(Behavior Tree)实现游戏AI
- 程序设计师不是那么好当的 (真实案例一起动动脑)
- Daily Scrumming* 2015.10.25(Day 6)
- HDU 2476 String painter(区间DP)
- aixs2客户端调axis1服务
- Handler sendMessage 与 obtainMessage (sendToTarget)比较
- Claim 2
- POJ-3735-Training little cats-构造矩阵+矩阵快速幂+稀疏矩阵乘法优化
- RAID的后台初始化 Background Initialization
- Teradata Explain
- Timus Online Judge 1017 Staircases(记忆化搜索)
- ERROR ITMS-90049:"This bundle is invalid.The bundle identifier contains disallowed characters."
- 全局光照技术解析Global Illumination Explained
- TEMP_FAILURE_RETRY,__extension,MSG_NOSIGNAL
- Intellij IDEA无法启动:Failed to create JVM:error code -4
- Onpaint和OnDraw的区别
- aidl远程调用其他应用服务
- LPC43XX JTAG Scan Chain
- pthread_cond_timedwait 实例