src/share/classes/java/sql/DriverManager.java

Print this page

        

@@ -86,24 +86,17 @@
     private static volatile int loginTimeout = 0;
     private static volatile java.io.PrintWriter logWriter = null;
     private static volatile java.io.PrintStream logStream = null;
     // Used in println() to synchronize logWriter
     private final static  Object logSync = new Object();
+    // Used in initDriversIfNeeded() to synchronize driversInitialized
+    private final static  Object driversSync = new Object();
+    private static volatile boolean driversInitialized;
 
     /* Prevent the DriverManager class from being instantiated. */
     private DriverManager(){}
 
-
-    /**
-     * Load the initial JDBC drivers by checking the System property
-     * jdbc.properties and then use the {@code ServiceLoader} mechanism
-     */
-    static {
-        loadInitialDrivers();
-        println("JDBC DriverManager initialized");
-    }
-
     /**
      * The <code>SQLPermission</code> constant that allows the
      * setting of the logging stream.
      * @since 1.3
      */

@@ -285,10 +278,12 @@
     public static Driver getDriver(String url)
         throws SQLException {
 
         println("DriverManager.getDriver(\"" + url + "\")");
 
+        initDriversIfNeeded();
+
         Class<?> callerClass = Reflection.getCallerClass();
 
         // Walk through the loaded registeredDrivers attempting to locate someone
         // who understands the given URL.
         for (DriverInfo aDriver : registeredDrivers) {

@@ -326,11 +321,11 @@
      * @param driver the new JDBC Driver that is to be registered with the
      *               {@code DriverManager}
      * @exception SQLException if a database access error occurs
      * @exception NullPointerException if {@code driver} is null
      */
-    public static synchronized void registerDriver(java.sql.Driver driver)
+    public static void registerDriver(java.sql.Driver driver)
         throws SQLException {
 
         registerDriver(driver, null);
     }
 

@@ -347,11 +342,11 @@
      *               {@code DriverManager#deregisterDriver} is called
      * @exception SQLException if a database access error occurs
      * @exception NullPointerException if {@code driver} is null
      * @since 1.8
      */
-    public static synchronized void registerDriver(java.sql.Driver driver,
+    public static void registerDriver(java.sql.Driver driver,
             DriverAction da)
         throws SQLException {
 
         /* Register the driver if it has not already been added to our list */
         if(driver != null) {

@@ -435,10 +430,12 @@
      */
     @CallerSensitive
     public static java.util.Enumeration<Driver> getDrivers() {
         java.util.Vector<Driver> result = new java.util.Vector<>();
 
+        initDriversIfNeeded();
+
         Class<?> callerClass = Reflection.getCallerClass();
 
         // Walk through the loaded registeredDrivers.
         for(DriverInfo aDriver : registeredDrivers) {
             // If the caller does not have permission to load the driver then

@@ -562,10 +559,30 @@
         }
 
         return result;
     }
 
+    /*
+     * Load the initial JDBC drivers by checking the System property
+     * jdbc.properties and then use the {@code ServiceLoader} mechanism
+    */
+    private static void initDriversIfNeeded() {
+        // Drivers have been inited earleir
+        if (driversInitialized) {
+            return;
+        }
+        
+        synchronized(driversSync) {
+            if (driversInitialized) {
+                return;
+            }
+            loadInitialDrivers();
+            driversInitialized = true;
+            println("JDBC DriverManager initialized");
+        }
+    }
+
     private static void loadInitialDrivers() {
         String drivers;
         try {
             drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
                 public String run() {

@@ -649,10 +666,12 @@
             throw new SQLException("The url cannot be null", "08001");
         }
 
         println("DriverManager.getConnection(\"" + url + "\")");
 
+        initDriversIfNeeded();
+
         // Walk through the loaded registeredDrivers attempting to make a connection.
         // Remember the first exception that gets raised so we can reraise it.
         SQLException reason = null;
 
         for(DriverInfo aDriver : registeredDrivers) {