KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > TestMultiNewsSimple


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11
12 public class TestMultiNewsSimple extends TestMultiThread {
13     
14     Connection JavaDoc conn;
15     
16     static int newsCount = 10000;
17     static int getNewsCount() {
18         return newsCount;
19     }
20
21     TestMultiNewsSimple(TestMulti base) throws SQLException JavaDoc {
22         super(base);
23         conn = base.getConnection();
24     }
25     
26     void first() throws SQLException JavaDoc {
27         Connection JavaDoc conn = base.getConnection();
28         conn.createStatement().execute("create table news(fid identity, fstate int default 0, text varchar default '')");
29         PreparedStatement JavaDoc prep = conn.prepareStatement("insert into news() values()");
30         for(int i=0; i<newsCount; i++) {
31             prep.executeUpdate();
32         }
33         conn.createStatement().execute("update news set text = 'Text' || fid");
34         conn.close();
35     }
36
37     void begin() throws SQLException JavaDoc {
38     }
39
40     void end() throws SQLException JavaDoc {
41         conn.close();
42     }
43
44     void operation() throws SQLException JavaDoc {
45         if(random.nextInt(10)==0) {
46             conn.setAutoCommit(random.nextBoolean());
47         } else if(random.nextInt(10)==0) {
48             if(random.nextBoolean()) {
49                 conn.commit();
50             } else {
51 // conn.rollback();
52
}
53         } else {
54             if(random.nextBoolean()) {
55                 PreparedStatement JavaDoc prep = conn.prepareStatement("update news set fstate = ? where fid = ?");
56                 prep.setInt(1, random.nextInt(getNewsCount()));
57                 prep.setInt(2, random.nextInt(10));
58                 prep.execute();
59             } else {
60                 PreparedStatement JavaDoc prep = conn.prepareStatement("select * from news where fid = ?");
61                 prep.setInt(1, random.nextInt(getNewsCount()));
62                 ResultSet JavaDoc rs = prep.executeQuery();
63                 if(!rs.next()) {
64                     System.out.println("No row found");
65 // throw new Error("No row found");
66
}
67                 if(rs.next()) {
68                     System.out.println("Multiple rows found");
69 // throw new Error("Multiple rows found");
70
}
71             }
72         }
73     }
74
75     void finalTest() throws SQLException JavaDoc {
76         // TODO Auto-generated method stub
77

78     }
79         
80 }
81
Popular Tags