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    
018    package org.apache.commons.dbcp;
019    import java.sql.Connection;
020    import java.sql.DriverManager;
021    import java.sql.SQLException;
022    import 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: 892307 $ $Date: 2013-12-31 23:27:28 +0000 (Tue, 31 Dec 2013) $
031     */
032    public 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         * @param props a list of arbitrary string tag/value pairs as
049         * connection arguments; normally at least a "user" and "password" 
050         * property should be included.
051         */
052        public DriverManagerConnectionFactory(String connectUri, Properties props) {
053            _connectUri = connectUri;
054            _props = props;
055        }
056    
057        /**
058         * Constructor for DriverManagerConnectionFactory.
059         * @param connectUri a database url of the form 
060         * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
061         * @param uname the database user
062         * @param passwd the user's password
063         */
064        public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) {
065            _connectUri = connectUri;
066            _uname = uname;
067            _passwd = passwd;
068        }
069    
070        public Connection createConnection() throws SQLException {
071            if(null == _props) {
072                if((_uname == null) && (_passwd == null)) {
073                    return DriverManager.getConnection(_connectUri);
074                } else {
075                    return DriverManager.getConnection(_connectUri,_uname,_passwd);
076                }
077            } else {
078                return DriverManager.getConnection(_connectUri,_props);
079            }
080        }
081    
082        protected String _connectUri = null;
083        protected String _uname = null;
084        protected String _passwd = null;
085        protected Properties _props = null;
086    }