java什么时候使用事物_java – 应用程序管理JPA,何时需要Transaction

我想人们可以总结出你的问题的答案.

几乎任何JPA操作都需要一个事务,除了不锁定实体的查找/选择(即任何不改变数据的JPA操作).

(JTA事务范围实体管理器)

对于JTA事务范围的实体管理器,最好从规范引用(第3章实体操作):

The persist, merge, remove, and refresh methods must be invoked within

a transaction context when an entity manager with a

transaction-scoped persistence context is used.

If there is no transaction context, the javax.persistence.TransactionRequiredException is thrown.

Methods that specify a lock mode other than LockModeType.NONE must be invoked

within a transaction context.

If there is no transaction context, the javax.persistence.TransactionRequiredException is thrown.

The find method (provided it is invoked without a lock or invoked with

LockModeType.NONE) and the getReference method are not required to be

invoked within a transaction context. If an entity manager with

transaction-scoped persistence context is in use, the resulting

entities will be detached; if an entity manager with an extended

persistence context is used, they will be managed. See section 3.3 for

entity manager use outside a transaction.

(应用程序管理/资源本地实体管理器)

对于应用程序管理的实体管理器,JPA规范不清楚该行为.在Hibernate的情况下,当不在事务内部时(它也可能依赖于JDBC驱动程序和DB连接的自动提交模式),发生的事情非常复杂.在这个主题上检查Hibernate’s article.基本上,强烈建议您始终使用事务进行上述操作.

对于问题的第二部分:如果你调用了一个托管实体的setter,并且没有刷新你分离它(即在事务提交之前),行为是不明确/未定义的,即你应该更好地纠正代码.

有缺陷的代码示例:

//begin Transaction

MyEntity entity = em.find(MyEntity.class, 1L);

entity.setField("New value");

em.detach();//it is not sure whether the "New value" will be persisted. To make sure it is persisted, ypu need to call em.flush() before detaching

//commit Transaction

通常,如果DB操作的顺序(与enity manager操作的顺序不同)不重要,您可以让JPA实现决定何时刷新(例如,在事务提交时).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值