关于spring hibernate的事务管理

期望结果

进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);

.....
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值