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