001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth.thread;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013:
014: /**
015: * The operation part of {@link TestMulti}.
016: * Queries and updates a table.
017: */
018: public class TestMultiNews extends TestMultiThread {
019:
020: private static final String PREFIX_URL = "http://feeds.wizbangblog.com/WizbangFullFeed?m=";
021:
022: int len = 10000;
023: Connection conn;
024:
025: TestMultiNews(TestMulti base) throws SQLException {
026: super (base);
027: conn = base.getConnection();
028: }
029:
030: void operation() throws SQLException {
031: if (random.nextInt(10) == 0) {
032: conn.close();
033: conn = base.getConnection();
034: } else if (random.nextInt(10) == 0) {
035: if (random.nextBoolean()) {
036: conn.commit();
037: } else {
038: conn.rollback();
039: }
040: } else if (random.nextInt(10) == 0) {
041: conn.setAutoCommit(random.nextBoolean());
042: } else {
043: if (random.nextBoolean()) {
044: PreparedStatement prep;
045: if (random.nextBoolean()) {
046: prep = conn
047: .prepareStatement("SELECT * FROM NEWS WHERE LINK = ?");
048: } else {
049: prep = conn
050: .prepareStatement("SELECT * FROM NEWS WHERE VALUE = ?");
051: }
052: prep.setString(1, PREFIX_URL + random.nextInt(len));
053: ResultSet rs = prep.executeQuery();
054: if (!rs.next()) {
055: throw new SQLException("expected one row, got none");
056: }
057: if (rs.next()) {
058: throw new SQLException("expected one row, got more");
059: }
060: } else {
061: PreparedStatement prep = conn
062: .prepareStatement("UPDATE NEWS SET STATE = ? WHERE FID = ?");
063: prep.setInt(1, random.nextInt(100));
064: prep.setInt(2, random.nextInt(len));
065: int count = prep.executeUpdate();
066: if (count != 1) {
067: throw new SQLException("expected one row, got "
068: + count);
069: }
070: }
071: }
072: }
073:
074: void begin() throws SQLException {
075: }
076:
077: void end() throws SQLException {
078: conn.close();
079: }
080:
081: void finalTest() throws Exception {
082: }
083:
084: void first() throws SQLException {
085: Connection conn = base.getConnection();
086: Statement stat = conn.createStatement();
087: stat.execute("CREATE TABLE TEST (ID IDENTITY, NAME VARCHAR)");
088: stat
089: .execute("CREATE TABLE NEWS (FID NUMERIC(19) PRIMARY KEY, COMMENTS LONGVARCHAR, "
090: + "LINK VARCHAR(255), STATE INTEGER, VALUE VARCHAR(255))");
091: stat
092: .execute("CREATE INDEX IF NOT EXISTS NEWS_GUID_VALUE_INDEX ON NEWS(VALUE)");
093: stat
094: .execute("CREATE INDEX IF NOT EXISTS NEWS_LINK_INDEX ON NEWS(LINK)");
095: stat
096: .execute("CREATE INDEX IF NOT EXISTS NEWS_STATE_INDEX ON NEWS(STATE)");
097: PreparedStatement prep = conn
098: .prepareStatement("INSERT INTO NEWS (FID, COMMENTS, LINK, STATE, VALUE) VALUES "
099: + "(?, ?, ?, ?, ?) ");
100: PreparedStatement prep2 = conn
101: .prepareStatement("INSERT INTO TEST (NAME) VALUES (?)");
102: for (int i = 0; i < len; i++) {
103: int x = random.nextInt(10) * 128;
104: StringBuffer buff = new StringBuffer();
105: while (buff.length() < x) {
106: buff.append("Test ");
107: buff.append(buff.length());
108: buff.append(' ');
109: }
110: String comment = buff.toString();
111: prep.setInt(1, i); // FID
112: prep.setString(2, comment); // COMMENTS
113: prep.setString(3, PREFIX_URL + i); // LINK
114: prep.setInt(4, 0); // STATE
115: prep.setString(5, PREFIX_URL + i); // VALUE
116: prep.execute();
117: prep2.setString(1, comment);
118: prep2.execute();
119: }
120: }
121:
122: }
|