Debian Codegraph @

src/test/org/h2/samples/ShowProgress.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.samples;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10 import java.sql.PreparedStatement;
11 import java.sql.SQLException;
12 import java.sql.Statement;
13 import java.util.concurrent.TimeUnit;
14
15 import org.h2.api.DatabaseEventListener;
16 import org.h2.engine.SessionLocal;
17 import org.h2.jdbc.JdbcConnection;
18
19 /**
20 * This example application implements a database event listener. This is useful
21 * to display progress information while opening a large database, or to log
22 * database exceptions.
23 */
24 public class ShowProgress implements DatabaseEventListener {
25
26 private final long startNs;
27 private long lastNs;
28
29 /**
30 * Create a new instance of this class, and startNs the timer.
31 */
32 public ShowProgress() {
33 startNs = lastNs = System.nanoTime();
34 }
35
36 /**
37 * This method is called when executing this sample application from the
38 * command line.
39 *
40 * @param args the command line parameters
41 * @throws Exception on failure
42 */
43 public static void main(String... args) throws Exception {
44 new ShowProgress().test();
45 }
46
47 /**
48 * Run the progress test.
49 */
50 void test() throws Exception {
51 Class.forName("org.h2.Driver");
52 Connection conn = DriverManager.getConnection("jdbc:h2:./test", "sa", "");
53 Statement stat = conn.createStatement();
54 stat.execute("DROP TABLE IF EXISTS TEST");
55 stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
56 PreparedStatement prep = conn.prepareStatement(
57 "INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
58 long time;
59 time = System.nanoTime();
60 int len = 1000;
61 for (int i = 0; i < len; i++) {
62 long now = System.nanoTime();
63 if (now > time + TimeUnit.SECONDS.toNanos(1)) {
64 time = now;
65 System.out.println("Inserting " + (100L * i / len) + "%");
66 }
67 prep.setInt(1, i);
68 prep.execute();
69 }
70 boolean abnormalTermination = true;
71 if (abnormalTermination) {
72 ((SessionLocal) ((JdbcConnection) conn).getSession()).getDatabase().setPowerOffCount(1);
73 try {
74 stat.execute("INSERT INTO TEST VALUES(-1, 'Test' || SPACE(100))");
75 } catch (SQLException e) {
76 // ignore
77 }
78 } else {
79 conn.close();
80 }
81
82 System.out.println("Open connection...");
83 time = System.nanoTime();
84 conn = DriverManager.getConnection(
85 "jdbc:h2:./test;DATABASE_EVENT_LISTENER='" +
86 getClass().getName() + "'", "sa", "");
87 time = System.nanoTime() - time;
88 System.out.println("Done after " + TimeUnit.NANOSECONDS.toMillis(time) + " ms");
89 prep.close();
90 stat.close();
91 conn.close();
92
93 }
94
95 /**
96 * This method is called if an exception occurs in the database.
97 *
98 * @param e the exception
99 * @param sql the SQL statement
100 */
101 @Override
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
109 * progress.
110 *
111 * @param state the current state
112 * @param name the object name (depends on the state)
113 * @param current the current progress
114 * @param max the 100% mark
115 */
116 @Override
117 public void setProgress(int state, String name, long current, long max) {
118 long time = System.nanoTime();
119 if (time < lastNs + TimeUnit.SECONDS.toNanos(5)) {
120 return;
121 }
122 lastNs = time;
123 String stateName = "?";
124 switch (state) {
125 case STATE_SCAN_FILE:
126 stateName = "Scan " + name;
127 break;
128 case STATE_CREATE_INDEX:
129 stateName = "Create Index " + name;
130 break;
131 case STATE_RECOVER:
132 stateName = "Recover";
133 break;
134 default:
135 return;
136 }
137 try {
138 Thread.sleep(1);
139 } catch (InterruptedException e) {
140 // ignore
141 }
142 System.out.println("State: " + stateName + " " +
143 (100 * current / max) + "% (" +
144 current + " of " + max + ") "
145 + TimeUnit.NANOSECONDS.toMillis(time - startNs) + " ms");
146 }
147
148 /**
149 * This method is called when the database is closed.
150 */
151 @Override
152 public void closingDatabase() {
153 System.out.println("Closing the database");
154 }
155
156 /**
157 * This method is called just after creating the instance.
158 *
159 * @param url the database URL
160 */
161 @Override
162 public void init(String url) {
163 System.out.println("Initializing the event listener for database " + url);
164 }
165
166 /**
167 * This method is called when the database is open.
168 */
169 @Override
170 public void opened() {
171 // do nothing
172 }
173
174 }