001/* 002 * Copyright 2002-2015 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 J2EE 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 J2EE 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>If you need a "real" connection pool outside of a J2EE container, consider 056 * <a href="https://commons.apache.org/proper/commons-dbcp">Apache Commons DBCP</a> 057 * or <a href="https://sourceforge.net/projects/c3p0">C3P0</a>. 058 * Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full 059 * connection pool beans, supporting the same basic properties as this class 060 * plus specific settings (such as minimal/maximal pool size etc). 061 * 062 * @author Juergen Hoeller 063 * @since 14.03.2003 064 * @see SimpleDriverDataSource 065 */ 066public class DriverManagerDataSource extends AbstractDriverBasedDataSource { 067 068 /** 069 * Constructor for bean-style configuration. 070 */ 071 public DriverManagerDataSource() { 072 } 073 074 /** 075 * Create a new DriverManagerDataSource with the given JDBC URL, 076 * not specifying a username or password for JDBC access. 077 * @param url the JDBC URL to use for accessing the DriverManager 078 * @see java.sql.DriverManager#getConnection(String) 079 */ 080 public DriverManagerDataSource(String url) { 081 setUrl(url); 082 } 083 084 /** 085 * Create a new DriverManagerDataSource with the given standard 086 * DriverManager parameters. 087 * @param url the JDBC URL to use for accessing the DriverManager 088 * @param username the JDBC username to use for accessing the DriverManager 089 * @param password the JDBC password to use for accessing the DriverManager 090 * @see java.sql.DriverManager#getConnection(String, String, String) 091 */ 092 public DriverManagerDataSource(String url, String username, String password) { 093 setUrl(url); 094 setUsername(username); 095 setPassword(password); 096 } 097 098 /** 099 * Create a new DriverManagerDataSource with the given JDBC URL, 100 * not specifying a username or password for JDBC access. 101 * @param url the JDBC URL to use for accessing the DriverManager 102 * @param conProps JDBC connection properties 103 * @see java.sql.DriverManager#getConnection(String) 104 */ 105 public DriverManagerDataSource(String url, Properties conProps) { 106 setUrl(url); 107 setConnectionProperties(conProps); 108 } 109 110 111 /** 112 * Set the JDBC driver class name. This driver will get initialized 113 * on startup, registering itself with the JDK's DriverManager. 114 * <p><b>NOTE: DriverManagerDataSource is primarily intended for accessing 115 * <i>pre-registered</i> JDBC drivers.</b> If you need to register a new driver, 116 * consider using {@link SimpleDriverDataSource} instead. Alternatively, consider 117 * initializing the JDBC driver yourself before instantiating this DataSource. 118 * The "driverClassName" property is mainly preserved for backwards compatibility, 119 * as well as for migrating between Commons DBCP and this DataSource. 120 * @see java.sql.DriverManager#registerDriver(java.sql.Driver) 121 * @see SimpleDriverDataSource 122 */ 123 public void setDriverClassName(String driverClassName) { 124 Assert.hasText(driverClassName, "Property 'driverClassName' must not be empty"); 125 String driverClassNameToUse = driverClassName.trim(); 126 try { 127 Class.forName(driverClassNameToUse, true, ClassUtils.getDefaultClassLoader()); 128 } 129 catch (ClassNotFoundException ex) { 130 throw new IllegalStateException("Could not load JDBC driver class [" + driverClassNameToUse + "]", ex); 131 } 132 if (logger.isInfoEnabled()) { 133 logger.info("Loaded JDBC driver: " + driverClassNameToUse); 134 } 135 } 136 137 138 @Override 139 protected Connection getConnectionFromDriver(Properties props) throws SQLException { 140 String url = getUrl(); 141 if (logger.isDebugEnabled()) { 142 logger.debug("Creating new JDBC DriverManager Connection to [" + url + "]"); 143 } 144 return getConnectionFromDriverManager(url, props); 145 } 146 147 /** 148 * Getting a Connection using the nasty static from DriverManager is extracted 149 * into a protected method to allow for easy unit testing. 150 * @see java.sql.DriverManager#getConnection(String, java.util.Properties) 151 */ 152 protected Connection getConnectionFromDriverManager(String url, Properties props) throws SQLException { 153 return DriverManager.getConnection(url, props); 154 } 155 156}