KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > loadbalancer > Raidb1ParallelDBScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.loadbalancer;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.objectweb.cjdbc.scenario.templates.SimpleRaidb1Template;
34
35 /**
36  * This class defines a Raidb1ParallelDBScenario
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modryzk </a>
39  * @version 1.0
40  */

41 public class Raidb1ParallelDBScenario extends SimpleRaidb1Template
42 {
43   /**
44    * Test parallelDB loadbalancer of type lprf
45    */

46   public void testFailFast()
47   {
48     try
49     {
50       //Load database
51
cm.loadVirtualDatabases(controller, "myDB",
52           "hsqldb-raidb1-parallel-lprf.xml");
53       mainVdb = controller.getVirtualDatabase("myDB");
54       mainVdb.enableAllBackends();
55       execute();
56     }
57     catch (Exception JavaDoc e)
58     {
59       e.printStackTrace();
60       fail("failed to test c-jdbc parallel loadbalancer of type least pending request first");
61     }
62   }
63
64   /**
65    * Test parallelDB loadbalancer of type roundrobin
66    */

67   public void testRoundRobin()
68   {
69     try
70     {
71       //Load database
72
cm.loadVirtualDatabases(controller, "myDB",
73           "hsqldb-raidb1-parallel-roundrobin.xml");
74       mainVdb = controller.getVirtualDatabase("myDB");
75       mainVdb.enableAllBackends();
76       execute();
77     }
78     catch (Exception JavaDoc e)
79     {
80       e.printStackTrace();
81       fail("failed to test c-jdbc parallel loadbalancer of type round robin");
82     }
83   }
84
85   private void execute() throws Exception JavaDoc
86   {
87     Class.forName("org.objectweb.cjdbc.driver.Driver");
88     int threadCount = 3;
89     ArrayList JavaDoc threads = new ArrayList JavaDoc(threadCount);
90     for (int i = 0; i < threadCount; i++)
91     {
92       ParallelThread par = new ParallelThread();
93       par.start();
94       threads.add(par);
95     }
96     for (int i = 0; i < threadCount; i++)
97     {
98       ParallelThread par = (ParallelThread) threads.get(i);
99       par.join();
100     }
101     for (int i = 0; i < threadCount; i++)
102     {
103       ParallelThread par = (ParallelThread) threads.get(i);
104       assertNull("A thread had an exception" ,
105           par.exception);
106     }
107   }
108
109   class ParallelThread extends Thread JavaDoc
110   {
111     Exception JavaDoc exception = null;
112
113     /**
114      * Starts the Parallel Thread
115      */

116     public void run()
117     {
118       try
119       {
120         // Execute request
121
Properties JavaDoc props = new Properties JavaDoc();
122         props.put("user", "user");
123         props.put("password", "");
124         Connection JavaDoc con = DriverManager.getConnection(
125             "jdbc:cjdbc://localhost/myDB", props);
126         assertNotNull("Connection is null", con);
127         Statement JavaDoc statement = con.createStatement();
128         for (int i = 0; i < 20; i++)
129         {
130           statement.executeUpdate("update product set name='Parallel Test" + i
131               + "'");
132           statement.executeQuery("select * from PRODUCT");
133         }
134       }
135       catch (Exception JavaDoc e)
136       {
137         if (exception == null)
138         {
139           exception = e;
140         }
141         e.printStackTrace();
142       }
143     }
144   }
145 }
146
Popular Tags