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