001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.datasource; 018 019import java.sql.Connection; 020import java.sql.DriverManager; 021import java.sql.SQLException; 022import java.util.Properties; 023 024import org.springframework.util.Assert; 025import org.springframework.util.ClassUtils; 026 027/** 028 * Simple implementation of the standard JDBC {@link javax.sql.DataSource} interface, 029 * configuring the plain old JDBC {@link java.sql.DriverManager} via bean properties, and 030 * returning a new {@link java.sql.Connection} from every {@code getConnection} call. 031 * 032 * <p><b>NOTE: This class is not an actual connection pool; it does not actually 033 * pool Connections.</b> It just serves as simple replacement for a full-blown 034 * connection pool, implementing the same standard interface, but creating new 035 * Connections on every call. 036 * 037 * <p>Useful for test or standalone environments outside of a Java EE container, either 038 * as a DataSource bean in a corresponding ApplicationContext or in conjunction with 039 * a simple JNDI environment. Pool-assuming {@code Connection.close()} calls will 040 * simply close the Connection, so any DataSource-aware persistence code should work. 041 * 042 * <p><b>NOTE: Within special class loading environments such as OSGi, this class 043 * is effectively superseded by {@link SimpleDriverDataSource} due to general class 044 * loading issues with the JDBC DriverManager that be resolved through direct Driver 045 * usage (which is exactly what SimpleDriverDataSource does).</b> 046 * 047 * <p>In a Java EE container, it is recommended to use a JNDI DataSource provided by 048 * the container. Such a DataSource can be exposed as a DataSource bean in a Spring 049 * ApplicationContext via {@link org.springframework.jndi.JndiObjectFactoryBean}, 050 * for seamless switching to and from a local DataSource bean like this class. 051 * For tests, you can then either set up a mock JNDI environment through Spring's 052 * {@link org.springframework.mock.jndi.SimpleNamingContextBuilder}, or switch the 053 * bean definition to a local DataSource (which is simpler and thus recommended). 054 * 055 * <p>This {@code DriverManagerDataSource} class was originally designed alongside 056 * <a href="https://commons.apache.org/proper/commons-dbcp">Apache Commons DBCP</a> 057 * and <a href="https://sourceforge.net/projects/c3p0">C3P0</a>, featuring bean-style 058 * {@code BasicDataSource}/{@code ComboPooledDataSource} classes with configuration 059 * properties for local resource setups. For a modern JDBC connection pool, consider 060 * <a href="https://github.com/brettwooldridge/HikariCP">HikariCP</a> instead, 061 * exposing a corresponding {@code HikariDataSource} instance to the application. 062 * 063 * @author Juergen Hoeller 064 * @since 14.03.2003 065 * @see SimpleDriverDataSource 066 */ 067public class DriverManagerDataSource extends AbstractDriverBasedDataSource { 068 069 /** 070 * Constructor for bean-style configuration. 071 */ 072 public DriverManagerDataSource() { 073 } 074 075 /** 076 * Create a new DriverManagerDataSource with the given JDBC URL, 077 * not specifying a username or password for JDBC access. 078 * @param url the JDBC URL to use for accessing the DriverManager 079 * @see java.sql.DriverManager#getConnection(String) 080 */ 081 public DriverManagerDataSource(String url) { 082 setUrl(url); 083 } 084 085 /** 086 * Create a new DriverManagerDataSource with the given standard 087 * DriverManager parameters. 088 * @param url the JDBC URL to use for accessing the DriverManager 089 * @param username the JDBC username to use for accessing the DriverManager 090 * @param password the JDBC password to use for accessing the DriverManager 091 * @see java.sql.DriverManager#getConnection(String, String, String) 092 */ 093 public DriverManagerDataSource(String url, String username, String password) { 094 setUrl(url); 095 setUsername(username); 096 setPassword(password); 097 } 098 099 /** 100 * Create a new DriverManagerDataSource with the given JDBC URL, 101 * not specifying a username or password for JDBC access. 102 * @param url the JDBC URL to use for accessing the DriverManager 103 * @param conProps the JDBC connection properties 104 * @see java.sql.DriverManager#getConnection(String) 105 */ 106 public DriverManagerDataSource(String url, Properties conProps) { 107 setUrl(url); 108 setConnectionProperties(conProps); 109 } 110 111 112 /** 113 * Set the JDBC driver class name. This driver will get initialized 114 * on startup, registering itself with the JDK's DriverManager. 115 * <p><b>NOTE: DriverManagerDataSource is primarily intended for accessing 116 * <i>pre-registered</i> JDBC drivers.</b> If you need to register a new driver, 117 * consider using {@link SimpleDriverDataSource} instead. Alternatively, consider 118 * initializing the JDBC driver yourself before instantiating this DataSource. 119 * The "driverClassName" property is mainly preserved for backwards compatibility, 120 * as well as for migrating between Commons DBCP and this DataSource. 121 * @see java.sql.DriverManager#registerDriver(java.sql.Driver) 122 * @see SimpleDriverDataSource 123 */ 124 public void setDriverClassName(String driverClassName) { 125 Assert.hasText(driverClassName, "Property 'driverClassName' must not be empty"); 126 String driverClassNameToUse = driverClassName.trim(); 127 try { 128 Class.forName(driverClassNameToUse, true, ClassUtils.getDefaultClassLoader()); 129 } 130 catch (ClassNotFoundException ex) { 131 throw new IllegalStateException("Could not load JDBC driver class [" + driverClassNameToUse + "]", ex); 132 } 133 if (logger.isDebugEnabled()) { 134 logger.debug("Loaded JDBC driver: " + driverClassNameToUse); 135 } 136 } 137 138 139 @Override 140 protected Connection getConnectionFromDriver(Properties props) throws SQLException { 141 String url = getUrl(); 142 Assert.state(url != null, "'url' not set"); 143 if (logger.isDebugEnabled()) { 144 logger.debug("Creating new JDBC DriverManager Connection to [" + url + "]"); 145 } 146 return getConnectionFromDriverManager(url, props); 147 } 148 149 /** 150 * Getting a Connection using the nasty static from DriverManager is extracted 151 * into a protected method to allow for easy unit testing. 152 * @see java.sql.DriverManager#getConnection(String, java.util.Properties) 153 */ 154 protected Connection getConnectionFromDriverManager(String url, Properties props) throws SQLException { 155 return DriverManager.getConnection(url, props); 156 } 157 158}