KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > odmg > RollbackBean


1 package org.apache.ojb.ejb.odmg;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 import javax.ejb.EJBException JavaDoc;
20 import javax.ejb.SessionBean JavaDoc;
21 import javax.ejb.CreateException JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
44  * This is an session bean implementation used for testing different "rollback"
45  * scenarios for the ODMG implementation.
46  * <p/>
47  * <p/>
48  * <b>How to use ODMG</b> <br>
49  * <p/>
50  * To keep this example as simple as possible, we lookup a static OJB ODMG implementation instance
51  * on each bean instance.
52  * But it's recommended to bind an instance of the Implementation class in JNDI
53  * (at appServer start), open the database and lookup this instances via JNDI in
54  * ejbCreate().
55  * <br/>
56  * However the examples use a simple helper class to lookup the OJB resources.
57  * <p/>
58  * To use the odmg-api within your bean, you can do:
59  * <p/>
60  * <ol type="a">
61  * <li>
62  * Obtain the current Database from the Implementation instance - Attend<br>
63  * that there must be already a Database opened before.<br><i>
64  * db = odmg.getDatabase(null);<br>
65  * // ... do something<br>
66  * </i></li>
67  * <li>
68  * Obtain the current odmg-Transaction from the Implementation instance<br>
69  * to lock objects - Attend that there must be already a Database opened before.<br><i>
70  * Transaction tx = odmg.currentTransaction();<br>
71  * tx.lock(aObject, mode);
72  * </i></li>
73  * </ol>
74  * </p>
75  *
76  *
77  * @ejb:bean type="Stateless"
78  * name="RollbackBeanODMG"
79  * jndi-name="org.apache.ojb.ejb.odmg.RollbackBean"
80  * local-jndi-name="org.apache.ojb.ejb.odmg.RollbackBeanLocal"
81  * view-type="both"
82  * transaction-type="Container"
83  *
84  * @ejb:interface remote-class="org.apache.ojb.ejb.odmg.RollbackRemote"
85  * local-class="org.apache.ojb.ejb.odmg.RollbackLocal"
86  * extends="javax.ejb.EJBObject"
87  *
88  * @ejb:home remote-class="org.apache.ojb.ejb.odmg.RollbackHome"
89  * local-class="org.apache.ojb.ejb.odmg.RollbackLocalHome"
90  * extends="javax.ejb.EJBHome"
91  *
92  * @ejb:ejb-ref
93  * ejb-name="PersonManagerODMGBean"
94  * view-type="local"
95  * ref-name="ejb/ojb/odmg/PersonManager"
96  *
97  * @ejb:ejb-ref
98  * ejb-name="ArticleManagerODMGBean"
99  * view-type="local"
100  * ref-name="ejb/ojb/odmg/ArticleManager"
101  *
102  * @ejb:transaction type="Required"
103  *
104  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
105  * @version $Id: RollbackBean.java,v 1.1.2.4 2005/12/21 22:21:39 tomdz Exp $
106  */

