KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > jdbc > TestCancel


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.jdbc;
6
7 import java.sql.*;
8
9 import org.h2.test.TestBase;
10
11 /**
12  * @author Thomas
13  */

14
15 public class TestCancel extends TestBase {
16     
17     class Canceller extends Thread JavaDoc {
18         private Statement cancel;
19         private int wait;
20         Canceller(Statement cancel, int wait) {
21             this.cancel = cancel;
22             this.wait = wait;
23         }
24         public void run() {
25             try {
26                 Thread.sleep(wait);
27                 cancel.cancel();
28                 Thread.yield();
29             } catch (SQLException e) {
30                 // ignore errors on closed statements
31
} catch(Exception JavaDoc e) {
32                 TestBase.logError("sleep", e);
33             }
34         }
35     }
36     
37     public void test() throws Exception JavaDoc {
38         deleteDb("cancel");
39         Connection conn = getConnection("cancel");
40         Statement stat = conn.createStatement();
41         stat.execute("DROP TABLE IF EXISTS TEST");
42         stat.execute("CREATE MEMORY TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
43         PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
44         trace("insert");
45         int len = getSize(1, 1000);
46         for(int i=0; i<len; i++) {
47             prep.setInt(1, i);
48             //prep.setString(2, "Test Value "+i);
49
prep.setString(2, "hi");
50             prep.execute();
51         }
52         trace("inserted");
53         // TODO test insert.. select
54
for (int i = 1;;) {
55             Statement query = conn.createStatement();
56             Canceller canceller = new Canceller(query, i);
57             canceller.start();
58             Thread.yield();
59             int j=0;
60             try {
61                 ResultSet rs = query.executeQuery("SELECT * FROM TEST");
62                 while(rs.next()) {
63                     j++;
64                 }
65                 trace("record count: "+j);
66             } catch(SQLException e) {
67                 checkNotGeneralException(e);
68                 // ignore cancelled statements
69
trace("record count: "+j);
70             }
71             if(j == 0) {
72                 i += 10;
73             } else if(j == len) {
74                 break;
75             }
76         }
77         conn.close();
78     }
79
80 }
81
Popular Tags