假设我让我的客户使用 Stateful Session Bean 预订飞机上的座位。如果客户端显式调用我的Remove方法,那么他的所有保留都将被取消,并且该bean随后将被删除。
但是,如果客户端空闲一段时间并且 Bean 被钝化,如果 Bean 在钝化时超时,它将被删除,而不会调用我的任何函数。因此,如果有人能告诉我如何确保在 bean 被删除时预订会被取消,我将非常感激。如果我使用@PreDestroy注解,可以解决这个问题吗?
最诚挚的问候, 詹姆斯·特兰
最佳答案
@PreDestroy
方法很可能不会被调用。 EJB 3.1 规范明确指出:
4.6.3 Missed PreDestroy Calls
The Bean Provider cannot assume that the container will always invoke the PreDestroy lifecycle callback interceptor method(s) (or ejbRemove method) for a session bean instance. The following scenarios result in the PreDestroy lifecycle callback interceptor method(s) not being called for an instance:
• A crash of the EJB container.
• A system exception thrown from the instance’s method to the container.
• A timeout of client inactivity while the instance is in the passive state. The timeout is specified by the Deployer in an EJB container implementation-specific way.
该规范还详细说明了如果在这种情况下未调用 @PreDestroy
方法,如何删除资源:
For example, if a shopping cart component is implemented as a session bean, and the session bean stores the shopping cart content in a database, the application should provide a program that runs periodically and removes “abandoned” shopping carts from the database.
就您而言,这取决于您存储预订状态的方式。如果它们保留在数据库中,那么我建议采用与规范中规定的相同的方法。您可以使用 EJB 计时器服务定期执行此 Activity ,或者使用 Quartz 等调度程序。请注意,必须区分不再存在的钝化 session bean 实例的内容和将再次准备好的 session bean 实例的内容。
关于java - EJB3 如何确保在删除 bean 之前发生事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6730466/