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; 019 020import java.sql.Connection; 021import java.sql.DriverManager; 022import java.sql.SQLException; 023import java.util.Properties; 024 025/** 026 * A {@link DriverManager}-based implementation of {@link ConnectionFactory}. 027 * 028 * @since 2.0 029 */ 030public class DriverManagerConnectionFactory implements ConnectionFactory { 031 032 static { 033 // Related to DBCP-212 034 // Driver manager does not sync loading of drivers that use the service 035 // provider interface. This will cause issues is multi-threaded 036 // environments. This hack makes sure the drivers are loaded before 037 // DBCP tries to use them. 038 DriverManager.getDrivers(); 039 } 040 041 /** 042 * Constructor for DriverManagerConnectionFactory. 043 * 044 * @param connectionUri 045 * a database url of the form <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> 046 * @since 2.2 047 */ 048 public DriverManagerConnectionFactory(final String connectionUri) { 049 this.connectionUri = connectionUri; 050 this.propeties = new Properties(); 051 } 052 053 /** 054 * Constructor for DriverManagerConnectionFactory. 055 * 056 * @param connectionUri 057 * a database url of the form <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> 058 * @param properties 059 * a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and 060 * "password" property should be included. 061 */ 062 public DriverManagerConnectionFactory(final String connectionUri, final Properties properties) { 063 this.connectionUri = connectionUri; 064 this.propeties = properties; 065 } 066 067 /** 068 * Constructor for DriverManagerConnectionFactory. 069 * 070 * @param connectionUri 071 * a database url of the form <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> 072 * @param userName 073 * the database user 074 * @param userPassword 075 * the user's password 076 */ 077 public DriverManagerConnectionFactory(final String connectionUri, final String userName, 078 final String userPassword) { 079 this.connectionUri = connectionUri; 080 this.userName = userName; 081 this.userPassword = userPassword; 082 } 083 084 @Override 085 public Connection createConnection() throws SQLException { 086 if (null == propeties) { 087 if (userName == null && userPassword == null) { 088 return DriverManager.getConnection(connectionUri); 089 } 090 return DriverManager.getConnection(connectionUri, userName, userPassword); 091 } 092 return DriverManager.getConnection(connectionUri, propeties); 093 } 094 095 private final String connectionUri; 096 private String userName; 097 private String userPassword; 098 private Properties propeties; 099}