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 19 import java.sql.Connection; 20 import java.sql.DriverManager; 21 import java.sql.SQLException; 22 import java.util.Properties; 23 24 /** 25 * A {@link DriverManager}-based implementation of {@link ConnectionFactory}. 26 * 27 * @since 2.0 28 */ 29 public class DriverManagerConnectionFactory implements ConnectionFactory { 30 31 static { 32 // Related to DBCP-212 33 // Driver manager does not sync loading of drivers that use the service 34 // provider interface. This will cause issues is multi-threaded 35 // environments. This hack makes sure the drivers are loaded before 36 // DBCP tries to use them. 37 DriverManager.getDrivers(); 38 } 39 40 private final String connectionUri; 41 42 private final String userName; 43 44 private final char[] userPassword; 45 46 private final Properties properties; 47 48 /** 49 * Constructor for DriverManagerConnectionFactory. 50 * 51 * @param connectionUri 52 * a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>} 53 * @since 2.2 54 */ 55 public DriverManagerConnectionFactory(final String connectionUri) { 56 this.connectionUri = connectionUri; 57 this.properties = new Properties(); 58 this.userName = null; 59 this.userPassword = null; 60 } 61 62 /** 63 * Constructor for DriverManagerConnectionFactory. 64 * 65 * @param connectionUri 66 * a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>} 67 * @param properties 68 * a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and 69 * "password" property should be included. 70 */ 71 public DriverManagerConnectionFactory(final String connectionUri, final Properties properties) { 72 this.connectionUri = connectionUri; 73 this.properties = properties; 74 this.userName = null; 75 this.userPassword = null; 76 } 77 78 /** 79 * Constructor for DriverManagerConnectionFactory. 80 * 81 * @param connectionUri 82 * a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>} 83 * @param userName 84 * the database user 85 * @param userPassword 86 * the user's password 87 */ 88 public DriverManagerConnectionFactory(final String connectionUri, final String userName, 89 final char[] userPassword) { 90 this.connectionUri = connectionUri; 91 this.userName = userName; 92 this.userPassword = Utils.clone(userPassword); 93 this.properties = null; 94 } 95 96 /** 97 * Constructor for DriverManagerConnectionFactory. 98 * 99 * @param connectionUri 100 * a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>} 101 * @param userName 102 * the database user 103 * @param userPassword 104 * the user's password 105 */ 106 public DriverManagerConnectionFactory(final String connectionUri, final String userName, 107 final String userPassword) { 108 this.connectionUri = connectionUri; 109 this.userName = userName; 110 this.userPassword = Utils.toCharArray(userPassword); 111 this.properties = null; 112 } 113 114 @Override 115 public Connection createConnection() throws SQLException { 116 if (null == properties) { 117 if (userName == null && userPassword == null) { 118 return DriverManager.getConnection(connectionUri); 119 } 120 return DriverManager.getConnection(connectionUri, userName, Utils.toString(userPassword)); 121 } 122 return DriverManager.getConnection(connectionUri, properties); 123 } 124 125 /** 126 * @return The connection URI. 127 * @since 2.6.0 128 */ 129 public String getConnectionUri() { 130 return connectionUri; 131 } 132 133 /** 134 * @return The Properties. 135 * @since 2.6.0 136 */ 137 public Properties getProperties() { 138 return properties; 139 } 140 141 /** 142 * @return The user name. 143 * @since 2.6.0 144 */ 145 public String getUserName() { 146 return userName; 147 } 148 }