1 5 package org.h2.test.db; 6 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 12 import org.h2.api.DatabaseEventListener; 13 import org.h2.test.TestBase; 14 15 public class TestListener extends TestBase implements DatabaseEventListener { 16 17 private long last, start; 18 19 public TestListener() { 20 start = last = System.currentTimeMillis(); 21 } 22 23 public void test() throws Exception { 24 if(config.networked) { 25 return; 26 } 27 deleteDb("listener"); 28 Connection conn; 29 conn = getConnection("listener"); 30 Statement stat = conn.createStatement(); 31 stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); 32 PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))"); 33 int len = getSize(100, 100000); 34 for(int i=0; i<len; i++) { 35 prep.setInt(1, i); 36 prep.execute(); 37 } 38 crash(conn); 39 40 conn = getConnection("listener;database_event_listener='" + getClass().getName() + "'"); 41 conn.close(); 42 43 } 44 45 public void diskSpaceIsLow(long stillAvailable) throws SQLException { 46 System.out.println("diskSpaceIsLow stillAvailable="+stillAvailable); 47 } 48 49 public void exceptionThrown(SQLException e) { 50 TestBase.logError("exceptionThrown", e); 51 } 52 53 public void setProgress(int state, String name, int current, int max) { 54 long time = System.currentTimeMillis(); 55 if(time < last+1000) { 56 return; 57 } 58 last = time; 59 String stateName; 60 switch(state) { 61 case STATE_SCAN_FILE: 62 stateName = "Scan " + name; 63 break; 64 case STATE_CREATE_INDEX: 65 stateName = "Create Index " + name; 66 break; 67 case STATE_RECOVER: 68 stateName = "Recover"; 69 break; 70 default: 71 TestBase.logError("unknownn state: " + state, null); 72 stateName = "? " + name; 73 } 74 try { 75 Thread.sleep(1); 76 } catch (InterruptedException e) { 77 } 78 System.out.println("state: " + stateName + " " + (100*current/max) + " " + (time-start)); 79 } 80 81 public void closingDatabase() { 82 } 83 84 public void init(String url) { 85 } 86 87 } 88 | Popular Tags |