| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DriverManagerConnectionFactory |
|
| 2.6666666666666665;2.667 |
| 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 | ||
| 18 | package org.apache.commons.dbcp; | |
| 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 | * @author Rodney Waldhoff | |
| 28 | * @author Ignacio J. Ortega | |
| 29 | * @author Dirk Verbeeck | |
| 30 | * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $ | |
| 31 | */ | |
| 32 | public class DriverManagerConnectionFactory implements ConnectionFactory { | |
| 33 | ||
| 34 | static { | |
| 35 | // Related to DBCP-212 | |
| 36 | // Driver manager does not sync loading of drivers that use the service | |
| 37 | // provider interface. This will cause issues is multi-threaded | |
| 38 | // environments. This hack makes sure the drivers are loaded before | |
| 39 | // DBCP tries to use them. | |
| 40 | 2 | DriverManager.getDrivers(); |
| 41 | 2 | } |
| 42 | ||
| 43 | ||
| 44 | /** | |
| 45 | * Constructor for DriverManagerConnectionFactory. | |
| 46 | * @param connectUri a database url of the form | |
| 47 | * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> | |
| 48 | * @param props a list of arbitrary string tag/value pairs as | |
| 49 | * connection arguments; normally at least a "user" and "password" | |
| 50 | * property should be included. | |
| 51 | */ | |
| 52 | 0 | public DriverManagerConnectionFactory(String connectUri, Properties props) { |
| 53 | 0 | _connectUri = connectUri; |
| 54 | 0 | _props = props; |
| 55 | 0 | } |
| 56 | ||
| 57 | /** | |
| 58 | * Constructor for DriverManagerConnectionFactory. | |
| 59 | * @param connectUri a database url of the form | |
| 60 | * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> | |
| 61 | * @param uname the database user | |
| 62 | * @param passwd the user's password | |
| 63 | */ | |
| 64 | 14 | public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) { |
| 65 | 14 | _connectUri = connectUri; |
| 66 | 14 | _uname = uname; |
| 67 | 14 | _passwd = passwd; |
| 68 | 14 | } |
| 69 | ||
| 70 | public Connection createConnection() throws SQLException { | |
| 71 | 76 | if(null == _props) { |
| 72 | 76 | if((_uname == null) && (_passwd == null)) { |
| 73 | 0 | return DriverManager.getConnection(_connectUri); |
| 74 | } else { | |
| 75 | 76 | return DriverManager.getConnection(_connectUri,_uname,_passwd); |
| 76 | } | |
| 77 | } else { | |
| 78 | 0 | return DriverManager.getConnection(_connectUri,_props); |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | 14 | protected String _connectUri = null; |
| 83 | 14 | protected String _uname = null; |
| 84 | 14 | protected String _passwd = null; |
| 85 | 14 | protected Properties _props = null; |
| 86 | } |