1 package org.apache.ojb.ejb.odmg; 2 3 17 18 19 import javax.ejb.EJBException ; 20 import javax.ejb.SessionBean ; 21 import javax.ejb.CreateException ; 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.OJBRuntimeException; 31 import org.apache.ojb.broker.PersistenceBroker; 32 import org.apache.ojb.broker.query.Criteria; 33 import org.apache.ojb.broker.query.Query; 34 import org.apache.ojb.broker.query.QueryByCriteria; 35 import org.apache.ojb.broker.util.logging.Logger; 36 import org.apache.ojb.broker.util.logging.LoggerFactory; 37 import org.apache.ojb.ejb.ArticleVO; 38 import org.apache.ojb.ejb.PersonVO; 39 import org.apache.ojb.odmg.TransactionExt; 40 import org.odmg.OQLQuery; 41 import org.odmg.QueryException; 42 43 107 public class RollbackBean extends ODMGBaseBeanImpl implements SessionBean 108 { 109 private Logger log = LoggerFactory.getLogger(RollbackBean.class); 110 111 private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/PersonManager"; 112 private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/ArticleManager"; 113 114 private ArticleManagerODMGLocal am; 115 private PersonManagerODMGLocal pm; 116 117 public RollbackBean() 118 { 119 } 120 121 128 public void rollbackOtherBeanUsing(List articles, List persons) 129 { 130 log.info("rollbackOtherBeanUsing method was called"); 131 ArticleManagerODMGLocal am = getArticleManager(); 133 PersonManagerODMGLocal pm = getPersonManager(); 134 am.storeArticles(articles); 135 pm.storePersons(persons); 136 137 throw new EJBException ("## Testing of rollback behaviour - rollbackOtherBeanUsing ##"); 139 } 140 141 148 public void rollbackOtherBeanUsing_2(ArticleVO article, List persons) 149 { 150 log.info("rollbackOtherBeanUsing_2 method was called"); 151 ArticleManagerODMGLocal am = getArticleManager(); 152 PersonManagerODMGLocal pm = getPersonManager(); 153 pm.storePersons(persons); 154 am.failureStore(article); 155 } 156 157 163 public void rollbackClientWrongInput(List articles, List persons) 164 { 165 log.info("rollbackClientWrongInput method was called"); 166 ArticleManagerODMGLocal am = getArticleManager(); 167 PersonManagerODMGLocal pm = getPersonManager(); 168 am.storeArticles(articles); 169 pm.storePersons(persons); 170 } 171 172 177 public void rollbackThrowException(List objects) 178 { 179 log.info("rollbackThrowException method was called"); 180 storeObjects(objects); 181 throw new EJBException ("## Testing of rollback behaviour - rollbackThrowException ##"); 183 } 184 185 190 public void rollbackPassInvalidObject(List objects) 191 { 192 log.info("rollbackPassInvalidObject method was called"); 193 storeObjects(objects); 194 } 195 196 201 public void rollbackOdmgAbort(List objects) 202 { 203 log.info("rollbackOdmgAbort method was called"); 204 storeObjects(objects); 205 getImplementation().currentTransaction().abort(); 206 } 207 208 213 public void rollbackSetRollbackOnly(List objects) 214 { 215 log.info("rollbackSetRollbackOnly method was called"); 216 storeObjects(objects); 217 221 getSessionContext().setRollbackOnly(); 222 227 getImplementation().currentTransaction().abort(); 228 } 229 230 235 public void rollbackSetRollbackAndThrowException(List objects) 236 { 237 log.info("rollbackSetRollbackAndThrowException method was called"); 238 storeObjects(objects); 239 getSessionContext().setRollbackOnly(); 240 245 getImplementation().currentTransaction().abort(); 246 throw new EJBException ("## Testing of rollback behaviour - rollbackSetRollbackAndThrowException ##"); 250 } 251 252 258 public void rollbackBreakIteration(List objectsToStore) 259 { 260 log.info("rollbackBreakIteration"); 262 266 storeObjects(objectsToStore); 267 TransactionExt tx = ((TransactionExt) getImplementation().currentTransaction()); 268 tx.flush(); 270 Class searchClass = objectsToStore.get(0).getClass(); 271 PersistenceBroker broker = tx.getBroker(); 272 Query q = new QueryByCriteria(searchClass); 273 Iterator it = broker.getIteratorByQuery(q); 275 it.next(); 276 281 getImplementation().currentTransaction().abort(); 282 throw new EJBException ("## Testing of rollback behaviour - rollbackBreakIteration ##"); 286 } 287 288 291 public List storeObjects(List objects) 292 { 293 return new ArrayList (super.storeObjects(objects)); 294 } 295 296 299 public void deleteObjects(List objects) 300 { 301 log.info("deleteObjects"); 302 super.deleteObjects(objects); 303 } 304 305 protected int getObjectCount(Class target) 306 { 307 log.info("getObjectCount was called"); 308 List list; 309 try 310 { 311 OQLQuery query = getImplementation().newOQLQuery(); 312 query.create("select allObjects from " + target.getName()); 313 list = (List ) query.execute(); 314 return list.size(); 315 } 316 catch(QueryException e) 317 { 318 throw new EJBException ("Query objects failed", e); 319 } 320 } 321 322 325 public int getArticleCount() 326 { 327 log.info("getArticleCount was called"); 328 return getObjectCount(ArticleVO.class); 329 } 330 331 334 public int getPersonCount() 335 { 336 log.info("getPersonCount was called"); 337 return getObjectCount(PersonVO.class); 338 } 339 340 343 public Collection getAllObjects(Class target) 344 { 345 if(log.isDebugEnabled()) log.debug("getAllObjects was called"); 346 OQLQuery query = getImplementation().newOQLQuery(); 347 try 348 { 349 query.create("select allObjects from " + target.getName()); 350 return (Collection ) query.execute(); 351 } 352 catch(Exception e) 353 { 354 log.error("OQLQuery failed", e); 355 throw new OJBRuntimeException("OQLQuery failed", e); 356 } 357 } 358 359 private ArticleManagerODMGLocal getArticleManager() 360 { 361 if (am == null) 362 { 363 Context context = null; 364 try 365 { 366 context = new InitialContext (); 367 am = ((ArticleManagerODMGLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create(); 368 log.info("** Found bean: " + am); 369 return am; 370 } 371 catch (NamingException e) 372 { 373 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e); 374 throw new EJBException (e); 375 } 376 catch (CreateException e) 377 { 378 log.error("Creation of ArticleManager failed", e); 379 throw new EJBException (e); 380 } 381 } 382 return am; 383 } 384 385 private PersonManagerODMGLocal getPersonManager() 386 { 387 if (pm == null) 388 { 389 Context context = null; 390 try 391 { 392 context = new InitialContext (); 393 pm = ((PersonManagerODMGLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create(); 394 log.info("** Found bean: " + pm); 395 return pm; 396 } 397 catch (NamingException e) 398 { 399 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e); 400 throw new EJBException (e); 401 } 402 catch (CreateException e) 403 { 404 log.error("Creation of PersonManager failed", e); 405 throw new EJBException (e); 406 } 407 } 408 return pm; 409 } 410 } 411 | Popular Tags |