java – 应用程序管理JPA,何时需要Transaction
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 应用程序管理JPA,何时需要Transaction,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2222字,纯文字阅读大概需要4分钟。
内容图文
我们正在开发一个小网站(将在Tomcat上运行),数据层使用JPA(Eclipselink)完成.
我前段时间做过类似的事情.但我总是不确定何时需要开始和结束交易或进行同花.
目前我使用事务,如果我添加(持久)和删除对象.如果我在已经持久化的对象上调用setter,则不使用事务.
是否有指南/教程或简短的答案何时使用事务或如何正确实现应用程序管理的JPA.
解决方法:
我想人们可以总结出你的问题的答案.
几乎任何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实现决定何时刷新(例如,在事务提交时).
内容总结
以上是互联网集市为您收集整理的java – 应用程序管理JPA,何时需要Transaction全部内容,希望文章能够帮你解决java – 应用程序管理JPA,何时需要Transaction所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。