DriverManagerConnectionFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2;

  18. import java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.SQLException;
  21. import java.util.Properties;

  22. /**
  23.  * A {@link DriverManager}-based implementation of {@link ConnectionFactory}.
  24.  *
  25.  * @since 2.0
  26.  */
  27. public class DriverManagerConnectionFactory implements ConnectionFactory {

  28.     static {
  29.         // Related to DBCP-212
  30.         // Driver manager does not sync loading of drivers that use the service
  31.         // provider interface. This will cause issues is multi-threaded
  32.         // environments. This hack makes sure the drivers are loaded before
  33.         // DBCP tries to use them.
  34.         DriverManager.getDrivers();
  35.     }

  36.     private final String connectionUri;

  37.     private final String userName;

  38.     private final char[] userPassword;

  39.     private final Properties properties;

  40.     /**
  41.      * Constructor for DriverManagerConnectionFactory.
  42.      *
  43.      * @param connectionUri
  44.      *            a database connection string of the form {@code  jdbc:<em>subprotocol</em>:<em>subname</em>}
  45.      * @since 2.2
  46.      */
  47.     public DriverManagerConnectionFactory(final String connectionUri) {
  48.         this.connectionUri = connectionUri;
  49.         this.properties = new Properties();
  50.         this.userName = null;
  51.         this.userPassword = null;
  52.     }

  53.     /**
  54.      * Constructor for DriverManagerConnectionFactory.
  55.      *
  56.      * @param connectionUri
  57.      *            a database connection string of the form {@code  jdbc:<em>subprotocol</em>:<em>subname</em>}
  58.      * @param properties
  59.      *            a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and
  60.      *            "password" property should be included.
  61.      */
  62.     public DriverManagerConnectionFactory(final String connectionUri, final Properties properties) {
  63.         this.connectionUri = connectionUri;
  64.         this.properties = properties;
  65.         this.userName = null;
  66.         this.userPassword = null;
  67.     }

  68.     /**
  69.      * Constructor for DriverManagerConnectionFactory.
  70.      *
  71.      * @param connectionUri
  72.      *            a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>}
  73.      * @param userName
  74.      *            the database user
  75.      * @param userPassword
  76.      *            the user's password
  77.      */
  78.     public DriverManagerConnectionFactory(final String connectionUri, final String userName,
  79.             final char[] userPassword) {
  80.         this.connectionUri = connectionUri;
  81.         this.userName = userName;
  82.         this.userPassword = Utils.clone(userPassword);
  83.         this.properties = null;
  84.     }

  85.     /**
  86.      * Constructor for DriverManagerConnectionFactory.
  87.      *
  88.      * @param connectionUri
  89.      *            a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>}
  90.      * @param userName
  91.      *            the database user
  92.      * @param userPassword
  93.      *            the user's password
  94.      */
  95.     public DriverManagerConnectionFactory(final String connectionUri, final String userName,
  96.             final String userPassword) {
  97.         this.connectionUri = connectionUri;
  98.         this.userName = userName;
  99.         this.userPassword =  Utils.toCharArray(userPassword);
  100.         this.properties = null;
  101.     }

  102.     @Override
  103.     public Connection createConnection() throws SQLException {
  104.         if (null == properties) {
  105.             if (userName == null && userPassword == null) {
  106.                 return DriverManager.getConnection(connectionUri);
  107.             }
  108.             return DriverManager.getConnection(connectionUri, userName, Utils.toString(userPassword));
  109.         }
  110.         return DriverManager.getConnection(connectionUri, properties);
  111.     }

  112.     /**
  113.      * @return The connection URI.
  114.      * @since 2.6.0
  115.      */
  116.     public String getConnectionUri() {
  117.         return connectionUri;
  118.     }

  119.     /**
  120.      * @return The Properties.
  121.      * @since 2.6.0
  122.      */
  123.     public Properties getProperties() {
  124.         return properties;
  125.     }

  126.     /**
  127.      * @return The user name.
  128.      * @since 2.6.0
  129.      */
  130.     public String getUserName() {
  131.         return userName;
  132.     }
  133. }