01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.synth.thread;
07:
08: import java.sql.Connection;
09: import java.sql.PreparedStatement;
10: import java.sql.ResultSet;
11: import java.sql.SQLException;
12:
13: /**
14: * The operation part of {@link TestMulti}.
15: * Executes simple queries and updates in a table.
16: */
17: public class TestMultiNewsSimple extends TestMultiThread {
18:
19: Connection conn;
20:
21: static int newsCount = 10000;
22:
23: static int getNewsCount() {
24: return newsCount;
25: }
26:
27: TestMultiNewsSimple(TestMulti base) throws SQLException {
28: super (base);
29: conn = base.getConnection();
30: }
31:
32: void first() throws SQLException {
33: Connection conn = base.getConnection();
34: conn
35: .createStatement()
36: .execute(
37: "create table news(id identity, state int default 0, text varchar default '')");
38: PreparedStatement prep = conn
39: .prepareStatement("insert into news() values()");
40: for (int i = 0; i < newsCount; i++) {
41: prep.executeUpdate();
42: }
43: conn.createStatement().execute(
44: "update news set text = 'Text' || id");
45: conn.close();
46: }
47:
48: void begin() throws SQLException {
49: }
50:
51: void end() throws SQLException {
52: conn.close();
53: }
54:
55: void operation() throws SQLException {
56: if (random.nextInt(10) == 0) {
57: conn.setAutoCommit(random.nextBoolean());
58: } else if (random.nextInt(10) == 0) {
59: if (random.nextBoolean()) {
60: conn.commit();
61: } else {
62: // conn.rollback();
63: }
64: } else {
65: if (random.nextBoolean()) {
66: PreparedStatement prep = conn
67: .prepareStatement("update news set state = ? where id = ?");
68: prep.setInt(1, random.nextInt(getNewsCount()));
69: prep.setInt(2, random.nextInt(10));
70: prep.execute();
71: } else {
72: PreparedStatement prep = conn
73: .prepareStatement("select * from news where id = ?");
74: prep.setInt(1, random.nextInt(getNewsCount()));
75: ResultSet rs = prep.executeQuery();
76: if (!rs.next()) {
77: System.out.println("No row found");
78: // throw new Error("No row found");
79: }
80: if (rs.next()) {
81: System.out.println("Multiple rows found");
82: // throw new Error("Multiple rows found");
83: }
84: }
85: }
86: }
87:
88: void finalTest() throws SQLException {
89: // TODO Auto-generated method stub
90:
91: }
92:
93: }
|