01: /*
02: * HsqlShutdownHook.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.shutdown;
13:
14: import java.sql.SQLException;
15: import java.sql.Statement;
16: import workbench.db.ConnectionMgr;
17: import workbench.db.WbConnection;
18: import workbench.log.LogMgr;
19: import workbench.util.SqlUtil;
20:
21: /**
22: * A Shutdown hook for H2 Database.
23: *
24: * @author support@sql-workbench.net
25: */
26: public class HsqlShutdownHook implements DbShutdownHook {
27:
28: private boolean canShutdown(WbConnection con) {
29: String url = con.getUrl();
30: if (url == null)
31: return true;
32:
33: // this is a HSQL server connection. Do not shut down this!
34: if (url.startsWith("jdbc:hsqldb:hsql:"))
35: return false;
36:
37: return true;
38: }
39:
40: public void shutdown(WbConnection con) throws SQLException {
41: if (con == null)
42: return;
43:
44: boolean otherActive = ConnectionMgr.getInstance().isActive(con);
45: if (otherActive)
46: return;
47:
48: if (canShutdown(con)) {
49: Statement stmt = null;
50: try {
51: stmt = con.createStatement();
52: LogMgr
53: .logInfo(
54: "HsqlShutdownHook.shutdown()",
55: "Local HSQL connection detected. Sending SHUTDOWN to the engine before disconnecting");
56: stmt.executeUpdate("SHUTDOWN");
57: } catch (Exception e) {
58: LogMgr.logWarning("HsqlShutdownHook.shutdown()",
59: "Error when executing SHUTDOWN", e);
60: } finally {
61: SqlUtil.closeStatement(stmt);
62: }
63: }
64: con.close();
65: }
66:
67: /**
68: * Disconnects a local HSQL connection. Beginning with 1.7.2 the local
69: * (=in process) engine should be closed down with SHUTDOWN when
70: * disconnecting. It shouldn't hurt for pre-1.7.2 either :-)
71: */
72: private void shutdownHsql(WbConnection con) {
73:
74: }
75:
76: }
|