简体   繁体   English

我什么时候应该使用EJB事务属性'Mandatory'和'Never'

[英]When should I use EJB transaction attributes 'Mandatory' and 'Never'

I am referring to container managed transaction attributes in Enterprise Java Beans. 我指的是Enterprise Java Bean中的容器管理事务属性。 I can't think of any use cases where using 'Mandatory' and 'Never' makes sense. 我想不出任何使用“强制”和“从不”有意义的用例。 Can anyone please explain what cases warrant using these transaction attributes? 任何人都可以解释使用这些交易属性的情况吗?

I worked with a project recently that insisted that all transactions were marked out at the application layer (the service classes), and that Data Access Objects must not be called directly. 我最近使用了一个项目,该项目坚持所有事务都在应用程序层(服务类)中标记出来,并且不能直接调用数据访问对象。

This was to ensure that the database wasn't ever written to without the associated application logic being invoked (usually, the database operation was paired up with a message sent to queue). 这是为了确保在没有调用关联的应用程序逻辑的情况下不会写入数据库(通常,数据库操作与发送到队列的消息配对)。

An application of MANDATORY on every DAO EJB ensured that whilst they would run in a transaction, it was the job of a caller to start one. MANDATORY在每个DAO EJB上的应用确保了它们在事务中运行时,调用者的工作就是启动它。 Since the DAOs were called by the service classes (which had the default of REQUIRED), this meant that they worked just fine but anybody accidentally calling them directly would be rewarded with an exception. 由于DAO是由服务类(默认为REQUIRED)调用的,这意味着它们工作得很好,但任何人不小心直接调用它们都会获得异常奖励。

Here's my stab on this: 这是我对此的抨击:

Mandatory : An EJB may be providing some internal function that assumes/relies on a caller's transaction already running, and if it is not, for various reasons, cannot initiate one and so it will throw an EJB error. 必需 :EJB可能提供一些内部函数,它假定/依赖于已经运行的调用者的事务 ,如果不是,由于各种原因,不能启动它,因此它将抛出EJB错误。 So the real question here is why would that ever be a requirement and the only scenario I can devise would be one where there may be specific transaction related actions that must be executed when a transaction starts and some EJBs are not equipped for these actions and so are marked mandatory. 所以真正的问题是为什么这将成为一个要求,我可以设计的唯一场景是可能存在特定的事务相关操作,当事务开始时必须执行这些操作,而某些EJB没有为这些操作配备,所以标记为强制性。 I suppose you might also use this attribute to ensure a consistent and correctly ordered lock acquisition where a failure to do so could result in a deadlock. 我想您也可以使用此属性来确保一致且正确排序的锁定获取,否则可能会导致死锁。

Never : This forces your EJB to throw an exception if a transaction is running when the EJB is invoked, and again, the real question is what sort of scenario would require this. 从不 :如果在调用EJB时​​事务正在运行,这会强制EJB抛出异常,而真正的问题是什么样的场景需要这样做。 Referring to Mastering EJB Third Edition , Ed Roman asserts that this attribute is useful in reducing client side coding errors by preventing the incorrect assumption that the EJB will participate in an ACID procedure. 参考Mastering EJB Third Edition ,Ed Roman声称这个属性可以通过防止EJB参与ACID过程的错误假设来减少客户端编码错误。

Perhaps others will be able to supply more concrete scenarios for these attributes. 也许其他人将能够为这些属性提供更具体的场景。

Mandatory and Never are used in the cases when you want to explicitly be informed of the fact that the client is not running inside a transaction scope, by throwing an exception. 当您希望通过抛出异常明确告知客户端未在事务范围内运行这一事实时,将使用强制从不

Mandatory throws TransactionRequiredException whilst Never throws RemoteException . 强制抛出TransactionRequiredExceptionNever不会抛出RemoteException

For example if client runs inside/outside a transaction and invokes an enterprise bean's method, that will also run inside/outside a transaction, then no exception will be thrown for SUPPORTS . 例如,如果客户端事务内部/外部运行并调用企业bean的方法,该方法也将事务内部/外部运行,那么SUPPORTS将不会抛出任何异常。 However if you used MANDATORY , for the outside case, an TransactionRequiredException will be thrown. 但是,如果您使用MANDATORY ,则对于外部情况,将抛出TransactionRequiredException。

i have a case where the mandatory attribute is usefull: 我有一个强制属性有用的情况:

i have a bean, with is called by many applications. 我有一个bean,被许多应用程序调用。 depending on the calling application, i want the possibility to join the calling transaction, or run in a new transaction. 根据调用应用程序,我希望有可能加入调用事务,或在新事务中运行。 so i have 2 beans defined, one with RequiresNew and one with Required. 所以我定义了2个bean,一个使用RequiresNew,另一个使用Required。 The second one can use the Mandatory, to ensure there is an calling transaction, but it is not nessesery. 第二个可以使用强制,以确保有一个呼叫交易,但它不是nessesery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM