EJB JPA交易 [英] EJB JPA transactions

查看:102
本文介绍了EJB JPA交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.1,EJB 3.1,JPA 2.0,Glassfish 3.1.1和NetBeans 7.0.1。



对于每个实体类,我创建了一个单独的Facade类,例如,使用NetBeans工具的UserFacade和AddressFacade。这两个实体彼此无关,完全独立于其他实体。但是,我需要在一个事务中将它们放入数据库,如果没有插入,那么另外一个也应该被回滚。我怎样才能做到这一点?据我所知,EJB容器管理事务本身,不允许手动控制事务边界。

解决方案

事务将只要您在同一个持久性,更新或删除中触摸两个实体,即可回滚所有内容,但是您说这些实体无论如何都不相关,因此我猜测您必须执行2个持久化,将不在同一个交易范围内。



选项1



您可以为此做一些简单而丑陋的事情,那里是JPA中的@PrePersist和@PreUpdate,您可以确保所有内容都与上一个持久化相关。这将造成不良的代码库并强制不必要的依赖。



选项2



您可以简单地在User和Address之间建立关系,这是自然的。



选项3



第三个选项是使用 Bean Managed Transaction


I am using JSF 2.1, EJB 3.1, JPA 2.0, Glassfish 3.1.1 and NetBeans 7.0.1.

For each entity class I created a separate Facade class, for example, UserFacade and AddressFacade using NetBeans tools. These two entities are not related to each other and are completely independent of each of other. However, I need to put them into database during one transaction and if one fails to be inserted then another one should also be rollbacked. How can I do that? As far as I know EJB container manages transactions itself and doesn't allow to manually control the transaction boundaries.

解决方案

The transaction will be rollback everything as long as you "touch" both of your entities in the same persist, update or remove, however, you say that they are not related to each other in anyway and so Im guessing that you have to perform 2 persists and that will not be in the same transaction scope.

Option 1

You could do something easy and ugly for this, there is @PrePersist and @PreUpdate in JPA that you can make sure that everything is with the previous persist. This will make a bad codebase and force unwanted dependencies.

Option 2

You could simply have a relation between User and Address, which is only natural.

Option 3

The third option is to use Bean Managed Transaction

这篇关于EJB JPA交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