0

I'm using an EJB with @scheduled annotation to loop over all my context entities every 10 secs. All Contexts entities are unrelated to each other and therefore should create new transactions for their update method:

@Stateless
public class UpdateService {

    @EJB
    ContextDao contextDao;

    @Schedule(second = "*/10", minute = "*", hour = "*")
    public void update() {
        for(ContextEntity context : contextDao.findAllContexts()) {
            updateContext(context);
        }
    }

    public void updateContext(ContextEntity context) {
         // load data from db
         // update some stuff
         // save back to db
    }
}

Now I wanna have an single transactions for the updateContext method. So if I get any error in one ContextEntity, only this transaction should be rolled back and not the whole loop.

  1. What are the correct TransactionAttributes for update and updateContext?
  2. Do I have to use a different EJB for the updateContext method?

Thanks

kaiser
  • 940
  • 1
  • 10
  • 25
  • This is a good and helpful description, but I would not have found it. I would have looked for *nested* or *embedded* calling *same* EJB or something. Perhaps it could be renamed – aschoerk Sep 17 '17 at 16:59

1 Answers1

0

I presume that you want to call updateContext not update inside the loop.

The update-method as defined has the attribute TransactionAttributeType.REQUIRED which is default and can be left like way.

The updateContext-method should be annotated TransactionAttributeType.REQUIRES_NEW, but... if you want to call in that transaction-context, you must use

@Resource
SessionContext sessionContext;

and fetch the businessinterface from sessionContext to call updateContext

As example look at SingletonEjb see Method methodCallUsingSessionContext

aschoerk
  • 3,333
  • 2
  • 15
  • 29