KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > test > ShowConcurrencyErrors


1 /*
2  * Speedo: an implementation of JDO compliant personality on top of JORM
3  * generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Release: 1.0
20  *
21  * Created on Apr 19, 2004 @author franck.milleville@cgey.com
22  *
23  */

24 package org.objectweb.speedo.j2eedo.test;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jdo.JDOException;
32 import javax.jdo.JDOFatalException;
33 import javax.jdo.JDOHelper;
34 import javax.jdo.PersistenceManagerFactory;
35
36 import org.objectweb.speedo.j2eedo.bo.DatabaseImpl;
37 import org.objectweb.speedo.j2eedo.common.PMHolder;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * This class allows to highlight conccurent access deadlock and issues.
42  *
43  * @author fmillevi@yahoo.com
44  * @see MainLauncher
45  */

46 public class ShowConcurrencyErrors extends MainLauncher {
47     /**
48      * This method connects to the database using the default speedo properties
49      * file and calls the test method doTest
50      *
51      * @param args
52      * not used
53      * @throws JDOException
54      * @throws Exception
55      * @see #doTest()
56      */

57     public static void main(String JavaDoc[] args) throws JDOException, Exception JavaDoc {
58         ShowConcurrencyErrors ml = new ShowConcurrencyErrors();
59         ml.initPMF();
60         ml.doTest();
61     }
62
63     private static final int NUMBER_OF_THREAD = 3;
64     private static final int NUMBER_OF_GET_BY_ID = 100;
65     private static final int NUMBER_OF_QUERIES = 100;
66     private static final int NUMBER_OF_REMOVE = 20;
67     private static final int NUMBER_OF_CREATE = 20;
68     private static final int NUMBER_OF_SPLIT = 40;
69
70     /**
71      * In order to stress the concurrency manager, the main function starts 3
72      * threads performing the same actions on projects :
73      * <ul>
74      * <li>Get by id</li>
75      * <li>Query by members or id</li>
76      * <li>Create new project</li>
77      * <li>Split a project</li>
78      * <li>Delete a project</li>
79      */

80     public void doTest() throws JDOException, Exception JavaDoc {
81         final int nbThread = NUMBER_OF_THREAD;
82         Thread JavaDoc ts[] = new Thread JavaDoc[nbThread];
83         final int nbTx = nbThread;
84         final List JavaDoc methodsList = new ArrayList JavaDoc();
85
86         for (int i = 0; i < NUMBER_OF_GET_BY_ID; i++)
87             methodsList.add(DatabaseImpl.PARAMETER_GET_PROJECT);
88         for (int i = 0; i < NUMBER_OF_QUERIES; i++)
89             methodsList.add(DatabaseImpl.PARAMETER_QUERY_PROJECTS);
90         for (int i = 0; i < NUMBER_OF_CREATE; i++)
91             methodsList.add(DatabaseImpl.PARAMETER_NEW_PROJECT);
92         for (int i = 0; i < NUMBER_OF_SPLIT; i++)
93             methodsList.add(DatabaseImpl.PARAMETER_SPLIT_PROJECT);
94         for (int i = 0; i < NUMBER_OF_REMOVE; i++)
95             methodsList.add(DatabaseImpl.PARAMETER_REM_PROJECT);
96
97         for (int thread = 0; thread < nbThread; thread++) {
98             ts[thread] = new Thread JavaDoc(new Runnable JavaDoc() {
99                 public void run() {
100                     String JavaDoc action = null;
101                     String JavaDoc returnStr = null;
102                     Iterator JavaDoc iter = null;
103                     PersistenceManagerFactory pmf =
104                         JDOHelper.getPersistenceManagerFactory(p);
105                     PMHolder pmHolder = new PMHolder(pmf);
106
107                     DatabaseImpl databaseImpl = new DatabaseImpl(pmHolder);
108
109                     int j = 0;
110
111                     for (int i = 0; i < nbTx; i++) {
112                         logger.log(BasicLevel.INFO, "Start loop " + i);
113                         Collections.shuffle(methodsList);
114                         iter = methodsList.iterator();
115                         while (iter.hasNext()) {
116                             if (0 == (j++ % 100.0))
117                                 logger.log(
118                                     BasicLevel.INFO,
119                                     j + " actions called...");
120                             action = (String JavaDoc) iter.next();
121                             // check if the action need to start a transaction
122
logger.log(
123                                 BasicLevel.DEBUG,
124                                 "Calls method:" + action);
125                             try {
126                                 returnStr = databaseImpl.doAction(action, true);
127                             } catch (JDOFatalException e) {
128                                 logger.log(
129                                     BasicLevel.WARN,
130                                     "Action '"
131                                         + action
132                                         + "' throws a JDOFatalException exception :",
133                                     e);
134                             } catch (JDOException e) {
135                                 logger.log(
136                                     BasicLevel.WARN,
137                                     "Action '"
138                                         + action
139                                         + "' throws a JDO exception :",
140                                     e);
141                             } catch (Exception JavaDoc e) {
142                                 logger.log(
143                                     BasicLevel.WARN,
144                                     "Action '"
145                                         + action
146                                         + "' throws an exception :",
147                                     e);
148                             }
149
150                             logger.log(
151                                 BasicLevel.DEBUG,
152                                 "The method "
153                                     + action
154                                     + " returns:\n"
155                                     + returnStr);
156                         }
157                     }
158                     logger.log(BasicLevel.INFO, j + " actions called ");
159                 }
160             });
161         }
162         //-------------- Launch all threads ------------//
163
for (int thread = 0; thread < nbThread; thread++) {
164             ts[thread].start();
165         }
166     }
167 }
168
Popular Tags