KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > driver > ReadWriteScenario


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): Marck Wick.
22  * Contributor(s): Emmanuel Cecchet.
23  */

24 package org.objectweb.cjdbc.scenario.raidb1.driver;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import org.objectweb.cjdbc.scenario.templates.Raidb1Template;
32
33 /**
34  * Read after Write test class. Problem submitted by Mark Wick on the mailing
35  * list: http://www.objectweb.org/wws/arc/c-jdbc/2003-09/msg00072.html
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
38  */

39 public class ReadWriteScenario extends Raidb1Template
40 {
41   
42   /**
43    * Test method for clobs.
44    *@throws Exception possibly ...
45    */

46   public void testReadWrite() throws Exception JavaDoc
47   {
48       Connection JavaDoc con = getCJDBCConnection();
49       Statement JavaDoc stmt = con.createStatement();
50       String JavaDoc tableName = "test"+ System.currentTimeMillis();
51       stmt.executeUpdate("create table "+tableName+" (id INTEGER)");
52       stmt.executeUpdate("delete from "+tableName);
53       Hashtable JavaDoc ht = new Hashtable JavaDoc();
54       for (int i = 1; i < 100; i++)
55       {
56         int counter = 1;
57         stmt.executeUpdate("insert into "+tableName+" (id) values (" + i + ")");
58         while (counter < 50)
59         {
60           ResultSet JavaDoc rs = stmt.executeQuery("select max(id) as id from "+tableName);
61           rs.next();
62           if (rs.getInt("id") != i)
63             fail(
64               "Read after write broken: id value ("
65                 + rs.getInt("id")
66                 + ") different from expected value ("
67                 + i
68                 + ")");
69           rs.close();
70           counter++;
71         }
72         Integer JavaDoc tot = (Integer JavaDoc) ht.get(new Integer JavaDoc(counter));
73         if (tot == null)
74         {
75           tot = new Integer JavaDoc(1);
76           ht.put(new Integer JavaDoc(counter), tot);
77         }
78         else
79         {
80           ht.put(new Integer JavaDoc(counter), new Integer JavaDoc(tot.intValue() + 1));
81         }
82       }
83       con.createStatement().executeUpdate("drop table "+tableName);
84       con.close();
85   }
86
87 }
88
Popular Tags