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.PreparedStatement;
10: import java.sql.Statement;
11: import java.util.Random;
12:
13: import org.h2.test.TestBase;
14:
15: /**
16: * Tests the cache.
17: */
18: public class TestCache extends TestBase {
19:
20: public void test() throws Exception {
21: if (config.memory) {
22: return;
23: }
24: deleteDb("cache");
25: Connection conn = getConnection("cache");
26: Statement stat = conn.createStatement();
27: stat.execute("SET CACHE_SIZE 1024");
28: stat
29: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
30: stat
31: .execute("CREATE TABLE MAIN(ID INT PRIMARY KEY, NAME VARCHAR)");
32: PreparedStatement prep = conn
33: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
34: PreparedStatement prep2 = conn
35: .prepareStatement("INSERT INTO MAIN VALUES(?, ?)");
36: int max = 10000;
37: for (int i = 0; i < max; i++) {
38: prep.setInt(1, i);
39: prep.setString(2, "Hello " + i);
40: prep.execute();
41: prep2.setInt(1, i);
42: prep2.setString(2, "World " + i);
43: prep2.execute();
44: }
45: conn.close();
46: conn = getConnection("cache");
47: stat = conn.createStatement();
48: stat.execute("SET CACHE_SIZE 1024");
49: Random random = new Random(1);
50: for (int i = 0; i < 100; i++) {
51: stat
52: .executeQuery("SELECT * FROM MAIN WHERE ID BETWEEN 40 AND 50");
53: stat.executeQuery("SELECT * FROM MAIN WHERE ID = "
54: + random.nextInt(max));
55: if ((i % 10) == 0) {
56: stat.executeQuery("SELECT * FROM TEST");
57: }
58: }
59: conn.close();
60: }
61:
62: }
|