107 public class RollbackBean extends ODMGBaseBeanImpl implements SessionBean JavaDoc
108 {
109     private Logger log = LoggerFactory.getLogger(RollbackBean.class);
110
111     private static final String JavaDoc PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/PersonManager";
112     private static final String JavaDoc 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     /**
122      * First stores all articles, persons form
123      * the lists using ArticleManager and PersonManager
124      * beans after doing that, a Exception will be thrown.
125      *
126      * @ejb:interface-method
127      */

128     public void rollbackOtherBeanUsing(List JavaDoc articles, List JavaDoc persons)
129     {
130         log.info("rollbackOtherBeanUsing method was called");
131         //store all objects
132
ArticleManagerODMGLocal am = getArticleManager();
133         PersonManagerODMGLocal pm = getPersonManager();
134         am.storeArticles(articles);
135         pm.storePersons(persons);
136
137         // after all is done we throw an exception to activate rollback process
138
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackOtherBeanUsing ##");
139     }
140
141     /**
142      * First store a list of persons then we
143      * store the article using a failure store
144      * method in ArticleManager.
145      *
146      * @ejb:interface-method
147      */

148     public void rollbackOtherBeanUsing_2(ArticleVO article, List JavaDoc 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     /**
158      * This test method expect an invalid object in the person list,
159      * so that OJB cause an internal error.
160      *
161      * @ejb:interface-method
162      */

163     public void rollbackClientWrongInput(List JavaDoc articles, List JavaDoc 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     /**
173      * The bean will throw an exception before the method ends.
174      *
175      * @ejb:interface-method
176      */

177     public void rollbackThrowException(List JavaDoc objects)
178     {
179         log.info("rollbackThrowException method was called");
180         storeObjects(objects);
181         // now we throw an exception
182
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackThrowException ##");
183     }
184
185     /**
186      * One of the objects passed by the client will cause an exception.
187      *
188      * @ejb:interface-method
189      */

190     public void rollbackPassInvalidObject(List JavaDoc objects)
191     {
192         log.info("rollbackPassInvalidObject method was called");
193         storeObjects(objects);
194     }
195
196     /**
197      * We do an odmg-tx.abort() call.
198      *
199      * @ejb:interface-method
200      */

201     public void rollbackOdmgAbort(List JavaDoc objects)
202     {
203         log.info("rollbackOdmgAbort method was called");
204         storeObjects(objects);
205         getImplementation().currentTransaction().abort();
206     }
207
208     /**
209      * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
210      *
211      * @ejb:interface-method
212      */

213     public void rollbackSetRollbackOnly(List JavaDoc objects)
214     {
215         log.info("rollbackSetRollbackOnly method was called");
216         storeObjects(objects);
217         /*
218         setRollbackOnly does only rollback the transaction, the client will not be
219         notified by an RemoteException (tested on JBoss)
220         */

221         getSessionContext().setRollbackOnly();
222         /*
223         seems that some appServer expect that all used resources will be closed before
224         the JTA-TXManager does call Synchronization#beforeCompletion(), so we have
225         to cleanup used resources. This could be done by call abort on odmg-api
226         */

227         getImplementation().currentTransaction().abort();
228     }
229
230     /**
231      * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
232      *
233      * @ejb:interface-method
234      */

235     public void rollbackSetRollbackAndThrowException(List JavaDoc objects)
236     {
237         log.info("rollbackSetRollbackAndThrowException method was called");
238         storeObjects(objects);
239         getSessionContext().setRollbackOnly();
240         /*
241         seems that some appServer expect that all used resources will be closed before
242         the JTA-TXManager does call Synchronization#beforeCompletion(), so we have
243         to cleanup used resources. This could be done by call abort on odmg-api
244         */

245         getImplementation().currentTransaction().abort();
246         // to notify the client about the failure we throw an exception
247
// if we don't throw such an exception the client don't get notified
248
// about the failure
249
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackSetRollbackAndThrowException ##");
250     }
251
252     /**
253      * We use several OJB services, start to iterate a query result and do
254      * an odmg-tx.abort call.
255      *
256      * @ejb:interface-method
257      */

258     public void rollbackBreakIteration(List JavaDoc objectsToStore)
259     {
260         // now we mix up different api's and use PB-api too
261
log.info("rollbackBreakIteration");
262         /*
263         store list of objects, then get these objects with Iterator, start
264         iteration, then break
265         */

266         storeObjects(objectsToStore);
267         TransactionExt tx = ((TransactionExt) getImplementation().currentTransaction());
268         // force writing to DB
269
tx.flush();
270         Class JavaDoc searchClass = objectsToStore.get(0).getClass();
271         PersistenceBroker broker = tx.getBroker();
272         Query q = new QueryByCriteria(searchClass);
273         // we get the iterator and step into the first found object
274
Iterator JavaDoc it = broker.getIteratorByQuery(q);
275         it.next();
276         /*
277         seems that some appServer expect that all used resources are closed before
278         the JTA-TXManager does call Synchronization#beforeCompletion(), so we have
279         to cleanup used resources. This could be done by call abort on odmg-api
280         */

281         getImplementation().currentTransaction().abort();
282         // to notify the client about the failure we throw an exception
283
// if we don't throw such an exception the client don't get notified
284
// about the failure
285
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackBreakIteration ##");
286     }
287
288     /**
289      * @ejb:interface-method
290      */

291     public List JavaDoc storeObjects(List JavaDoc objects)
292     {
293         return new ArrayList JavaDoc(super.storeObjects(objects));
294     }
295
296     /**
297      * @ejb:interface-method
298      */

299     public void deleteObjects(List JavaDoc objects)
300     {
301         log.info("deleteObjects");
302         super.deleteObjects(objects);
303     }
304
305     protected int getObjectCount(Class JavaDoc target)
306     {
307         log.info("getObjectCount was called");
308         List JavaDoc list;
309         try
310         {
311             OQLQuery query = getImplementation().newOQLQuery();
312             query.create("select allObjects from " + target.getName());
313             list = (List JavaDoc) query.execute();
314             return list.size();
315         }
316         catch(QueryException e)
317         {
318             throw new EJBException JavaDoc("Query objects failed", e);
319         }
320     }
321
322     /**
323      * @ejb:interface-method
324      */

325     public int getArticleCount()
326     {
327         log.info("getArticleCount was called");
328         return getObjectCount(ArticleVO.class);
329     }
330
331     /**
332      * @ejb:interface-method
333      */

334     public int getPersonCount()
335     {
336         log.info("getPersonCount was called");
337         return getObjectCount(PersonVO.class);
338     }
339
340     /**
341      * @ejb:interface-method
342      */

343     public Collection JavaDoc getAllObjects(Class JavaDoc 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 JavaDoc) query.execute();
351         }
352         catch(Exception JavaDoc 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 JavaDoc context = null;
364             try
365             {
366                 context = new InitialContext JavaDoc();
367                 am = ((ArticleManagerODMGLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
368                 log.info("** Found bean: " + am);
369                 return am;
370             }
371             catch (NamingException JavaDoc e)
372             {
373                 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
374                 throw new EJBException JavaDoc(e);
375             }
376             catch (CreateException JavaDoc e)
377             {
378                 log.error("Creation of ArticleManager failed", e);
379                 throw new EJBException JavaDoc(e);
380             }
381         }
382         return am;
383     }
384
385     private PersonManagerODMGLocal getPersonManager()
386     {
387         if (pm == null)
388         {
389             Context JavaDoc context = null;
390             try
391             {
392                 context = new InitialContext JavaDoc();
393                 pm = ((PersonManagerODMGLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
394                 log.info("** Found bean: " + pm);
395                 return pm;
396             }
397             catch (NamingException JavaDoc e)
398             {
399                 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
400                 throw new EJBException JavaDoc(e);
401             }
402             catch (CreateException JavaDoc e)
403             {
404                 log.error("Creation of PersonManager failed", e);
405                 throw new EJBException JavaDoc(e);
406             }
407         }
408         return pm;
409     }
410 }
411
Popular Tags