File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\Driver.java.html

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