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.db;
007:
008: import java.sql.Connection;
009: import java.sql.ResultSet;
010: import java.sql.SQLException;
011: import java.sql.Statement;
012:
013: import org.h2.test.TestBase;
014:
015: /**
016: * Tests the meta data tables information_schema.locks and sessions.
017: */
018: public class TestSessionsLocks extends TestBase {
019:
020: public void test() throws Exception {
021: testCancelStatement();
022: testLocks();
023: }
024:
025: private void testLocks() throws Exception {
026: deleteDb("sessionsLocks");
027: Connection conn = getConnection("sessionsLocks;MULTI_THREADED=1");
028: Statement stat = conn.createStatement();
029: ResultSet rs;
030: rs = stat
031: .executeQuery("select * from information_schema.locks order by session_id");
032: checkFalse(rs.next());
033: Connection conn2 = getConnection("sessionsLocks");
034: Statement stat2 = conn2.createStatement();
035: stat2
036: .execute("create table test(id int primary key, name varchar)");
037: conn2.setAutoCommit(false);
038: stat2.execute("insert into test values(1, 'Hello')");
039: rs = stat
040: .executeQuery("select * from information_schema.locks order by session_id");
041: rs.next();
042: check("PUBLIC", rs.getString("TABLE_SCHEMA"));
043: check("TEST", rs.getString("TABLE_NAME"));
044: rs.getString("SESSION_ID");
045: if (config.mvcc) {
046: check("READ", rs.getString("LOCK_TYPE"));
047: } else {
048: check("WRITE", rs.getString("LOCK_TYPE"));
049: }
050: checkFalse(rs.next());
051: conn2.commit();
052: conn2
053: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
054: stat2.execute("SELECT * FROM TEST");
055: rs = stat
056: .executeQuery("select * from information_schema.locks order by session_id");
057: if (!config.mvcc) {
058: rs.next();
059: check("PUBLIC", rs.getString("TABLE_SCHEMA"));
060: check("TEST", rs.getString("TABLE_NAME"));
061: rs.getString("SESSION_ID");
062: check("READ", rs.getString("LOCK_TYPE"));
063: }
064: checkFalse(rs.next());
065: conn2.commit();
066: rs = stat
067: .executeQuery("select * from information_schema.locks order by session_id");
068: checkFalse(rs.next());
069: conn.close();
070: conn2.close();
071: }
072:
073: public void testCancelStatement() throws Exception {
074: deleteDb("sessionsLocks");
075: Connection conn = getConnection("sessionsLocks;MULTI_THREADED=1");
076: Statement stat = conn.createStatement();
077: ResultSet rs;
078: rs = stat
079: .executeQuery("select * from information_schema.sessions order by SESSION_START, ID");
080: rs.next();
081: int sessionId = rs.getInt("ID");
082: rs.getString("USER_NAME");
083: rs.getTimestamp("SESSION_START");
084: rs.getString("STATEMENT");
085: rs.getTimestamp("STATEMENT_START");
086: checkFalse(rs.next());
087: Connection conn2 = getConnection("sessionsLocks");
088: final Statement stat2 = conn2.createStatement();
089: rs = stat
090: .executeQuery("select * from information_schema.sessions order by SESSION_START, ID");
091: check(rs.next());
092: check(sessionId, rs.getInt("ID"));
093: check(rs.next());
094: int otherId = rs.getInt("ID");
095: check(otherId != sessionId);
096: checkFalse(rs.next());
097: stat2.execute("set throttle 1");
098: final boolean[] done = new boolean[1];
099: Runnable runnable = new Runnable() {
100: public void run() {
101: try {
102: stat2
103: .execute("select count(*) from system_range(1, 10000000) t1, system_range(1, 10000000) t2");
104: new Error("Unexpected success").printStackTrace();
105: } catch (SQLException e) {
106: done[0] = true;
107: }
108: }
109: };
110: new Thread(runnable).start();
111: while (true) {
112: Thread.sleep(100);
113: rs = stat.executeQuery("CALL CANCEL_SESSION(" + otherId
114: + ")");
115: rs.next();
116: if (rs.getBoolean(1)) {
117: for (int i = 0; i < 20; i++) {
118: Thread.sleep(100);
119: if (done[0]) {
120: break;
121: }
122: }
123: check(done[0]);
124: break;
125: } else {
126: // no statement is executing yet
127: }
128: }
129: conn2.close();
130: conn.close();
131: }
132:
133: }
|