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.jdbc;
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: import org.h2.constant.ErrorCode;
015: import org.h2.constant.SysProperties;
016: import org.h2.test.TestBase;
017:
018: /**
019: * Tests Statement.cancel
020: */
021: public class TestCancel extends TestBase {
022:
023: class CancelThread extends Thread {
024: private Statement cancel;
025: private int wait;
026:
027: CancelThread(Statement cancel, int wait) {
028: this .cancel = cancel;
029: this .wait = wait;
030: }
031:
032: public void run() {
033: try {
034: Thread.sleep(wait);
035: cancel.cancel();
036: Thread.yield();
037: } catch (SQLException e) {
038: // ignore errors on closed statements
039: } catch (Exception e) {
040: TestBase.logError("sleep", e);
041: }
042: }
043: }
044:
045: public void test() throws Exception {
046: testMaxQueryTimeout();
047: testQueryTimeout();
048: testJdbcQueryTimeout();
049: testCancelStatement();
050: }
051:
052: private void testJdbcQueryTimeout() throws Exception {
053: deleteDb("cancel");
054: Connection conn = getConnection("cancel");
055: Statement stat = conn.createStatement();
056: check(0, stat.getQueryTimeout());
057: stat.setQueryTimeout(1);
058: check(1, stat.getQueryTimeout());
059: Statement s2 = conn.createStatement();
060: check(1, s2.getQueryTimeout());
061: ResultSet rs = s2
062: .executeQuery("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME = 'QUERY_TIMEOUT'");
063: rs.next();
064: check(1000, rs.getInt(1));
065: try {
066: stat
067: .executeQuery("SELECT MAX(RAND()) FROM SYSTEM_RANGE(1, 100000000)");
068: error();
069: } catch (SQLException e) {
070: check(ErrorCode.STATEMENT_WAS_CANCELLED, e.getErrorCode());
071: }
072: stat.setQueryTimeout(0);
073: stat.execute("SET QUERY_TIMEOUT 1100");
074: check(2, stat.getQueryTimeout());
075: conn.close();
076: }
077:
078: private void testQueryTimeout() throws Exception {
079: deleteDb("cancel");
080: Connection conn = getConnection("cancel");
081: Statement stat = conn.createStatement();
082: stat.execute("SET QUERY_TIMEOUT 10");
083: try {
084: stat
085: .executeQuery("SELECT MAX(RAND()) FROM SYSTEM_RANGE(1, 100000000)");
086: error();
087: } catch (SQLException e) {
088: check(ErrorCode.STATEMENT_WAS_CANCELLED, e.getErrorCode());
089: }
090: conn.close();
091: }
092:
093: private void testMaxQueryTimeout() throws Exception {
094: deleteDb("cancel");
095: int oldMax = SysProperties.getMaxQueryTimeout();
096: try {
097: System.setProperty(SysProperties.H2_MAX_QUERY_TIMEOUT,
098: "" + 10);
099: Connection conn = getConnection("cancel");
100: Statement stat = conn.createStatement();
101: try {
102: stat
103: .executeQuery("SELECT MAX(RAND()) FROM SYSTEM_RANGE(1, 100000000)");
104: error();
105: } catch (SQLException e) {
106: check(ErrorCode.STATEMENT_WAS_CANCELLED, e
107: .getErrorCode());
108: }
109: conn.close();
110: } finally {
111: System.setProperty("h2.maxQueryTimeout", "" + oldMax);
112: }
113: }
114:
115: private void testCancelStatement() throws Exception {
116: deleteDb("cancel");
117: Connection conn = getConnection("cancel");
118: Statement stat = conn.createStatement();
119: stat.execute("DROP TABLE IF EXISTS TEST");
120: stat
121: .execute("CREATE MEMORY TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
122: PreparedStatement prep = conn
123: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
124: trace("insert");
125: int len = getSize(1, 1000);
126: for (int i = 0; i < len; i++) {
127: prep.setInt(1, i);
128: // prep.setString(2, "Test Value "+i);
129: prep.setString(2, "hi");
130: prep.execute();
131: }
132: trace("inserted");
133: // TODO test insert.. select
134: for (int i = 1;;) {
135: Statement query = conn.createStatement();
136: CancelThread cancel = new CancelThread(query, i);
137: cancel.start();
138: Thread.yield();
139: int j = 0;
140: try {
141: ResultSet rs = query.executeQuery("SELECT * FROM TEST");
142: while (rs.next()) {
143: j++;
144: }
145: trace("record count: " + j);
146: } catch (SQLException e) {
147: checkNotGeneralException(e);
148: // ignore cancelled statements
149: trace("record count: " + j);
150: }
151: if (j == 0) {
152: i += 10;
153: } else if (j == len) {
154: break;
155: }
156: }
157: conn.close();
158: }
159:
160: }
|