• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB Transaction inside loop

 
Raj CK
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls go thru the following code

StateLessSB1:sb1(List records) {
  for (i=0;i<records.size();i++){
   try {
    SomeObject someObject = (SomeObject)records(i);
     StateLessSB2:sb2Persist(someObject)
   } catch (Exception ex) {
     ctx.setRollbackOnly();
     throw ex;
   }
  }
}


StateLessSB2:sb2Persist(SomeObject someObject) throws SomeDaoSysException {
   try {
    SomeDao someDao = SomeDaoFactory.getDao();
     someDao.insert(someObject);
   } catch (SomeDaoSysException ex) {
    ctx.setRollbackOnly();
    throw ex;
   }
}

--------------------------------
Transaction attributes
StateLessSB1:sb1(List records) -requires new
StateLessSB2:sb2Persist(SomeObject obj) -required
--------------------------------


Questions:
1. I think all the records committed at once when StateLessSB1:sb1(List records) completes successfully. is it true?

2. Please suggest on better ways of handling this scenario.


Thanks
[ June 09, 2005: Message edited by: Raj CK ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35719
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj CK:
1. I think all the records committed at once when StateLessSB1:sb1(List records) completes successfully. is it true?

2. Please suggest on better ways of handling this scenario.

Raj,
1) That is correct.
2) It depends on what you want to do. If you want each transaction to commit separately (and therefore not be able to rollback the whole transaction), you can use "requires new" on the second session bean. If you want to be able to rollback the whole transaction, you need to postpone the commit as it currently is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!