diff options
author | Barry Lind <barry@xythos.com> | 2002-09-25 07:01:31 +0000 |
---|---|---|
committer | Barry Lind <barry@xythos.com> | 2002-09-25 07:01:31 +0000 |
commit | 7bf1c8b0ad5f2534362fb6938be28bb041d79c90 (patch) | |
tree | 4e12707f3fb34f61d91c24f6b4b86cee49596c62 /src/interfaces/jdbc/org/postgresql/jdbc3 | |
parent | 65bf5a39e08ed5ae395725c8353714482a8ec132 (diff) | |
download | postgresql-7bf1c8b0ad5f2534362fb6938be28bb041d79c90.tar.gz postgresql-7bf1c8b0ad5f2534362fb6938be28bb041d79c90.zip |
Applied patch from Aaron Mulder (ammulder@alumni.princeton.edu) that fixes
jdbc datasource support for jdk1.4/jdbc3
Modified Files:
jdbc/build.xml jdbc/org/postgresql/Driver.java.in
jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java
jdbc/org/postgresql/jdbc2/optional/PGObjectFactory.java
jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java
jdbc/org/postgresql/jdbc2/optional/PoolingDataSource.java
jdbc/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java
jdbc/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java
jdbc/org/postgresql/test/jdbc3/Jdbc3TestSuite.java
Added Files:
jdbc/org/postgresql/jdbc3/Jdbc3ConnectionPool.java
jdbc/org/postgresql/jdbc3/Jdbc3ObjectFactory.java
jdbc/org/postgresql/jdbc3/Jdbc3PooledConnection.java
jdbc/org/postgresql/jdbc3/Jdbc3PoolingDataSource.java
jdbc/org/postgresql/jdbc3/Jdbc3SimpleDataSource.java
jdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java
jdbc/org/postgresql/test/jdbc3/Jdbc3ConnectionPoolTest.java
jdbc/org/postgresql/test/jdbc3/Jdbc3PoolingDataSourceTest.java
jdbc/org/postgresql/test/jdbc3/Jdbc3SimpleDataSourceTest.java
jdbc/org/postgresql/test/util/MiniJndiContext.java
jdbc/org/postgresql/test/util/MiniJndiContextFactory.java
Diffstat (limited to 'src/interfaces/jdbc/org/postgresql/jdbc3')
5 files changed, 287 insertions, 0 deletions
diff --git a/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ConnectionPool.java b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ConnectionPool.java new file mode 100644 index 00000000000..1f739d5eb9d --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ConnectionPool.java @@ -0,0 +1,61 @@ +package org.postgresql.jdbc3; + +import org.postgresql.jdbc2.optional.ConnectionPool; + +import javax.sql.PooledConnection; +import javax.naming.Reference; +import java.sql.SQLException; + +/** + * Jdbc3 implementation of ConnectionPoolDataSource. This is + * typically the interface used by an app server to interact + * with connection pools provided by a JDBC driver. PostgreSQL + * does not support XADataSource, the other common connection + * pooling interface (for connections supporting the two-phase + * commit protocol). + * + * @author Aaron Mulder (ammulder@alumni.princeton.edu) + * @version $Revision: 1.1 $ + */ +public class Jdbc3ConnectionPool extends ConnectionPool +{ + /** + * Gets a description of this DataSource. + */ + public String getDescription() + { + return "Jdbc3ConnectionPool from " + org.postgresql.Driver.getVersion(); + } + + /** + * Gets a connection which may be pooled by the app server or middleware + * implementation of DataSource. + * + * @throws java.sql.SQLException + * Occurs when the physical database connection cannot be established. + */ + public PooledConnection getPooledConnection() throws SQLException + { + return new Jdbc3PooledConnection(getConnection(), isDefaultAutoCommit()); + } + + /** + * Gets a connection which may be pooled by the app server or middleware + * implementation of DataSource. + * + * @throws java.sql.SQLException + * Occurs when the physical database connection cannot be established. + */ + public PooledConnection getPooledConnection(String user, String password) throws SQLException + { + return new Jdbc3PooledConnection(getConnection(user, password), isDefaultAutoCommit()); + } + + /** + * Generates a JDBC object factory reference. + */ + protected Reference createReference() + { + return new Reference(getClass().getName(), Jdbc3ObjectFactory.class.getName(), null); + } +} diff --git a/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ObjectFactory.java b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ObjectFactory.java new file mode 100644 index 00000000000..53151b80a24 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3ObjectFactory.java @@ -0,0 +1,80 @@ +package org.postgresql.jdbc3; + +import java.util.*; +import javax.naming.*; +import org.postgresql.jdbc2.optional.PGObjectFactory; + +/** + * JDBC3 version of the Object Factory used to recreate objects + * from their JNDI references. + * + * @author Aaron Mulder (ammulder@alumni.princeton.edu) + * @version $Revision: 1.1 $ + */ +public class Jdbc3ObjectFactory extends PGObjectFactory +{ + /** + * Dereferences a PostgreSQL DataSource. Other types of references are + * ignored. + */ + public Object getObjectInstance(Object obj, Name name, Context nameCtx, + Hashtable environment) throws Exception + { + Reference ref = (Reference) obj; + if (ref.getClassName().equals(Jdbc3SimpleDataSource.class.getName())) + { + return loadSimpleDataSource(ref); + } + else if (ref.getClassName().equals(Jdbc3ConnectionPool.class.getName())) + { + return loadConnectionPool(ref); + } + else if (ref.getClassName().equals(Jdbc3PoolingDataSource.class.getName())) + { + return loadPoolingDataSource(ref); + } + else + { + return null; + } + } + + private Object loadPoolingDataSource(Reference ref) + { + // If DataSource exists, return it + String name = getProperty(ref, "dataSourceName"); + Jdbc3PoolingDataSource pds = Jdbc3PoolingDataSource.getDataSource(name); + if (pds != null) + { + return pds; + } + // Otherwise, create a new one + pds = new Jdbc3PoolingDataSource(); + pds.setDataSourceName(name); + loadBaseDataSource(pds, ref); + String min = getProperty(ref, "initialConnections"); + if (min != null) + { + pds.setInitialConnections(Integer.parseInt(min)); + } + String max = getProperty(ref, "maxConnections"); + if (max != null) + { + pds.setMaxConnections(Integer.parseInt(max)); + } + return pds; + } + + private Object loadSimpleDataSource(Reference ref) + { + Jdbc3SimpleDataSource ds = new Jdbc3SimpleDataSource(); + return loadBaseDataSource(ds, ref); + } + + private Object loadConnectionPool(Reference ref) + { + Jdbc3ConnectionPool cp = new Jdbc3ConnectionPool(); + return loadBaseDataSource(cp, ref); + } + +} diff --git a/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PooledConnection.java b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PooledConnection.java new file mode 100644 index 00000000000..cac966ddc81 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PooledConnection.java @@ -0,0 +1,20 @@ +package org.postgresql.jdbc3; + +import org.postgresql.jdbc2.optional.PooledConnectionImpl; + +import java.sql.Connection; + +/** + * JDBC3 implementation of PooledConnection, which manages + * a connection in a connection pool. + * + * @author Aaron Mulder (ammulder@alumni.princeton.edu) + * @version $Revision: 1.1 $ + */ +public class Jdbc3PooledConnection extends PooledConnectionImpl +{ + Jdbc3PooledConnection(Connection con, boolean autoCommit) + { + super(con, autoCommit); + } +} diff --git a/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PoolingDataSource.java b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PoolingDataSource.java new file mode 100644 index 00000000000..df3e50ad074 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PoolingDataSource.java @@ -0,0 +1,96 @@ +package org.postgresql.jdbc3; + +import java.util.*; +import org.postgresql.jdbc2.optional.PoolingDataSource; +import org.postgresql.jdbc2.optional.ConnectionPool; + +import javax.naming.Reference; + +/** + * JDBC 3 implementation of a pooling DataSource. This is best + * used outside of an application server environment. Application + * servers generally prefer to deal with instances of + * ConnectionPoolDataSource (see ConnectionPool) or XADataSource + * (not available for PostgreSQL). + * + * @author Aaron Mulder (ammulder@alumni.princeton.edu) + * @version $Revision: 1.1 $ + */ +public class Jdbc3PoolingDataSource extends PoolingDataSource +{ + /** + * Store JDBC3 DataSources in different bucket than JDBC2 DataSources + */ + private static Map dataSources = new HashMap(); + + /** + * Store JDBC3 DataSources in different bucket than JDBC2 DataSources + */ + static Jdbc3PoolingDataSource getDataSource(String name) + { + return (Jdbc3PoolingDataSource) dataSources.get(name); + } + + /** + * Store JDBC3 DataSources in different bucket than JDBC2 DataSources + */ + protected void removeStoredDataSource() + { + synchronized (dataSources) + { + dataSources.remove(dataSourceName); + } + } + + /** + * Store JDBC3 DataSources in different bucket than JDBC2 DataSources + */ + public void setDataSourceName(String dataSourceName) + { + if (isInitialized()) + { + throw new IllegalStateException("Cannot set Data Source properties after DataSource has been used"); + } + if (this.dataSourceName != null && dataSourceName != null && dataSourceName.equals(this.dataSourceName)) + { + return; + } + synchronized (dataSources) + { + if (getDataSource(dataSourceName) != null) + { + throw new IllegalArgumentException("DataSource with name '" + dataSourceName + "' already exists!"); + } + if (this.dataSourceName != null) + { + dataSources.remove(this.dataSourceName); + } + this.dataSourceName = dataSourceName; + dataSources.put(dataSourceName, this); + } + } + + /** + * Generates a JDBC3 object factory reference. + */ + protected Reference createReference() + { + return new Reference(getClass().getName(), Jdbc3ObjectFactory.class.getName(), null); + } + + /** + * Creates a JDBC3 ConnectionPool to use with this DataSource. + */ + protected ConnectionPool createConnectionPool() + { + return new Jdbc3ConnectionPool(); + } + + /** + * Gets a description of this DataSource. + */ + public String getDescription() + { + return "JDBC3 Pooling DataSource from " + org.postgresql.Driver.getVersion(); + } +} diff --git a/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3SimpleDataSource.java b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3SimpleDataSource.java new file mode 100644 index 00000000000..d8dff03221b --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3SimpleDataSource.java @@ -0,0 +1,30 @@ +package org.postgresql.jdbc3; + +import org.postgresql.jdbc2.optional.SimpleDataSource; + +import javax.naming.Reference; + +/** + * JDBC3 implementation of a non-pooling DataSource. + * + * @author Aaron Mulder (ammulder@alumni.princeton.edu) + * @version $Revision: 1.1 $ + */ +public class Jdbc3SimpleDataSource extends SimpleDataSource +{ + /** + * Generates a JDBC3 object factory reference. + */ + protected Reference createReference() + { + return new Reference(getClass().getName(), Jdbc3ObjectFactory.class.getName(), null); + } + + /** + * Gets a description of this DataSource. + */ + public String getDescription() + { + return "JDBC3 Non-Pooling DataSource from " + org.postgresql.Driver.getVersion(); + } +} |