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.Driver;
021import java.sql.SQLException;
022import java.util.Properties;
023
024import org.springframework.beans.BeanUtils;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * Simple implementation of the standard JDBC {@link javax.sql.DataSource} interface,
030 * configuring a plain old JDBC {@link java.sql.Driver} via bean properties, and
031 * returning a new {@link java.sql.Connection} from every {@code getConnection} call.
032 *
033 * <p><b>NOTE: This class is not an actual connection pool; it does not actually
034 * pool Connections.</b> It just serves as simple replacement for a full-blown
035 * connection pool, implementing the same standard interface, but creating new
036 * Connections on every call.
037 *
038 * <p>In a Java EE container, it is recommended to use a JNDI DataSource provided by
039 * the container. Such a DataSource can be exposed as a DataSource bean in a Spring
040 * ApplicationContext via {@link org.springframework.jndi.JndiObjectFactoryBean},
041 * for seamless switching to and from a local DataSource bean like this class.
042 *
043 * <p>This {@code SimpleDriverDataSource} class was originally designed alongside
044 * <a href="https://commons.apache.org/proper/commons-dbcp">Apache Commons DBCP</a>
045 * and <a href="https://sourceforge.net/projects/c3p0">C3P0</a>, featuring bean-style
046 * {@code BasicDataSource}/{@code ComboPooledDataSource} classes with configuration
047 * properties for local resource setups. For a modern JDBC connection pool, consider
048 * <a href="https://github.com/brettwooldridge/HikariCP">HikariCP</a> instead,
049 * exposing a corresponding {@code HikariDataSource} instance to the application.
050 *
051 * @author Juergen Hoeller
052 * @since 2.5.5
053 * @see DriverManagerDataSource
054 */
055public class SimpleDriverDataSource extends AbstractDriverBasedDataSource {
056
057        @Nullable
058        private Driver driver;
059
060
061        /**
062         * Constructor for bean-style configuration.
063         */
064        public SimpleDriverDataSource() {
065        }
066
067        /**
068         * Create a new DriverManagerDataSource with the given standard Driver parameters.
069         * @param driver the JDBC Driver object
070         * @param url the JDBC URL to use for accessing the DriverManager
071         * @see java.sql.Driver#connect(String, java.util.Properties)
072         */
073        public SimpleDriverDataSource(Driver driver, String url) {
074                setDriver(driver);
075                setUrl(url);
076        }
077
078        /**
079         * Create a new DriverManagerDataSource with the given standard Driver parameters.
080         * @param driver the JDBC Driver object
081         * @param url the JDBC URL to use for accessing the DriverManager
082         * @param username the JDBC username to use for accessing the DriverManager
083         * @param password the JDBC password to use for accessing the DriverManager
084         * @see java.sql.Driver#connect(String, java.util.Properties)
085         */
086        public SimpleDriverDataSource(Driver driver, String url, String username, String password) {
087                setDriver(driver);
088                setUrl(url);
089                setUsername(username);
090                setPassword(password);
091        }
092
093        /**
094         * Create a new DriverManagerDataSource with the given standard Driver parameters.
095         * @param driver the JDBC Driver object
096         * @param url the JDBC URL to use for accessing the DriverManager
097         * @param conProps the JDBC connection properties
098         * @see java.sql.Driver#connect(String, java.util.Properties)
099         */
100        public SimpleDriverDataSource(Driver driver, String url, Properties conProps) {
101                setDriver(driver);
102                setUrl(url);
103                setConnectionProperties(conProps);
104        }
105
106
107        /**
108         * Specify the JDBC Driver implementation class to use.
109         * <p>An instance of this Driver class will be created and held
110         * within the SimpleDriverDataSource.
111         * @see #setDriver
112         */
113        public void setDriverClass(Class<? extends Driver> driverClass) {
114                this.driver = BeanUtils.instantiateClass(driverClass);
115        }
116
117        /**
118         * Specify the JDBC Driver instance to use.
119         * <p>This allows for passing in a shared, possibly pre-configured
120         * Driver instance.
121         * @see #setDriverClass
122         */
123        public void setDriver(@Nullable Driver driver) {
124                this.driver = driver;
125        }
126
127        /**
128         * Return the JDBC Driver instance to use.
129         */
130        @Nullable
131        public Driver getDriver() {
132                return this.driver;
133        }
134
135
136        @Override
137        protected Connection getConnectionFromDriver(Properties props) throws SQLException {
138                Driver driver = getDriver();
139                String url = getUrl();
140                Assert.notNull(driver, "Driver must not be null");
141                if (logger.isDebugEnabled()) {
142                        logger.debug("Creating new JDBC Driver Connection to [" + url + "]");
143                }
144                return driver.connect(url, props);
145        }
146
147}