comparison tests/BugConcurrent_sequences.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 04fbf3655452
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
7 */
8
9 import java.sql.*;
10
11 public class BugConcurrent_sequences {
12 public static void main(String[] args) throws Exception {
13 Connection con1 = null, con2 = null;
14 Statement stmt1 = null, stmt2 = null;
15 ResultSet rs1 = null, rs2 = null;
16 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
17 con1 = DriverManager.getConnection(args[0]);
18 con2 = DriverManager.getConnection(args[0]);
19 stmt1 = con1.createStatement();
20 stmt2 = con2.createStatement();
21
22 // >> true: auto commit should be on by default
23 System.out.println("0. true\t" + con1.getAutoCommit());
24 System.out.println("0. true\t" + con2.getAutoCommit());
25
26 // create a table
27 try {
28 System.out.print("1. create table t1 using client 1... ");
29 stmt1.executeUpdate("CREATE TABLE t1 ( id serial, who varchar(12) )");
30 System.out.println("passed :)");
31 } catch (SQLException e) {
32 // this means we failed (that sux)
33 System.out.println("FAILED 1 :( " + e.getMessage());
34 System.out.println("ABORTING TEST!!!");
35 con1.close();
36 con2.close();
37 System.exit(-1);
38 }
39
40 // test the insertion of values with concurrent clients
41 try {
42 System.out.print("2. insert into t1 using client 1 and 2... ");
43 stmt1.executeUpdate("INSERT INTO t1(who) VALUES('client1')");
44 System.out.println("client 1 passed :)");
45 con2.setAutoCommit(false);
46 stmt2.executeUpdate("INSERT INTO t1(who) VALUES('client2')");
47 System.out.println("transaction on client 2 :)");
48 stmt1.executeUpdate("INSERT INTO t1(who) VALUES('client1')");
49 System.out.println("client 1 passed :)");
50 try {
51 con2.commit();
52 System.out.println("transaction client 2 PASSED :(");
53 System.exit(-1);
54 } catch (SQLException e) {
55 System.out.println("transaction client 2 failed :)");
56 }
57 con2.setAutoCommit(true);
58 stmt2.executeUpdate("INSERT INTO t1(who) VALUES('client2')");
59 System.out.println("passed :)");
60 System.out.println("2.1. check table status with client 1...");
61 rs1 = stmt1.executeQuery("SELECT * FROM t1");
62 while (rs1.next())
63 System.out.println(rs1.getInt("id") + ", " +
64 rs1.getString("who"));
65 System.out.println("passed :)");
66 System.out.println("2.2. check table status with client 2...");
67 rs2 = stmt2.executeQuery("SELECT * FROM t1");
68 while (rs2.next())
69 System.out.println(rs2.getInt("id") + ", " +
70 rs2.getString("who"));
71 System.out.println("passed :)");
72 } catch (SQLException e) {
73 // this means we failed (table not there perhaps?)
74 System.out.println("FAILED 2 :( " + e.getMessage());
75 System.out.println("ABORTING TEST!!!");
76 if (rs1 != null) rs1.close();
77 if (rs2 != null) rs2.close();
78 con1.close();
79 con2.close();
80 System.exit(-1);
81 }
82
83 // drop the table (not dropping the sequence) from client 1
84 try {
85 System.out.print("3.1. drop table t1 using client 1... ");
86 stmt1.executeUpdate("DROP TABLE t1");
87 System.out.println("passed :)");
88 System.out.print("3.1. recreate t1 using client 1... ");
89 stmt1.executeUpdate("CREATE TABLE t1 ( id serial, who varchar(12) )");
90 System.out.println("passed :)");
91 } catch (SQLException e) {
92 // this means we failed (table not there perhaps?)
93 System.out.println("FAILED 3 :( " + e.getMessage());
94 System.out.println("ABORTING TEST!!!");
95 if (rs1 != null) rs1.close();
96 if (rs2 != null) rs2.close();
97 con1.close();
98 con2.close();
99 System.exit(-1);
100 }
101
102 // re-establish connection
103 try {
104 System.out.print("x. Reconnecting client 1 and 2... ");
105 con1.close();
106 con2.close();
107 con1 = DriverManager.getConnection(args[0]);
108 con2 = DriverManager.getConnection(args[0]);
109 stmt1 = con1.createStatement();
110 stmt2 = con2.createStatement();
111 System.out.println("passed :)");
112 } catch (SQLException e) {
113 // this means we failed (table not there perhaps?)
114 System.out.println("FAILED x :( " + e.getMessage());
115 System.out.println("ABORTING TEST!!!");
116 if (rs1 != null) rs1.close();
117 if (rs2 != null) rs2.close();
118 con1.close();
119 con2.close();
120 System.exit(-1);
121 }
122
123 // insert and print, should get 1,2
124 try {
125 System.out.println("4. insert into t1 using client 1 and 2...");
126 stmt1.executeUpdate("INSERT INTO t1(who) VALUES('client1')");
127 System.out.println("passed :)");
128 con2.setAutoCommit(false);
129 stmt2.executeUpdate("INSERT INTO t1(who) VALUES('client2')");
130 con2.commit();
131 con2.setAutoCommit(true);
132 System.out.println("passed :)");
133 System.out.println("4.1. check table status with client 1...");
134 rs1 = stmt1.executeQuery("SELECT * FROM t1 ORDER BY who");
135 for (int cntr = 1; rs1.next(); cntr++) {
136 int id = rs1.getInt("id");
137 System.out.println(id + ", " +
138 rs1.getString("who"));
139 if (id != cntr)
140 throw new SQLException("expected " + cntr + ", got " + id);
141 }
142 System.out.println("passed :)");
143 System.out.println("4.2. check table status with client 2...");
144 rs2 = stmt2.executeQuery("SELECT * FROM t1 ORDER BY who");
145 for (int cntr = 1; rs2.next(); cntr++) {
146 int id = rs2.getInt("id");
147 System.out.println(id + ", " +
148 rs2.getString("who"));
149 if (id != cntr)
150 throw new SQLException("expected " + cntr + ", got " + id);
151 }
152 System.out.println("passed :)");
153 } catch (SQLException e) {
154 // this means we failed (table not there perhaps?)
155 System.out.println("FAILED 4 :( " + e.getMessage());
156 System.out.println("ABORTING TEST!!!");
157 if (rs1 != null) rs1.close();
158 if (rs2 != null) rs2.close();
159 con1.close();
160 con2.close();
161 System.exit(-1);
162 }
163
164 if (rs1 != null) rs1.close();
165 if (rs2 != null) rs2.close();
166
167 // cleanup
168 try {
169 stmt1.executeUpdate("DROP TABLE t1");
170 } catch (SQLException e) {
171 System.out.println("FAILED to clean up! :( " + e.getMessage());
172 }
173
174 con1.close();
175 con2.close();
176 }
177 }