001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.samples;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.PreparedStatement;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013:
014: import org.h2.api.DatabaseEventListener;
015: import org.h2.jdbc.JdbcConnection;
016:
017: /**
018: * This example application implements a database event listener.
019: * This is useful to display progress information while opening a large database,
020: * or to log database exceptions.
021: */
022: public class ShowProgress implements DatabaseEventListener {
023:
024: private long last, start;
025:
026: /**
027: * Create a new instance of this class, and start the timer.
028: */
029: public ShowProgress() {
030: start = last = System.currentTimeMillis();
031: }
032:
033: public static void main(String[] args) throws Exception {
034: new ShowProgress().test();
035: }
036:
037: /**
038: * Run the progress test.
039: */
040: void test() throws Exception {
041: Class.forName("org.h2.Driver");
042: Connection conn = DriverManager.getConnection(
043: "jdbc:h2:test;LOG=2", "sa", "");
044: Statement stat = conn.createStatement();
045: stat.execute("DROP TABLE IF EXISTS TEST");
046: stat
047: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
048: PreparedStatement prep = conn
049: .prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
050: long time;
051: time = System.currentTimeMillis();
052: int len = 1000;
053: for (int i = 0; i < len; i++) {
054: long last = System.currentTimeMillis();
055: if (last > time + 1000) {
056: time = last;
057: System.out.println("Inserting " + (100L * i / len)
058: + "%");
059: }
060: prep.setInt(1, i);
061: prep.execute();
062: }
063: boolean abnormalTermination = true;
064: if (abnormalTermination) {
065: ((JdbcConnection) conn).setPowerOffCount(1);
066: try {
067: stat
068: .execute("INSERT INTO TEST VALUES(-1, 'Test' || SPACE(100))");
069: } catch (SQLException e) {
070: }
071: } else {
072: conn.close();
073: }
074:
075: System.out.println("Open connection...");
076: time = System.currentTimeMillis();
077: conn = DriverManager.getConnection(
078: "jdbc:h2:test;LOG=2;DATABASE_EVENT_LISTENER='"
079: + getClass().getName() + "'", "sa", "");
080: time = System.currentTimeMillis() - time;
081: System.out.println("Done after " + time + " ms");
082: conn.close();
083:
084: }
085:
086: /**
087: * This method is called by the database if disk space is low.
088: *
089: * @param stillAvailable the number of bytes still available
090: */
091: public void diskSpaceIsLow(long stillAvailable) throws SQLException {
092: System.out.println("diskSpaceIsLow stillAvailable="
093: + stillAvailable);
094: }
095:
096: /**
097: * This method is called if an exception occurs in the database.
098: *
099: * @param e the exception
100: * @param sql the SQL statement
101: */
102: public void exceptionThrown(SQLException e, String sql) {
103: System.out.println("Error executing " + sql);
104: e.printStackTrace();
105: }
106:
107: /**
108: * This method is called when opening the database to notify about the progress.
109: *
110: * @param state the current state
111: * @param name the object name (depends on the state)
112: * @param current the current progress
113: * @param max the 100% mark
114: */
115: public void setProgress(int state, String name, int current, int max) {
116: long time = System.currentTimeMillis();
117: if (time < last + 5000) {
118: return;
119: }
120: last = time;
121: String stateName = "?";
122: switch (state) {
123: case STATE_SCAN_FILE:
124: stateName = "Scan " + name;
125: break;
126: case STATE_CREATE_INDEX:
127: stateName = "Create Index " + name;
128: break;
129: case STATE_RECOVER:
130: stateName = "Recover";
131: break;
132: }
133: try {
134: Thread.sleep(1);
135: } catch (InterruptedException e) {
136: }
137: System.out.println("State: " + stateName + " "
138: + (100 * current / max) + "% (" + current + " of "
139: + max + ") " + (time - start) + " ms");
140: }
141:
142: /**
143: * This method is called when the database is closed.
144: */
145: public void closingDatabase() {
146: System.out.println("Closing the database");
147: }
148:
149: /**
150: * This method is called just after creating the instance.
151: *
152: * @param url the database URL
153: */
154: public void init(String url) {
155: System.out
156: .println("Initializing the event listener for database "
157: + url);
158: }
159:
160: /**
161: * This method is called when the database is open.
162: */
163: public void opened() {
164: }
165:
166: }
|