KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > Raidb1BasicFailoverScenario


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;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 import org.objectweb.cjdbc.scenario.templates.SimpleRaidb1Template;
33
34 /**
35  * This class defines a Raidb1BasicFailoverScenario. Test the stability of the
36  * controller when databases are failing
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class Raidb1BasicFailoverScenario extends SimpleRaidb1Template
42 {
43   /**
44    * Test CJDBC failover in raidb1 with variable pool
45    */

46   public void testFailOverWithVariablePool()
47   {
48     try
49     {
50       //Load database
51
cm.loadVirtualDatabases(controller, "myDB",
52           "hsqldb-raidb1-variablepool.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 failover in variable pool");
61     }
62   }
63
64   /**
65    * Test CJDBC failover in raidb1 with randomwait pool
66    */

67   public void testFailOverWithRandomWaitPool()
68   {
69     try
70     {
71       //Load database
72
cm.loadVirtualDatabases(controller, "myDB",
73           "hsqldb-raidb1-randomwaitpool.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 failover in random wait pool");
82     }
83   }
84
85   /**
86    * Test CJDBC failover in raidb1 with fail fast pool
87    */

88   public void testFailOverWithFailFastPool()
89   {
90     try
91     {
92       //Load database
93
cm.loadVirtualDatabases(controller, "myDB",
94           "hsqldb-raidb1-failfastpool.xml");
95       mainVdb = controller.getVirtualDatabase("myDB");
96       mainVdb.enableAllBackends();
97       execute();
98     }
99     catch (Exception JavaDoc e)
100     {
101       e.printStackTrace();
102       fail("failed to test c-jdbc failover in fail fast wait pool");
103     }
104   }
105
106   /**
107    * Test CJDBC failover in raidb1 with simple connection manager
108    */

109   public void testFailOverWithNoPool()
110   {
111     try
112     {
113       //Load database
114
cm.loadVirtualDatabases(controller, "myDB", "hsqldb-raidb1-nopool.xml");
115       mainVdb = controller.getVirtualDatabase("myDB");
116       mainVdb.enableAllBackends();
117       execute();
118     }
119     catch (Exception JavaDoc e)
120     {
121       e.printStackTrace();
122       fail("failed to test c-jdbc failover in simple connection manager");
123     }
124   }
125
126   /**
127    * Execute the test for failover once the database is loaded
128    */

129   private void execute() throws Exception JavaDoc
130   {
131     // Execute a request
132
Connection JavaDoc con = getCJDBCConnection();
133     assertNotNull("Connection is null", con);
134     Statement JavaDoc statement = con.createStatement();
135     ResultSet JavaDoc rs1 = statement.executeQuery("Select * from document");
136     assertNotNull("ResultSet is null", rs1);
137
138     // Drop a backend
139
hm.stop(hm1);
140
141     // Wait for backend to receive the dead command
142
synchronized (this)
143     {
144       wait(1000);
145     }
146
147     // Execute requests with same connection
148
Statement JavaDoc statement2 = con.createStatement();
149     ResultSet JavaDoc rs2 = statement2.executeQuery("Select * from document");
150     assertNotNull("ResultSet after failover is null", rs2);
151     rs1.last();
152     rs2.last();
153     assertTrue("Row numbers are different", rs1.getRow() == rs2.getRow());
154     rs1.first();
155     rs2.first();
156     while (rs1.next() & rs2.next())
157     {
158       assertTrue("Some result differs from expect result set", rs1.getString(
159           "id").equals(rs2.getString("id")));
160     }
161
162     // Drop other backend
163
hm.stop(hm2);
164
165     // Wait for backend to receive the dead command
166
synchronized (this)
167     {
168       wait(1000);
169     }
170     // Execute request
171
Statement JavaDoc statement3 = con.createStatement();
172     ResultSet JavaDoc rs3 = null;
173     try
174     {
175       rs3 = statement3.executeQuery("Select * from document");
176     }
177     catch (SQLException JavaDoc expected)
178     {
179       // expected cause no more backends.
180
}
181     assertNull("Should not be able to get a result set anymore", rs3);
182   }
183 }
184
Popular Tags