001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.dbcp2; 019import java.sql.Connection; 020import java.sql.DriverManager; 021import java.sql.SQLException; 022import java.util.Properties; 023 024/** 025 * A {@link DriverManager}-based implementation of {@link ConnectionFactory}. 026 * 027 * @author Rodney Waldhoff 028 * @author Ignacio J. Ortega 029 * @author Dirk Verbeeck 030 * @version $Revision: 1572242 $ $Date: 2014-02-26 20:34:39 +0000 (Wed, 26 Feb 2014) $ 031 * @since 2.0 032 */ 033public class DriverManagerConnectionFactory implements ConnectionFactory { 034 035 static { 036 // Related to DBCP-212 037 // Driver manager does not sync loading of drivers that use the service 038 // provider interface. This will cause issues is multi-threaded 039 // environments. This hack makes sure the drivers are loaded before 040 // DBCP tries to use them. 041 DriverManager.getDrivers(); 042 } 043 044 045 /** 046 * Constructor for DriverManagerConnectionFactory. 047 * @param connectUri a database url of the form 048 * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> 049 * @param props a list of arbitrary string tag/value pairs as 050 * connection arguments; normally at least a "user" and "password" 051 * property should be included. 052 */ 053 public DriverManagerConnectionFactory(String connectUri, Properties props) { 054 _connectUri = connectUri; 055 _props = props; 056 } 057 058 /** 059 * Constructor for DriverManagerConnectionFactory. 060 * @param connectUri a database url of the form 061 * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> 062 * @param uname the database user 063 * @param passwd the user's password 064 */ 065 public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) { 066 _connectUri = connectUri; 067 _uname = uname; 068 _passwd = passwd; 069 } 070 071 @Override 072 public Connection createConnection() throws SQLException { 073 if(null == _props) { 074 if(_uname == null && _passwd == null) { 075 return DriverManager.getConnection(_connectUri); 076 } 077 return DriverManager.getConnection(_connectUri,_uname,_passwd); 078 } 079 return DriverManager.getConnection(_connectUri,_props); 080 } 081 082 private String _connectUri = null; 083 private String _uname = null; 084 private String _passwd = null; 085 private Properties _props = null; 086}