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.unit;
07:
08: import java.sql.Connection;
09: import java.sql.DriverManager;
10: import java.sql.PreparedStatement;
11: import java.sql.SQLException;
12:
13: import org.h2.test.TestBase;
14:
15: /**
16: * Tests the multi-threaded kernel feature.
17: */
18: public class TestMultiThreadedKernel extends TestBase implements
19: Runnable {
20:
21: private String url, user, password;
22: private int id;
23: private TestMultiThreadedKernel master;
24: private volatile boolean stop;
25:
26: public void test() throws Exception {
27: if (config.networked) {
28: return;
29: }
30: deleteDb("multiThreadedKernel");
31: int count = getSize(2, 5);
32: Thread[] list = new Thread[count];
33: for (int i = 0; i < count; i++) {
34: TestMultiThreadedKernel r = new TestMultiThreadedKernel();
35: r.url = getURL("multiThreadedKernel;MULTI_THREADED=1", true);
36: r.user = getUser();
37: r.password = getPassword();
38: r.master = this ;
39: r.id = i;
40: Thread thread = new Thread(r);
41: thread.setName("Thread " + i);
42: thread.start();
43: list[i] = thread;
44: }
45: Thread.sleep(getSize(2000, 5000));
46: stop = true;
47: for (int i = 0; i < count; i++) {
48: list[i].join();
49: }
50: }
51:
52: public void run() {
53: try {
54: org.h2.Driver.load();
55: Connection conn = DriverManager.getConnection(url
56: + ";MULTI_THREADED=1;LOCK_MODE=3;WRITE_DELAY=0",
57: user, password);
58: conn
59: .createStatement()
60: .execute(
61: "CREATE TABLE TEST"
62: + id
63: + "(COL1 BIGINT AUTO_INCREMENT PRIMARY KEY, COL2 BIGINT)");
64: PreparedStatement prep = conn
65: .prepareStatement("insert into TEST" + id
66: + "(col2) values (?)");
67: for (int i = 0; !master.stop; i++) {
68: prep.setLong(1, i);
69: prep.execute();
70: }
71: conn.close();
72: } catch (SQLException e) {
73: e.printStackTrace();
74: }
75: }
76:
77: }
|