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

org.javasimon.jdbcx4.SimonDataSource Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.jdbcx4;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;

import org.javasimon.jdbc4.SimonConnection;
import org.javasimon.jdbc4.WrapperSupport;

/**
 * Wrapper class for real DataSource implementation, produces standard {@link java.sql.Connection}
 * object.
 * 

* To use SimonDataSource, MUST properties are: *

    *
  • realDataSourceClassName - full qualified classname of real Datasource * implementation
  • *
  • url - JDBC connection URL (no special Simon added tags are needed)
  • *
  • user - DB user name
  • *
  • password - DB user name
  • *
* MAY properties are: *
    *
  • prefix - Simon prefix (default: org.javasimon.jdbcx4
  • *
*

* As mentioned in package description all getConnection methods * just invokes real {@link javax.sql.DataSource} object methods and wraps obtained * {@link java.sql.Connection} with {@link org.javasimon.jdbc4.SimonConnection} object. *

* Real {@link javax.sql.DataSource} is obtained in method {@link #datasource()}. It tries * to instantiate real datasource object by property realDataSourceClassName * (setters and getters for properties are in {@link AbstractSimonDataSource}) and then sets * basic properties (url, user, password). * * @author Radovan Sninsky * @author Richard "Virgo" Richter * @since 2.4 */ public final class SimonDataSource extends AbstractSimonDataSource implements DataSource { private DataSource ds; private WrapperSupport wrapperSupport; DataSource datasource() throws SQLException { if (ds == null) { ds = createDataSource(DataSource.class); ds.setLogWriter(logWriter); wrapperSupport = new WrapperSupport<>(ds, DataSource.class); } return ds; } /** * Attempts to establish a connection with the data source that this {@code DataSource} object represents. * * @return a connection to the data source * @throws java.sql.SQLException if a database access error occurs */ @Override public Connection getConnection() throws SQLException { return new SimonConnection(datasource().getConnection(), getPrefix()); } /** * Attempts to establish a connection with the data source that this {@code DataSource} object represents. * * @param user the database user on whose behalf the connection is being made * @param password the user's password * @return a connection to the data source * @throws java.sql.SQLException if a database access error occurs */ @Override public Connection getConnection(String user, String password) throws SQLException { return new SimonConnection(datasource().getConnection(user, password), getPrefix()); } @Override public T unwrap(Class iface) throws SQLException { return wrapperSupport.unwrap(iface); } @Override public boolean isWrapperFor(Class iface) throws SQLException { return wrapperSupport.isWrapperFor(iface); } @Override protected String doGetRealDataSourceClassName() { return this.configuration.getRealDataSourceName(); } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return ds.getParentLogger(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy