I have poller scheduled like this

@Schedule(minute = "*/5", hour = "*", persistent = false)
public void pollTimer() {

    startfirstPoller();

    startsecondPoller();

}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void startfirstPoller() {
// find all booking from database and update
bokingFacade.findAll();
    bokingFacade.update(booking);
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void startsecondPoller() {
// find all booking updated from database and update
bokingFacade.findAll();
    bokingFacade.update(booking);
}

The first method update some bookings, save them to database and second method use the updated information to process further and again update the database. The problem is changes doesn't reflect in the database until and unless second executes successfully. Moreover, exception in second method rollback the successful changes made by first method. Please let me know what is happening and how to make two method independent of each other.

有帮助吗?

解决方案

I would assume that because you don't actually define the start and end of the transaction that it is managed by the container and spans the entire execution.

You can manage the transactions using annotations in java EE. Look up JTA which is the API for transactions.

其他提示

The main problem with the code is that the @TransactionAttribute annotation has no effect at all. In your pollTimer() method you're doing a simple local method invocation, therefore the EJB container doesn't know that you want a new transaction for each of your poller methods.

If you inject an EJB with @EJB, the container injects only a proxy, therefore it can intercept the EJB method invocations and do the necessary work, like tx management. However, there is no interception in your case, because you're doing a local method invocation.

The following things happen in your case:

  • At time-out the pollTimer() method starts running in a new tx managed by the container
  • The first and the second poller method get invoked in the exact same tx.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top