Debian Codegraph @

src/main/org/h2/Driver.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;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10 import java.sql.DriverPropertyInfo;
11 import java.sql.SQLException;
12 import java.util.Properties;
13 import java.util.logging.Logger;
14 import org.h2.api.ErrorCode;
15 import org.h2.engine.Constants;
16 import org.h2.jdbc.JdbcConnection;
17 import org.h2.message.DbException;
18
19 /**
20 * The database driver. An application should not use this class directly. The
21 * only thing the application needs to do is load the driver. This can be done
22 * using Class.forName. To load the driver and open a database connection, use
23 * the following code:
24 *
25 * <pre>
26 * Class.forName(&quot;org.h2.Driver&quot;);
27 * Connection conn = DriverManager.getConnection(
28 * &quot;jdbc:h2:&tilde;/test&quot;, &quot;sa&quot;, &quot;sa&quot;);
29 * </pre>
30 */
31 public class Driver implements java.sql.Driver, JdbcDriverBackwardsCompat {
32
33 private static final Driver INSTANCE = new Driver();
34 private static final String DEFAULT_URL = "jdbc:default:connection";
35 private static final ThreadLocal<Connection> DEFAULT_CONNECTION =
36 new ThreadLocal<>();
37
38 private static boolean registered;
39
40 static {
41 load();
42 }
43
44 /**
45 * Open a database connection.
46 * This method should not be called by an application.
47 * Instead, the method DriverManager.getConnection should be used.
48 *
49 * @param url the database URL
50 * @param info the connection properties
51 * @return the new connection or null if the URL is not supported
52 * @throws SQLException on connection exception or if URL is {@code null}
53 */
54 @Override
55 public Connection connect(String url, Properties info) throws SQLException {
56 if (url == null) {
57 throw DbException.getJdbcSQLException(ErrorCode.URL_FORMAT_ERROR_2, null, Constants.URL_FORMAT, null);
58 } else if (url.startsWith(Constants.START_URL)) {
59 return new JdbcConnection(url, info, null, null, false);
60 } else if (url.equals(DEFAULT_URL)) {
61 return DEFAULT_CONNECTION.get();
62 } else {
63 return null;
64 }
65 }
66
67 /**
68 * Check if the driver understands this URL.
69 * This method should not be called by an application.
70 *
71 * @param url the database URL
72 * @return if the driver understands the URL
73 * @throws SQLException if URL is {@code null}
74 */
75 @Override
76 public boolean acceptsURL(String url) throws SQLException {
77 if (url == null) {
78 throw DbException.getJdbcSQLException(ErrorCode.URL_FORMAT_ERROR_2, null, Constants.URL_FORMAT, null);
79 } else if (url.startsWith(Constants.START_URL)) {
80 return true;
81 } else if (url.equals(DEFAULT_URL)) {
82 return DEFAULT_CONNECTION.get() != null;
83 } else {
84 return false;
85 }
86 }
87
88 /**
89 * Get the major version number of the driver.
90 * This method should not be called by an application.
91 *
92 * @return the major version number
93 */
94 @Override
95 public int getMajorVersion() {
96 return Constants.VERSION_MAJOR;
97 }
98
99 /**
100 * Get the minor version number of the driver.
101 * This method should not be called by an application.
102 *
103 * @return the minor version number
104 */
105 @Override
106 public int getMinorVersion() {
107 return Constants.VERSION_MINOR;
108 }
109
110 /**
111 * Get the list of supported properties.
112 * This method should not be called by an application.
113 *
114 * @param url the database URL
115 * @param info the connection properties
116 * @return a zero length array
117 */
118 @Override
119 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
120 return new DriverPropertyInfo[0];
121 }
122
123 /**
124 * Check if this driver is compliant to the JDBC specification.
125 * This method should not be called by an application.
126 *
127 * @return true
128 */
129 @Override
130 public boolean jdbcCompliant() {
131 return true;
132 }
133
134 /**
135 * [Not supported]
136 */
137 @Override
138 public Logger getParentLogger() {
139 return null;
140 }
141
142 /**
143 * INTERNAL
144 * @return instance of the driver registered with the DriverManager
145 */
146 public static synchronized Driver load() {
147 try {
148 if (!registered) {
149 registered = true;
150 DriverManager.registerDriver(INSTANCE);
151 }
152 } catch (SQLException e) {
153 DbException.traceThrowable(e);
154 }
155 return INSTANCE;
156 }
157
158 /**
159 * INTERNAL
160 */
161 public static synchronized void unload() {
162 try {
163 if (registered) {
164 registered = false;
165 DriverManager.deregisterDriver(INSTANCE);
166 }
167 } catch (SQLException e) {
168 DbException.traceThrowable(e);
169 }
170 }
171
172 /**
173 * INTERNAL
174 * Sets, on a per-thread basis, the default-connection for
175 * user-defined functions.
176 * @param c to set default to
177 */
178 public static void setDefaultConnection(Connection c) {
179 if (c == null) {
180 DEFAULT_CONNECTION.remove();
181 } else {
182 DEFAULT_CONNECTION.set(c);
183 }
184 }
185
186 /**
187 * INTERNAL
188 * @param thread to set context class loader for
189 */
190 public static void setThreadContextClassLoader(Thread thread) {
191 // Apache Tomcat: use the classloader of the driver to avoid the
192 // following log message:
193 // org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
194 // SEVERE: The web application appears to have started a thread named
195 // ... but has failed to stop it.
196 // This is very likely to create a memory leak.
197 try {
198 thread.setContextClassLoader(Driver.class.getClassLoader());
199 } catch (Throwable t) {
200 // ignore
201 }
202 }
203
204 }