简体   繁体   English

SFSB中的容器管理的事务边界

[英]Container managed transaction boundary in SFSB

I have read in the book that: 我在书中读到:

Typically, the container begins a transaction immediately before an enterprise bean method starts. 通常,容器在企业bean方法启动之前立即开始事务。 It commits the transaction just before the method exits. 它在方法退出之前提交事务。

Suppose my stateful EJB has exposed 3 business methods and the client calls these 3 methods sequentially and want wants to run these 3 methods within a single transaction. 假设我的有状态EJB公开了3个业务方法,并且客户端依次调用了这3个方法,并且希望在单个事务中运行这3个方法。 These methods would be called across multiple requests. 这些方法将在多个请求中调用。

Now the spec says that the transaction is committed just before the method exits. 现在规范说明事务在方法退出之前就已提交。 How will I be able to rollback the whole transaction, if my 1st method is successful and 2nd (or 3rd) method fails? 如果第一种方法成功而第二种(或第三种)方法失败,我将如何回滚整个事务?

Please note that I don't want to use create my own transaction boundary. 请注意,我不想使用创建自己的交易边界。

Any help or the pointer in the right direction would be highly appreciated. 任何帮助或朝着正确方向的指针将不胜感激。

Create a method in the bean that calls all the other 3 methods. 在Bean中创建一个调用所有其他3种方法的方法。 Then they'll be in the same transaction. 然后他们将在同一笔交易中。

You are using a stateful session bean to act as a buffer. 您正在使用有状态会话Bean充当缓冲区。 And you want a transaction around multiple calls. 您想要围绕多个呼叫进行交易。

From a design perspective, it's OK, if the transaction is started and committed/rollbacked from a single request (from within a single HttpServletRequest for example). 从设计的角度来看,如果从单个请求(例如在单个HttpServletRequest中)启动和提交/回滚事务,则可以。 In this case you can use a UserTransaction to span the transaction over multiple calls to an EJB. 在这种情况下,您可以使用UserTransaction将事务扩展到对EJB的多次调用。 But a UserTransaction is bound to the current thread, so it might be difficult to pass it to the next request. 但是UserTransaction绑定到当前线程,因此可能很难将其传递给下一个请求。

If you start and commit from different requests, you lose control over the duration of the transaction: Now a client controls the transaction. 如果您从不同的请求开始并提交,则您将失去对事务持续时间的控制:现在,客户端将控制事务。 If that client crashes, the transaction won't be rolled back until the transaction timeout is reached. 如果该客户端崩溃,则在达到事务超时之前,事务不会回滚。 So the recommendation in this case is to buffer in a HttpSession for example. 因此,在这种情况下,建议例如在HttpSession进行缓冲。 If all data has been collected, call a (stateless) EJB to persist it. 如果已经收集了所有数据,请调用(无状态)EJB以将其持久化。

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

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