KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > hsqldb > HsqlConnection


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.hsqldb;
17
18 import scriptella.jdbc.JdbcConnection;
19 import scriptella.jdbc.JdbcUtils;
20 import scriptella.spi.ConnectionParameters;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 /**
28  * Hsqldb connection wrapper.
29  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  */

34 public class HsqlConnection extends JdbcConnection {
35     /**
36      * True if SHUTDOWN command should be executed before last connection closed. Default value is true.
37      * In 1.7.2, in-process databases are no longer closed when the last connection to the database
38      * is explicitly closed via JDBC, a SHUTDOWN is required
39      */

40     public static final String JavaDoc SHUTDOWN_ON_EXIT = "shutdown_on_exit";
41
42     private static final Logger JavaDoc LOG = Logger.getLogger(HsqlConnection.class.getName());
43     private boolean shutdownOnExit;
44
45     /**
46      * Creates a wrapper for HSQL connection.
47      *
48      * @param con
49      */

50     HsqlConnection(Connection con, ConnectionParameters parameters) {
51         super(con, parameters);
52     }
53
54     @Override JavaDoc
55     protected void init(ConnectionParameters parameters) {
56         super.init(parameters);
57         shutdownOnExit = parameters.getBooleanProperty(SHUTDOWN_ON_EXIT, true)
58                 && isInprocess(parameters.getUrl());
59     }
60
61     void shutdown() {
62         assert shutdownOnExit;
63         Connection con = getNativeConnection();
64         assert con != null; //we are going to close, so con!=null
65
try {
66             if (con.isClosed()) {
67                 LOG.info("Unable to correctly shutdown in-process HSQLDB. Connection has already already been closed");
68                 return;
69             }
70             Statement JavaDoc st = con.createStatement();
71             st.execute("SHUTDOWN");
72             JdbcUtils.closeSilent(st);
73         } catch (Exception JavaDoc e) {
74             LOG.log(Level.WARNING, "Problem occured while trying to shutdown in-process HSQLDB", e);
75         } finally {
76             JdbcUtils.closeSilent(con);
77         }
78     }
79
80     public void close() {
81         if (shutdownOnExit) {
82             HsqlConnection previous = Driver.setLastConnection(this);
83             //discards previous connection
84
if (previous != null) {
85                 previous.shutdownOnExit = false;
86                 previous.close();
87             }
88         } else {
89             super.close();
90         }
91     }
92
93     private static boolean isInprocess(String JavaDoc url) {
94         //Returning false for server modes
95
if (url.startsWith("jdbc:hsqldb:http:")) {
96             return false;
97         }
98         if (url.startsWith("jdbc:hsqldb:https:")) {
99             return false;
100         }
101         if (url.startsWith("jdbc:hsqldb:hsql")) {
102             return false;
103         }
104         return !url.startsWith("jdbc:hsqldb:hsqls");
105     }
106
107
108
109 }
110
Popular Tags