关于spring hibernate的事务管理
2014-09-30 21:16
447 查看
期望结果
进service包的类事务开始,出service事务结束,如果在进行中抛出任何BusinessException,所有修改的数据回滚
<bean id="transactionManager"
class="${hibernate.transactionManager}">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="ling2.jdbcTransactionManager" /> -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ling2..*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ling2..*.context..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.deloitte..*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.deloitte..*.context..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* velox.service..*.*(..))"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="recycle*" propagation="REQUIRED" rollback-for="BusinessException" />
........
<!-- workflow end -->
<tx:method name="init*" read-only="true" />
<tx:method name="current*" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
但有些情况却不会回滚,测试如下,有个字段20长度,list中数据后面几条长度超过会报错
测试代码在service层用包裹
try {
} catch (Exception e) {
throw new BusinessException("保存失败:"+e.getMessage());
}
直接在一个service中调用另外的service,如果单体保存数据会被立即提交,不能回滚,调试看过Session的id,Session是一个id
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
shoppingService.save(detail);
.....
}
如果批量保存,数据能回滚
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
allDetails.add(detail);
.....
}
shoppingService.saveAll(allDetails);
以上情况,有人说一个insert就是一个事物,一个失败了就回滚一个
如果用dao层单体保存数据,数据居然不能保存,效果和read-only的事务一样,为什么,暂时不知道
这个很奇怪,我其他模块的数据保存都是这样的,也能保存数据,这里就不行,奇怪
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
shoppingDao.save(detail);
.....
}
进service包的类事务开始,出service事务结束,如果在进行中抛出任何BusinessException,所有修改的数据回滚
<bean id="transactionManager"
class="${hibernate.transactionManager}">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="ling2.jdbcTransactionManager" /> -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ling2..*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ling2..*.context..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.deloitte..*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.deloitte..*.context..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* velox.service..*.*(..))"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="recycle*" propagation="REQUIRED" rollback-for="BusinessException" />
........
<!-- workflow end -->
<tx:method name="init*" read-only="true" />
<tx:method name="current*" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
但有些情况却不会回滚,测试如下,有个字段20长度,list中数据后面几条长度超过会报错
测试代码在service层用包裹
try {
} catch (Exception e) {
throw new BusinessException("保存失败:"+e.getMessage());
}
直接在一个service中调用另外的service,如果单体保存数据会被立即提交,不能回滚,调试看过Session的id,Session是一个id
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
shoppingService.save(detail);
.....
}
如果批量保存,数据能回滚
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
allDetails.add(detail);
.....
}
shoppingService.saveAll(allDetails);
以上情况,有人说一个insert就是一个事物,一个失败了就回滚一个
如果用dao层单体保存数据,数据居然不能保存,效果和read-only的事务一样,为什么,暂时不知道
这个很奇怪,我其他模块的数据保存都是这样的,也能保存数据,这里就不行,奇怪
for(RowAndProperty rowAndProperty:rowAndProperties)
{
.......
shoppingDao.save(detail);
.....
}
相关文章推荐
- 一些关于hibernate 与 spring 事务管理的问题
- 关于spring 与Hibernate 结合使用的事务管理
- 关于spring声明式事务管理异常处理的测试和小结
- spring整合hibernate关于session的管理
- spring对hibernate提供的事务管理的一个实用的例子
- Spring声明式事务管理、Spring整合Hibernate
- Spring 中基础配置和事务管理的配置和spring托管hibernate配置文件的方法
- spring整合hibernate关于session的管理
- Spring管理Hibernate事务
- Spring学习总结9(基于Hibernate的事务管理)
- Spring管理Hibernate中的事务(2)
- spring hibernate 事务管理
- spring整合hibernate关于session的管理
- Spring Hibernate 事务管理
- spring管理hibernate事务
- spring+hibernate中的事务管理
- Spring中事务管理与Hibernate自带事务管理冲突的一个问题
- 用spring管理hibernate事务时,lzay="true"不能用的解决方法
- Spring管理 hibernate 事务配置的五种方式
- Spring和hibernate多个数据源的事务管理