All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jn.sqlhelper.datasource.driver.SimpleDriverDataSource Maven / Gradle / Ivy

There is a newer version: 3.6.16
Show newest version
/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the LGPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at  http://www.gnu.org/licenses/lgpl-3.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jn.sqlhelper.datasource.driver;


import com.jn.langx.util.Preconditions;
import com.jn.langx.util.reflect.Reflects;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Simple implementation of the standard JDBC {@link javax.sql.DataSource} interface,
 * configuring a plain old JDBC {@link java.sql.Driver} via bean properties, and
 * returning a new {@link java.sql.Connection} from every {@code getConnection} call.
 *
 * 

NOTE: This class is not an actual connection pool; it does not actually * pool Connections. It just serves as simple replacement for a full-blown * connection pool, implementing the same standard interface, but creating new * Connections on every call. * *

In a Java EE container, it is recommended to use a JNDI DataSource provided by * the container. Such a DataSource can be exposed as a DataSource bean in a Spring * ApplicationContext via {@link org.springframework.jndi.JndiObjectFactoryBean}, * for seamless switching to and from a local DataSource bean like this class. * *

If you need a "real" connection pool outside of a Java EE container, consider * Apache Commons DBCP * or C3P0. * Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full * connection pool beans, supporting the same basic properties as this class * plus specific settings (such as minimal/maximal pool size etc). * * @see DriverManagerDataSource * @since 3.4.0 */ public class SimpleDriverDataSource extends AbstractDriverBasedDataSource { private Driver driver; /** * Constructor for bean-style configuration. */ public SimpleDriverDataSource() { } /** * Create a new DriverManagerDataSource with the given standard Driver parameters. * @param driver the JDBC Driver object * @param url the JDBC URL to use for accessing the DriverManager * @see java.sql.Driver#connect(String, java.util.Properties) */ public SimpleDriverDataSource(Driver driver, String url) { setDriver(driver); setUrl(url); } /** * Create a new DriverManagerDataSource with the given standard Driver parameters. * @param driver the JDBC Driver object * @param url the JDBC URL to use for accessing the DriverManager * @param username the JDBC username to use for accessing the DriverManager * @param password the JDBC password to use for accessing the DriverManager * @see java.sql.Driver#connect(String, java.util.Properties) */ public SimpleDriverDataSource(Driver driver, String url, String username, String password) { setDriver(driver); setUrl(url); setUsername(username); setPassword(password); } /** * Create a new DriverManagerDataSource with the given standard Driver parameters. * @param driver the JDBC Driver object * @param url the JDBC URL to use for accessing the DriverManager * @param conProps JDBC connection properties * @see java.sql.Driver#connect(String, java.util.Properties) */ public SimpleDriverDataSource(Driver driver, String url, Properties conProps) { setDriver(driver); setUrl(url); setConnectionProperties(conProps); } /** * Specify the JDBC Driver implementation class to use. *

An instance of this Driver class will be created and held * within the SimpleDriverDataSource. * @see #setDriver */ public void setDriverClass(Class driverClass) { this.driver = Reflects.newInstance(driverClass); } /** * Specify the JDBC Driver instance to use. *

This allows for passing in a shared, possibly pre-configured * Driver instance. * @see #setDriverClass */ public void setDriver(Driver driver) { this.driver = driver; } /** * Return the JDBC Driver instance to use. */ public Driver getDriver() { return this.driver; } @Override protected Connection getConnectionFromDriver(Properties props) throws SQLException { Driver driver = getDriver(); String url = getUrl(); Preconditions.checkNotNull(driver, "Driver must not be null"); if (logger.isDebugEnabled()) { logger.debug("Creating new JDBC Driver Connection to [" + url + "]"); } return driver.connect(url, props); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy