您的位置:首页 > 编程语言 > Java开发

关于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);

.....

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: