jdk/src/share/classes/java/sql/DriverManager.java
author lancea
Sat, 19 Jan 2013 10:53:14 -0500
changeset 15278 e081d3f73b75
parent 14174 74a7d8795c22
child 16887 b7c122a88eed
permissions -rw-r--r--
8005080: JDBC 4.2 Core changes Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.sql;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ServiceLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    32
import java.util.concurrent.CopyOnWriteArrayList;
12885
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
    33
import sun.reflect.Reflection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <P>The basic service for managing a set of JDBC drivers.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <B>NOTE:</B> The {@link <code>DataSource</code>} interface, new in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * JDBC 2.0 API, provides another way to connect to a data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The use of a <code>DataSource</code> object is the preferred means of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * connecting to a data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>As part of its initialization, the <code>DriverManager</code> class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * attempt to load the driver classes referenced in the "jdbc.drivers"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * system property. This allows a user to customize the JDBC Drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * used by their applications. For example in your
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * ~/.hotjava/properties file you might specify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *<P> The <code>DriverManager</code> methods <code>getConnection</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>getDrivers</code> have been enhanced to support the Java Standard Edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">Service Provider</a> mechanism. JDBC 4.0 Drivers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * include the file <code>META-INF/services/java.sql.Driver</code>. This file contains the name of the JDBC drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * implementation of <code>java.sql.Driver</code>.  For example, to load the <code>my.sql.Driver</code> class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the <code>META-INF/services/java.sql.Driver</code> file would contain the entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>my.sql.Driver</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <P>Applications no longer need to explictly load JDBC drivers using <code>Class.forName()</code>. Existing programs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * which currently load JDBC drivers using <code>Class.forName()</code> will continue to work without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <P>When the method <code>getConnection</code> is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the <code>DriverManager</code> will attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * locate a suitable driver from amongst those loaded at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * initialization and those loaded explicitly using the same classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * as the current applet or application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Starting with the Java 2 SDK, Standard Edition, version 1.3, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * logging stream can be set only if the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * permission has been granted.  Normally this will be done with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * the tool PolicyTool, which can be used to grant <code>permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * java.sql.SQLPermission "setLog"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see Driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see Connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class DriverManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    83
    // List of registered JDBC drivers
11129
f9ad1aadf3fa 7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents: 9243
diff changeset
    84
    private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    85
    private static volatile int loginTimeout = 0;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    86
    private static volatile java.io.PrintWriter logWriter = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    87
    private static volatile java.io.PrintStream logStream = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    88
    // Used in println() to synchronize logWriter
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    89
    private final static  Object logSync = new Object();
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    90
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    91
    /* Prevent the DriverManager class from being instantiated. */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    92
    private DriverManager(){}
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    93
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    94
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    95
    /**
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    96
     * Load the initial JDBC drivers by checking the System property
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    97
     * jdbc.properties and then use the {@code ServiceLoader} mechanism
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    98
     */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    99
    static {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   100
        loadInitialDrivers();
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   101
        println("JDBC DriverManager initialized");
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   102
    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   103
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The <code>SQLPermission</code> constant that allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * setting of the logging stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    final static SQLPermission SET_LOG_PERMISSION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        new SQLPermission("setLog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    //--------------------------JDBC 2.0-----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Retrieves the log writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The <code>getLogWriter</code> and <code>setLogWriter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * methods should be used instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * of the <code>get/setlogStream</code> methods, which are deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return a <code>java.io.PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #setLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static java.io.PrintWriter getLogWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return logWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Sets the logging/tracing <code>PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * that is used by the <code>DriverManager</code> and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * There is a minor versioning problem created by the introduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * of the method <code>setLogWriter</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * method <code>setLogWriter</code> cannot create a <code>PrintStream</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * that will be returned by <code>getLogStream</code>---the Java platform does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * not provide a backward conversion.  As a result, a new application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * that uses <code>setLogWriter</code> and also uses a JDBC 1.0 driver that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>getLogStream</code> will likely not see debugging information written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * by that driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Starting with the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param out the new logging/tracing <code>PrintStream</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *      <code>null</code> to disable logging and tracing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *    if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *    <code>checkPermission</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *    setting the log writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see #getLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static void setLogWriter(java.io.PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            logStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            logWriter = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    //---------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * the set of registered JDBC drivers.
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   175
     *<p>
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   176
     * <B>Note:</B> If a property is specified as part of the {@code url} and
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   177
     * is also specified in the {@code Properties} object, it is
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   178
     * implementation-defined as to which value will take precedence.
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   179
     * For maximum portability, an application should only specify a
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   180
     * property once.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param info a list of arbitrary string tag/value pairs as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * connection arguments; normally at least a "user" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * "password" property should be included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return a Connection to the URL
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   188
     * @exception SQLException if a database access error occurs or the url is
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   189
     * {@code null}
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   190
     * @throws SQLTimeoutException  when the driver has determined that the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   191
     * timeout value specified by the {@code setLoginTimeout} method
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   192
     * has been exceeded and has at least tried to cancel the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   193
     * current database connection attempt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        java.util.Properties info) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * the set of registered JDBC drivers.
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   209
     *<p>
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   210
     * <B>Note:</B> If a property is specified as part of the {@code url} and
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   211
     * is also specified in the {@code Properties} object, it is
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   212
     * implementation-defined as to which value will take precedence.
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   213
     * For maximum portability, an application should only specify a
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   214
     * property once.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param user the database user on whose behalf the connection is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *   made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param password the user's password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return a connection to the URL
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   222
     * @exception SQLException if a database access error occurs or the url is
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   223
     * {@code null}
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   224
     * @throws SQLTimeoutException  when the driver has determined that the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   225
     * timeout value specified by the {@code setLoginTimeout} method
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   226
     * has been exceeded and has at least tried to cancel the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   227
     * current database connection attempt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        String user, String password) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (user != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            info.put("user", user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            info.put("password", password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *  <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return a connection to the URL
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   255
     * @exception SQLException if a database access error occurs or the url is
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   256
     * {@code null}
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   257
     * @throws SQLTimeoutException  when the driver has determined that the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   258
     * timeout value specified by the {@code setLoginTimeout} method
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   259
     * has been exceeded and has at least tried to cancel the
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   260
     * current database connection attempt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public static Connection getConnection(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Attempts to locate a driver that understands the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param url a database URL of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return a <code>Driver</code> object representing a driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * that can connect to the given URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public static Driver getDriver(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        println("DriverManager.getDriver(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   294
        // Walk through the loaded registeredDrivers attempting to locate someone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // who understands the given URL.
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   296
        for (DriverInfo aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // skip it.
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   299
            if(isDriverAllowed(aDriver.driver, callerCL)) {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   300
                try {
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   301
                    if(aDriver.driver.acceptsURL(url)) {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   302
                        // Success!
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   303
                        println("getDriver returning " + aDriver.driver.getClass().getName());
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   304
                    return (aDriver.driver);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   305
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   306
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   307
                } catch(SQLException sqe) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   308
                    // Drop through and try the next driver.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   309
                }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   310
            } else {
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   311
                println("    skipping: " + aDriver.driver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   313
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        println("getDriver: no suitable driver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        throw new SQLException("No suitable driver", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Registers the given driver with the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * A newly-loaded driver class should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * the method <code>registerDriver</code> to make itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * known to the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param driver the new JDBC Driver that is to be registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *               <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public static synchronized void registerDriver(java.sql.Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        throws SQLException {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   333
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   334
        /* Register the driver if it has not already been added to our list */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   335
        if(driver != null) {
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   336
            registeredDrivers.addIfAbsent(new DriverInfo(driver));
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   337
        } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   338
            // This is for compatibility with the original DriverManager
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   339
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   342
        println("registerDriver: " + driver);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Drops a driver from the <code>DriverManager</code>'s list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *  Applets can only deregister drivers from their own classloaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param driver the JDBC Driver to drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public static synchronized void deregisterDriver(Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws SQLException {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   355
        if (driver == null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   356
            return;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   357
        }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Gets the classloader of the code that called this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        println("DriverManager.deregisterDriver: " + driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   364
        DriverInfo aDriver = new DriverInfo(driver);
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   365
        if(registeredDrivers.contains(aDriver)) {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   366
            if (isDriverAllowed(driver, callerCL)) {
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   367
                 registeredDrivers.remove(aDriver);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   368
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   369
                // If the caller does not have permission to load the driver then
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   370
                // throw a SecurityException.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   371
                throw new SecurityException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   373
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            println("    couldn't find driver to unload");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Retrieves an Enumeration with all of the currently loaded JDBC drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * to which the current caller has access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <P><B>Note:</B> The classname of a driver can be found using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <CODE>d.getClass().getName()</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return the list of JDBC Drivers loaded by the caller's class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static java.util.Enumeration<Driver> getDrivers() {
11129
f9ad1aadf3fa 7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents: 9243
diff changeset
   388
        java.util.Vector<Driver> result = new java.util.Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   394
        // Walk through the loaded registeredDrivers.
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   395
        for(DriverInfo aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // skip it.
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   398
            if(isDriverAllowed(aDriver.driver, callerCL)) {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   399
                result.addElement(aDriver.driver);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   400
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   401
                println("    skipping: " + aDriver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return (result.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Sets the maximum time in seconds that a driver will wait
15278
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   410
     * while attempting to connect to a database once the driver has
e081d3f73b75 8005080: JDBC 4.2 Core changes
lancea
parents: 14174
diff changeset
   411
     * been identified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param seconds the login time limit in seconds; zero means there is no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see #getLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public static void setLoginTimeout(int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        loginTimeout = seconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Gets the maximum time in seconds that a driver can wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * when attempting to log in to a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return the driver login time limit in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @see #setLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public static int getLoginTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return (loginTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Sets the logging/tracing PrintStream that is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param out the new logging/tracing PrintStream; to disable, set to <code>null</code>
14174
74a7d8795c22 8000687: Correct javadoc typo for getLogWriter and setLogWriter
lancea
parents: 14171
diff changeset
   443
     * @deprecated Use {@code setLogWriter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *    <code>checkPermission</code> method denies setting the log stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @see #getLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
14171
94eb36844bd7 7197395: Add @Deprecated to all deprecated methods to eliminate compiler warnings in JDBC
lancea
parents: 13660
diff changeset
   450
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public static void setLogStream(java.io.PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        logStream = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if ( out != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            logWriter = new java.io.PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            logWriter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Retrieves the logging/tracing PrintStream that is used by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return the logging/tracing PrintStream; if disabled, is <code>null</code>
14174
74a7d8795c22 8000687: Correct javadoc typo for getLogWriter and setLogWriter
lancea
parents: 14171
diff changeset
   470
     * @deprecated  Use {@code getLogWriter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see #setLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
14171
94eb36844bd7 7197395: Add @Deprecated to all deprecated methods to eliminate compiler warnings in JDBC
lancea
parents: 13660
diff changeset
   473
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public static java.io.PrintStream getLogStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return logStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Prints a message to the current JDBC log stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param message a log or tracing message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public static void println(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        synchronized (logSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (logWriter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                logWriter.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // automatic flushing is never enabled, so we must do it ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                logWriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    //------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
12885
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   496
    // Internal method used to get the caller's class loader.
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   497
    // Replaces the call to the native method
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   498
    private static ClassLoader getCallerClassLoader() {
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   499
        Class<?> cc = Reflection.getCallerClass(3);
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   500
        ClassLoader cl = (cc != null) ? cc.getClassLoader() : null;
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   501
        return cl;
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   502
    }
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   503
30a5ef357cf1 7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents: 11129
diff changeset
   504
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   505
    // Indicates whether the class object that would be created if the code calling
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   506
    // DriverManager is accessible.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   507
    private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   508
        boolean result = false;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   509
        if(driver != null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   510
            Class<?> aClass = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   511
            try {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   512
                aClass =  Class.forName(driver.getClass().getName(), true, classLoader);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   513
            } catch (Exception ex) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   514
                result = false;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   515
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   517
             result = ( aClass == driver.getClass() ) ? true : false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   520
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private static void loadInitialDrivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        String drivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        try {
9243
d7a77bce0742 7034656: Address lint warnings for DriverManager
lancea
parents: 9239
diff changeset
   526
            drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
d7a77bce0742 7034656: Address lint warnings for DriverManager
lancea
parents: 9239
diff changeset
   527
                public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    return System.getProperty("jdbc.drivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // If the driver is packaged as a Service Provider, load it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // Get all the drivers through the classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // exposed as a java.sql.Driver.class service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // ServiceLoader.load() replaces the sun.misc.Providers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
9243
d7a77bce0742 7034656: Address lint warnings for DriverManager
lancea
parents: 9239
diff changeset
   539
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
d7a77bce0742 7034656: Address lint warnings for DriverManager
lancea
parents: 9239
diff changeset
   540
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
13660
bf59eb142ae2 7193683: DriverManager Iterator Warning cleanup
lancea
parents: 12885
diff changeset
   543
                Iterator<Driver> driversIterator = loadedDrivers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                /* Load these drivers, so that they can be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                 * It may be the case that the driver class may not be there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                 * i.e. there may be a packaged driver with the service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                 * as implementation of java.sql.Driver but the actual class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                 * may be missing. In that case a java.util.ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                 * will be thrown at runtime by the VM trying to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                 * and load the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                 * Adding a try catch block to catch those runtime errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                 * if driver not available in classpath but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                 * packaged as service and that service is there in classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    while(driversIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        println(" Loading done by the java.util.ServiceLoader :  "+driversIterator.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                } catch(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                // Do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        println("DriverManager.initialize: jdbc.drivers = " + drivers);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   569
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   570
        if (drivers == null || drivers.equals("")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   573
        String[] driversList = drivers.split(":");
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   574
        println("number of Drivers:" + driversList.length);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   575
        for (String aDriver : driversList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            try {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   577
                println("DriverManager.Initialize: loading " + aDriver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   578
                Class.forName(aDriver, true,
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   579
                        ClassLoader.getSystemClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                println("DriverManager.Initialize: load failed: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    //  Worker method called by the public getConnection() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static Connection getConnection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * When callerCl is null, we should check the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * (which is invoking this class indirectly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * classloader, so that the JDBC driver class outside rt.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * can be loaded from here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        synchronized(DriverManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
          // synchronize loading of the correct classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
          if(callerCL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
              callerCL = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if(url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new SQLException("The url cannot be null", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        println("DriverManager.getConnection(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   609
        // Walk through the loaded registeredDrivers attempting to make a connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        // Remember the first exception that gets raised so we can reraise it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        SQLException reason = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   613
        for(DriverInfo aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // skip it.
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   616
            if(isDriverAllowed(aDriver.driver, callerCL)) {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   617
                try {
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   618
                    println("    trying " + aDriver.driver.getClass().getName());
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   619
                    Connection con = aDriver.driver.connect(url, info);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   620
                    if (con != null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   621
                        // Success!
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   622
                        println("getConnection returning " + aDriver.driver.getClass().getName());
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   623
                        return (con);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   624
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   625
                } catch (SQLException ex) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   626
                    if (reason == null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   627
                        reason = ex;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   628
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   629
                }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   630
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   631
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   632
                println("    skipping: " + aDriver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   634
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // if we got here nobody could connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (reason != null)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            println("getConnection failed: " + reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            throw reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        println("getConnection: no suitable driver found for "+ url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        throw new SQLException("No suitable driver found for "+ url, "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
}
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   649
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   650
/*
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   651
 * Wrapper class for registered Drivers in order to not expose Driver.equals()
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   652
 * to avoid the capture of the Driver it being compared to as it might not
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   653
 * normally have access.
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   654
 */
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   655
class DriverInfo {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   656
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   657
    final Driver driver;
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   658
    DriverInfo(Driver driver) {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   659
        this.driver = driver;
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   660
    }
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   661
11129
f9ad1aadf3fa 7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents: 9243
diff changeset
   662
    @Override
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   663
    public boolean equals(Object other) {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   664
        return (other instanceof DriverInfo)
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   665
                && this.driver == ((DriverInfo) other).driver;
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   666
    }
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   667
11129
f9ad1aadf3fa 7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents: 9243
diff changeset
   668
    @Override
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   669
    public int hashCode() {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   670
        return driver.hashCode();
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   671
    }
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   672
11129
f9ad1aadf3fa 7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents: 9243
diff changeset
   673
    @Override
9239
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   674
    public String toString() {
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   675
        return ("driver[className="  + driver + "]");
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   676
    }
f4bedba1a211 7034471: Wrap registeredDrivers in DriverManager
lancea
parents: 8797
diff changeset
   677
}