001/*
002 * Copyright 2002-2017 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.util.Assert;
026
027/**
028 * Simple implementation of the standard JDBC {@link javax.sql.DataSource} interface,
029 * configuring a plain old JDBC {@link java.sql.Driver} 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>In a Java EE container, it is recommended to use a JNDI DataSource provided by
038 * the container. Such a DataSource can be exposed as a DataSource bean in a Spring
039 * ApplicationContext via {@link org.springframework.jndi.JndiObjectFactoryBean},
040 * for seamless switching to and from a local DataSource bean like this class.
041 *
042 * <p>If you need a "real" connection pool outside of a Java EE container, consider
043 * <a href="https://commons.apache.org/proper/commons-dbcp">Apache Commons DBCP</a>
044 * or <a href="https://sourceforge.net/projects/c3p0">C3P0</a>.
045 * Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full
046 * connection pool beans, supporting the same basic properties as this class
047 * plus specific settings (such as minimal/maximal pool size etc).
048 *
049 * @author Juergen Hoeller
050 * @since 2.5.5
051 * @see DriverManagerDataSource
052 */
053public class SimpleDriverDataSource extends AbstractDriverBasedDataSource {
054
055        private Driver driver;
056
057
058        /**
059         * Constructor for bean-style configuration.
060         */
061        public SimpleDriverDataSource() {
062        }
063
064        /**
065         * Create a new DriverManagerDataSource with the given standard Driver parameters.
066         * @param driver the JDBC Driver object
067         * @param url the JDBC URL to use for accessing the DriverManager
068         * @see java.sql.Driver#connect(String, java.util.Properties)
069         */
070        public SimpleDriverDataSource(Driver driver, String url) {
071                setDriver(driver);
072                setUrl(url);
073        }
074
075        /**
076         * Create a new DriverManagerDataSource with the given standard Driver parameters.
077         * @param driver the JDBC Driver object
078         * @param url the JDBC URL to use for accessing the DriverManager
079         * @param username the JDBC username to use for accessing the DriverManager
080         * @param password the JDBC password to use for accessing the DriverManager
081         * @see java.sql.Driver#connect(String, java.util.Properties)
082         */
083        public SimpleDriverDataSource(Driver driver, String url, String username, String password) {
084                setDriver(driver);
085                setUrl(url);
086                setUsername(username);
087                setPassword(password);
088        }
089
090        /**
091         * Create a new DriverManagerDataSource with the given standard Driver parameters.
092         * @param driver the JDBC Driver object
093         * @param url the JDBC URL to use for accessing the DriverManager
094         * @param conProps JDBC connection properties
095         * @see java.sql.Driver#connect(String, java.util.Properties)
096         */
097        public SimpleDriverDataSource(Driver driver, String url, Properties conProps) {
098                setDriver(driver);
099                setUrl(url);
100                setConnectionProperties(conProps);
101        }
102
103
104        /**
105         * Specify the JDBC Driver implementation class to use.
106         * <p>An instance of this Driver class will be created and held
107         * within the SimpleDriverDataSource.
108         * @see #setDriver
109         */
110        public void setDriverClass(Class<? extends Driver> driverClass) {
111                this.driver = BeanUtils.instantiateClass(driverClass);
112        }
113
114        /**
115         * Specify the JDBC Driver instance to use.
116         * <p>This allows for passing in a shared, possibly pre-configured
117         * Driver instance.
118         * @see #setDriverClass
119         */
120        public void setDriver(Driver driver) {
121                this.driver = driver;
122        }
123
124        /**
125         * Return the JDBC Driver instance to use.
126         */
127        public Driver getDriver() {
128                return this.driver;
129        }
130
131
132        @Override
133        protected Connection getConnectionFromDriver(Properties props) throws SQLException {
134                Driver driver = getDriver();
135                String url = getUrl();
136                Assert.notNull(driver, "Driver must not be null");
137                if (logger.isDebugEnabled()) {
138                        logger.debug("Creating new JDBC Driver Connection to [" + url + "]");
139                }
140                return driver.connect(url, props);
141        }
142
143}