DriverManager.java revision 3768
dc661981841afbd8a4fb4e94d510f080531ddc16theAdib * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * This code is free software; you can redistribute it and/or modify it
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * under the terms of the GNU General Public License version 2 only, as
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * published by the Free Software Foundation. Oracle designates this
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * particular file as subject to the "Classpath" exception as provided
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * by Oracle in the LICENSE file that accompanied this code.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * This code is distributed in the hope that it will be useful, but WITHOUT
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * version 2 for more details (a copy is included in the LICENSE file that
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * accompanied this code).
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * You should have received a copy of the GNU General Public License version
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * 2 along with this work; if not, write to the Free Software Foundation,
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * or visit www.oracle.com if you need additional information or have any
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * questions.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morganimport java.util.concurrent.CopyOnWriteArrayList;
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <P>The basic service for managing a set of JDBC drivers.<br>
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <B>NOTE:</B> The {@link <code>DataSource</code>} interface, new in the
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * JDBC 2.0 API, provides another way to connect to a data source.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * The use of a <code>DataSource</code> object is the preferred means of
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * connecting to a data source.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <P>As part of its initialization, the <code>DriverManager</code> class will
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * attempt to load the driver classes referenced in the "jdbc.drivers"
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * system property. This allows a user to customize the JDBC Drivers
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * used by their applications. For example in your
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * ~/.hotjava/properties file you might specify:
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE>
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan *<P> The <code>DriverManager</code> methods <code>getConnection</code> and
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <code>getDrivers</code> have been enhanced to support the Java Standard Edition
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">Service Provider</a> mechanism. JDBC 4.0 Drivers must
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * include the file <code>META-INF/services/java.sql.Driver</code>. This file contains the name of the JDBC drivers
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * implementation of <code>java.sql.Driver</code>. For example, to load the <code>my.sql.Driver</code> class,
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * the <code>META-INF/services/java.sql.Driver</code> file would contain the entry:
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <code>my.sql.Driver</code>
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <P>Applications no longer need to explictly load JDBC drivers using <code>Class.forName()</code>. Existing programs
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * which currently load JDBC drivers using <code>Class.forName()</code> will continue to work without
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * modification.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * <P>When the method <code>getConnection</code> is called,
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * the <code>DriverManager</code> will attempt to
38e4ad89ca32c4c0a5bb2acae7e9b2e89ac210d1JazzyNico * locate a suitable driver from amongst those loaded at
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * initialization and those loaded explicitly using the same classloader
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * as the current applet or application.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * Starting with the Java 2 SDK, Standard Edition, version 1.3, a
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * logging stream can be set only if the proper
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * permission has been granted. Normally this will be done with
d380b9443b463cec3bc1bdb313452647858fd88btheAdib * the tool PolicyTool, which can be used to grant <code>permission
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * java.sql.SQLPermission "setLog"</code>.
18d2737bb9a2e47325277e7916dea6c8a1363a1eSveinn à Felli * @see Driver
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * @see Connection
d380b9443b463cec3bc1bdb313452647858fd88btheAdib // List of registered JDBC drivers
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan private final static CopyOnWriteArrayList<Driver> registeredDrivers = new CopyOnWriteArrayList<Driver>();
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan private static volatile java.io.PrintWriter logWriter = null;
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan private static volatile java.io.PrintStream logStream = null;
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan // Used in println() to synchronize logWriter
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan private final static Object logSync = new Object();
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan /* Prevent the DriverManager class from being instantiated. */
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * Load the initial JDBC drivers by checking the System property
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * jdbc.properties and then use the {@code ServiceLoader} mechanism
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * The <code>SQLPermission</code> constant that allows the
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * setting of the logging stream.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * @since 1.3
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan final static SQLPermission SET_LOG_PERMISSION =
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan //--------------------------JDBC 2.0-----------------------------
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * Retrieves the log writer.
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * The <code>getLogWriter</code> and <code>setLogWriter</code>
c556b852adbbb75d16564cf48962727ac3b916b4Chris Morgan * methods should be used instead
* of the <code>get/setlogStream</code> methods, which are deprecated.
return logWriter;
throws SQLException {
throws SQLException {
return (aDriver);
throws SQLException {
throw new NullPointerException();
throws SQLException {
throw new SecurityException();
public static int getLoginTimeout() {
return (loginTimeout);
return logStream;
synchronized (logSync) {
boolean result = false;
result = false;
return result;
private static void loadInitialDrivers() {
} catch(Throwable t) {
return null;
synchronized(DriverManager.class) {
return (con);
throw reason;