1 package org.apache.ojb.ejb.pb; 2 3 17 18 19 import javax.ejb.CreateException ; 20 import javax.ejb.EJBException ; 21 import javax.ejb.SessionBean ; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.ojb.broker.PersistenceBroker; 31 import org.apache.ojb.broker.query.Criteria; 32 import org.apache.ojb.broker.query.Query; 33 import org.apache.ojb.broker.query.QueryByCriteria; 34 import org.apache.ojb.broker.util.logging.Logger; 35 import org.apache.ojb.broker.util.logging.LoggerFactory; 36 import org.apache.ojb.ejb.ArticleVO; 37 import org.apache.ojb.ejb.PersonVO; 38 39 76 public class RollbackBean extends PBBaseBeanImpl implements SessionBean 77 { 78 private Logger log = LoggerFactory.getLogger(RollbackBean.class); 79 80 private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager"; 81 private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager"; 82 83 private ArticleManagerPBLocal am; 84 private PersonManagerPBLocal pm; 85 86 public RollbackBean() 87 { 88 } 89 90 97 public void rollbackOtherBeanUsing(List articles, List persons) 98 { 99 log.info("rollbackOtherBeanUsing method was called"); 100 ArticleManagerPBLocal am = getArticleManager(); 102 PersonManagerPBLocal pm = getPersonManager(); 103 am.storeArticles(articles); 104 pm.storePersons(persons); 105 106 throw new EJBException ("## Testing of rollback behaviour - rollbackOtherBeanUsing ##"); 108 } 109 110 117 public void rollbackOtherBeanUsing_2(ArticleVO article, List persons) 118 { 119 log.info("rollbackOtherBeanUsing_2 method was called"); 120 ArticleManagerPBLocal am = getArticleManager(); 121 PersonManagerPBLocal pm = getPersonManager(); 122 pm.storePersons(persons); 123 am.failureStore(article); 124 } 125 126 132 public void rollbackClientWrongInput(List articles, List persons) 133 { 134 log.info("rollbackClientWrongInput method was called"); 135 ArticleManagerPBLocal am = getArticleManager(); 136 PersonManagerPBLocal pm = getPersonManager(); 137 am.storeArticles(articles); 138 pm.storePersons(persons); 139 } 140 141 146 public void rollbackThrowException(List objects) 147 { 148 log.info("rollbackThrowException method was called"); 149 storeObjects(objects); 150 throw new EJBException ("## Testing of rollback behaviour - rollbackThrowException ##"); 152 } 153 154 159 public void rollbackPassInvalidObject(List objects) 160 { 161 log.info("rollbackPassInvalidObject method was called"); 162 storeObjects(objects); 163 } 164 165 170 public void rollbackSetRollbackOnly(List objects) 171 { 172 log.info("rollbackSetRollbackOnly method was called"); 173 storeObjects(objects); 174 178 getSessionContext().setRollbackOnly(); 179 } 180 181 186 public void rollbackSetRollbackAndThrowException(List objects) 187 { 188 log.info("rollbackSetRollbackAndThrowException method was called"); 189 storeObjects(objects); 190 getSessionContext().setRollbackOnly(); 191 throw new EJBException ("## Testing of rollback behaviour - rollbackSetRollbackAndThrowException ##"); 195 } 196 197 203 public void rollbackBreakIteration(List objectsToStore) 204 { 205 log.info("rollbackBreakIteration"); 207 211 storeObjects(objectsToStore); 212 Class searchClass = objectsToStore.get(0).getClass(); 213 PersistenceBroker broker = getBroker(); 214 try 215 { 216 Query q = new QueryByCriteria(searchClass); 217 Iterator it = broker.getIteratorByQuery(q); 219 it.next(); 220 } 221 225 finally 226 { 227 if(broker != null) broker.close(); 228 } 229 230 throw new EJBException ("## Testing of rollback behaviour - rollbackBreakIteration ##"); 234 } 235 236 239 public List storeObjects(List objects) 240 { 241 return new ArrayList (super.storeObjects(objects)); 242 } 243 244 247 public void deleteObjects(List objects) 248 { 249 log.info("deleteObjects"); 250 super.deleteObjects(objects); 251 } 252 253 256 public int getArticleCount() 257 { 258 log.info("getArticleCount was called"); 259 return getCount(ArticleVO.class); 260 } 261 262 265 public int getPersonCount() 266 { 267 log.info("getPersonCount was called"); 268 return getCount(PersonVO.class); 269 } 270 271 274 public Collection getAllObjects(Class target) 275 { 276 if(log.isDebugEnabled()) log.debug("getAllObjects was called"); 277 return super.getAllObjects(target); 278 } 279 280 private ArticleManagerPBLocal getArticleManager() 281 { 282 if (am == null) 283 { 284 Context context = null; 285 try 286 { 287 context = new InitialContext (); 288 am = ((ArticleManagerPBLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create(); 289 log.info("** Found bean: " + am); 290 return am; 291 } 292 catch (NamingException e) 293 { 294 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e); 295 throw new EJBException (e); 296 } 297 catch (CreateException e) 298 { 299 log.error("Creation of ArticleManager failed", e); 300 throw new EJBException (e); 301 } 302 } 303 return am; 304 } 305 306 private PersonManagerPBLocal getPersonManager() 307 { 308 if (pm == null) 309 { 310 Context context = null; 311 try 312 { 313 context = new InitialContext (); 314 pm = ((PersonManagerPBLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create(); 315 log.info("** Found bean: " + pm); 316 return pm; 317 } 318 catch (NamingException e) 319 { 320 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e); 321 throw new EJBException (e); 322 } 323 catch (CreateException e) 324 { 325 log.error("Creation of PersonManager failed", e); 326 throw new EJBException (e); 327 } 328 } 329 return pm; 330 } 331 } 332 | Popular Tags |