You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by fr...@apache.org on 2002/02/11 21:23:22 UTC
cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc DriverDataSource.java
froehlich 02/02/11 12:23:22
Added: simplestore/src/java/org/apache/commons/simplestore/jdbc
DriverDataSource.java
Log:
re-structured repository
Revision Changes Path
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc/DriverDataSource.java
Index: DriverDataSource.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache Cocoon" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.simplestore.jdbc;
import javax.sql.DataSource;
import java.util.Properties;
import java.io.PrintWriter;
import java.sql.Driver;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.SQLWarning;
import java.sql.ResultSet;
import java.util.Map;
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: DriverDataSource.java,v 1.1 2002/02/11 20:23:22 froehlich Exp $
*
* This is light weight Read Only Datasource implementation, asumes Driver returns reentrant connections
* it tested and designed for PostgresSQL JDBC driver only.
*/
public class DriverDataSource implements DataSource {
private final Properties properties = new Properties();
private ConnectionWrapper connection[] = null;
private int counter = 0;
private Driver driver;
/** Holds value of property user. */
private String user;
/** Holds value of property password. */
private String password;
/** Holds value of property url. */
private String url;
/** Holds value of property maxConnections. */
private int maxConnections = 5;
/** Creates new DriverDataSourceFactoryImpl */
public DriverDataSource() { }
/**
* Setter for property user.
*
*@param user New value of property user.
*/
public void setUser(String user) {
if (this.user != null) {
throw new IllegalStateException();
}
this.user = user;
properties.setProperty("user", user);
}
/**
* Setter for property password.
*
*@param password New value of property password.
*/
public void setPassword(String password) {
if (this.password != null) {
throw new IllegalStateException();
}
properties.setProperty("password", password);
this.password = password;
}
/**
* Setter for property url.
*
*@param url New value of property url.
*/
public void setUrl(String url) {
if (this.url != null) {
throw new IllegalStateException();
}
this.url = url;
}
public void setLogWriter(PrintWriter printWriter) throws SQLException { }
public void setLoginTimeout(int param) throws SQLException { }
/**
* Setter for property maxConnections.
*
*@param maxConnections New value of property maxConnections.
*/
public void setMaxConnections(int maxConnections) {
if (connection != null) {
throw new IllegalStateException();
}
this.maxConnections = maxConnections;
}
/**
* Setter for property driver.
*
*@param driver New value of property driver.
*/
public void setDriver(String driver) {
if (this.driver != null) {
throw new IllegalStateException();
}
try {
this.driver = (Driver) Class.forName(driver).newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
/**
* Getter for property user.
*
*@return Value of property user.
*/
public String getUser() {
return user;
}
/**
* Getter for property password.
*
*@return Value of property password.
*/
public String getPassword() {
return password;
}
/**
* Getter for property url.
*
*@return Value of property url.
*/
public String getUrl() {
return url;
}
public Connection getConnection() throws SQLException {
synchronized (this) {
if (connection == null) {
connection = new ConnectionWrapper[maxConnections];
}
counter = (counter + 1) % maxConnections;
for (int i = 0; i < maxConnections; i++) {
if (connection[i] != null) {
if (!connection[i].isUsed()) {
counter = i;
break;
}
}
}
if (connection[counter] == null) {
connection[counter] = newConnection();
}
if (connection[counter].isClosed()) {
try {
connection[counter].release();
} catch (Exception ignore) {
}
connection[counter] = newConnection();
}
connection[counter].setUsed(true);
return connection[counter];
}
}
public PrintWriter getLogWriter() throws SQLException {
return new PrintWriter(System.out);
}
public Connection getConnection(String str, String str1) throws SQLException {
return getConnection();
}
public int getLoginTimeout() throws SQLException {
return 0;
}
/**
* Getter for property maxConnections.
*
*@return Value of property maxConnections.
*/
public int getMaxConnections() {
return maxConnections;
}
private ConnectionWrapper newConnection() throws SQLException {
Connection con = driver.connect(url, properties);
con.setAutoCommit(false);
return new ConnectionWrapper(con);
}
}
/**
*@author Juozas Baliuka
*@version $Id: DriverDataSource.java,v 1.1 2002/02/11 20:23:22 froehlich Exp $
*/
class ConnectionWrapper implements Connection {
boolean used = true;
private Connection connection;
public ConnectionWrapper(Connection connection) {
this.connection = connection;
}
public void setUsed(boolean used) {
this.used = used;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
connection.setAutoCommit(autoCommit);
}
public void setReadOnly(boolean readOnly) throws SQLException {
connection.setReadOnly(readOnly);
}
public void setCatalog(String catalog) throws SQLException {
connection.setCatalog(catalog);
}
public void setTransactionIsolation(int level) throws SQLException {
connection.setTransactionIsolation(level);
}
public void setTypeMap(Map map) throws SQLException {
connection.setTypeMap(map);
}
public boolean isUsed() {
return used;
}
public boolean getAutoCommit() throws SQLException {
return connection.getAutoCommit();
}
public boolean isClosed() throws SQLException {
if (connection == null) {
return true;
}
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.next();
rs.getInt(1);
rs.close();
stmt.close();
} catch (Exception sqle) {
try {
connection.close();
} catch (Exception ignore) {
}
return true;
}
return connection.isClosed();
}
public DatabaseMetaData getMetaData() throws SQLException {
return connection.getMetaData();
}
public boolean isReadOnly() throws SQLException {
return connection.isReadOnly();
}
public String getCatalog() throws SQLException {
return connection.getCatalog();
}
public int getTransactionIsolation() throws SQLException {
return connection.getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
return connection.getWarnings();
}
public Map getTypeMap() throws SQLException {
return connection.getTypeMap();
}
public void release() throws SQLException {
connection.close();
}
public void close() throws SQLException {
setUsed(false);
}
public Statement createStatement() throws SQLException {
return connection.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return connection.prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return connection.prepareCall(sql);
}
public String nativeSQL(String sql) throws SQLException {
return connection.nativeSQL(sql);
}
public void commit() throws SQLException {
connection.commit();
}
public void rollback() throws SQLException {
connection.rollback();
}
public void clearWarnings() throws SQLException {
connection.clearWarnings();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return connection.createStatement(resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>