Skip to content

Commit 7bf1c8b

Browse files
author
Barry Lind
committed
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
1 parent 65bf5a3 commit 7bf1c8b

20 files changed

+1023
-31
lines changed

src/interfaces/jdbc/build.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
This file now requires Ant 1.4.1. 2002-04-18
88
9-
$Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/build.xml,v 1.28 2002/08/14 20:35:39 barry Exp $
9+
$Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/build.xml,v 1.29 2002/09/25 07:01:30 barry Exp $
1010
1111
-->
1212

@@ -262,6 +262,7 @@
262262
<target name="testjdbc2optional" depends="jar" if="jdbc2optionaltests">
263263
<javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
264264
<include name="${package}/test/jdbc2/optional/**" />
265+
<include name="${package}/test/util/**" />
265266
</javac>
266267
<java fork="yes" classname="junit.${junit.ui}.TestRunner" taskname="junit" failonerror="true">
267268
<arg value="org.postgresql.test.jdbc2.optional.OptionalTestSuite" />
@@ -278,6 +279,7 @@
278279
<target name="testjdbc3" depends="jar" if="jdbc3tests">
279280
<javac srcdir="${srcdir}" destdir="${builddir}" debug="${debug}">
280281
<include name="${package}/test/jdbc3/*" />
282+
<include name="${package}/test/util/*" />
281283
</javac>
282284
<java fork="yes" classname="junit.${junit.ui}.TestRunner" taskname="junit" failonerror="true">
283285
<arg value="org.postgresql.test.jdbc3.Jdbc3TestSuite" />

src/interfaces/jdbc/org/postgresql/Driver.java.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ public class Driver implements java.sql.Driver
446446
}
447447

448448
//The build number should be incremented for every new build
449-
private static int m_buildNumber = 105;
449+
private static int m_buildNumber = 106;
450450

451451
}

src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Base class for data sources and related classes.
99
*
1010
* @author Aaron Mulder (ammulder@chariotsolutions.com)
11-
* @version $Revision: 1.2 $
11+
* @version $Revision: 1.3 $
1212
*/
1313
public abstract class BaseDataSource implements Referenceable
1414
{
@@ -233,9 +233,18 @@ private String getUrl()
233233
return "jdbc:postgresql://" + serverName + (portNumber == 0 ? "" : ":" + portNumber) + "/" + databaseName;
234234
}
235235

236+
/**
237+
* Generates a reference using the appropriate object factory. This
238+
* implementation uses the JDBC 2 optional package object factory.
239+
*/
240+
protected Reference createReference()
241+
{
242+
return new Reference(getClass().getName(), PGObjectFactory.class.getName(), null);
243+
}
244+
236245
public Reference getReference() throws NamingException
237246
{
238-
Reference ref = new Reference(getClass().getName(), PGObjectFactory.class.getName(), null);
247+
Reference ref = createReference();
239248
ref.add(new StringRefAddr("serverName", serverName));
240249
if (portNumber != 0)
241250
{

src/interfaces/jdbc/org/postgresql/jdbc2/optional/PGObjectFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* consistent.
1414
*
1515
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16-
* @version $Revision: 1.2 $
16+
* @version $Revision: 1.3 $
1717
*/
1818
public class PGObjectFactory implements ObjectFactory
1919
{
@@ -81,7 +81,7 @@ private Object loadConnectionPool(Reference ref)
8181
return loadBaseDataSource(cp, ref);
8282
}
8383

84-
private Object loadBaseDataSource(BaseDataSource ds, Reference ref)
84+
protected Object loadBaseDataSource(BaseDataSource ds, Reference ref)
8585
{
8686
ds.setDatabaseName(getProperty(ref, "databaseName"));
8787
ds.setPassword(getProperty(ref, "password"));
@@ -95,7 +95,7 @@ private Object loadBaseDataSource(BaseDataSource ds, Reference ref)
9595
return ds;
9696
}
9797

98-
private String getProperty(Reference ref, String s)
98+
protected String getProperty(Reference ref, String s)
9999
{
100100
RefAddr addr = ref.get(s);
101101
if (addr == null)

src/interfaces/jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @see ConnectionPool
1414
*
1515
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16-
* @version $Revision: 1.2 $
16+
* @version $Revision: 1.3 $
1717
*/
1818
public class PooledConnectionImpl implements PooledConnection
1919
{
@@ -26,7 +26,7 @@ public class PooledConnectionImpl implements PooledConnection
2626
* Creates a new PooledConnection representing the specified physical
2727
* connection.
2828
*/
29-
PooledConnectionImpl(Connection con, boolean autoCommit)
29+
protected PooledConnectionImpl(Connection con, boolean autoCommit)
3030
{
3131
this.con = con;
3232
this.autoCommit = autoCommit;

src/interfaces/jdbc/org/postgresql/jdbc2/optional/PoolingDataSource.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* <p>This implementation supports JDK 1.3 and higher.</p>
3434
*
3535
* @author Aaron Mulder (ammulder@chariotsolutions.com)
36-
* @version $Revision: 1.2 $
36+
* @version $Revision: 1.3 $
3737
*/
3838
public class PoolingDataSource extends BaseDataSource implements DataSource
3939
{
@@ -45,7 +45,7 @@ static PoolingDataSource getDataSource(String name)
4545
}
4646

4747
// Additional Data Source properties
48-
private String dataSourceName;
48+
protected String dataSourceName; // Must be protected for subclasses to sync updates to it
4949
private int initialConnections = 0;
5050
private int maxConnections = 0;
5151
// State variables
@@ -262,7 +262,7 @@ public void setDataSourceName(String dataSourceName)
262262
* that number of connections will be created. After this method is called,
263263
* the DataSource properties cannot be changed. If you do not call this
264264
* explicitly, it will be called the first time you get a connection from the
265-
* Datasource.
265+
* DataSource.
266266
* @throws java.sql.SQLException
267267
* Occurs when the initialConnections is greater than zero, but the
268268
* DataSource is not able to create enough physical connections.
@@ -271,7 +271,7 @@ public void initialize() throws SQLException
271271
{
272272
synchronized (lock)
273273
{
274-
source = new ConnectionPool();
274+
source = createConnectionPool();
275275
source.setDatabaseName(getDatabaseName());
276276
source.setPassword(getPassword());
277277
source.setPortNumber(getPortNumber());
@@ -285,6 +285,17 @@ public void initialize() throws SQLException
285285
}
286286
}
287287

288+
protected boolean isInitialized() {
289+
return initialized;
290+
}
291+
292+
/**
293+
* Creates the appropriate ConnectionPool to use for this DataSource.
294+
*/
295+
protected ConnectionPool createConnectionPool() {
296+
return new ConnectionPool();
297+
}
298+
288299
/**
289300
* Gets a <b>non-pooled</b> connection, unless the user and password are the
290301
* same as the default values for this connection pool.
@@ -358,13 +369,17 @@ public void close()
358369
}
359370
used = null;
360371
}
361-
synchronized (dataSources)
362-
{
363-
dataSources.remove(dataSourceName);
364-
}
365-
}
372+
removeStoredDataSource();
373+
}
366374

367-
/**
375+
protected void removeStoredDataSource() {
376+
synchronized (dataSources)
377+
{
378+
dataSources.remove(dataSourceName);
379+
}
380+
}
381+
382+
/**
368383
* Gets a connection from the pool. Will get an available one if
369384
* present, or create a new one if under the max limit. Will
370385
* block if all used and a new one would exceed the max.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.postgresql.jdbc3;
2+
3+
import org.postgresql.jdbc2.optional.ConnectionPool;
4+
5+
import javax.sql.PooledConnection;
6+
import javax.naming.Reference;
7+
import java.sql.SQLException;
8+
9+
/**
10+
* Jdbc3 implementation of ConnectionPoolDataSource. This is
11+
* typically the interface used by an app server to interact
12+
* with connection pools provided by a JDBC driver. PostgreSQL
13+
* does not support XADataSource, the other common connection
14+
* pooling interface (for connections supporting the two-phase
15+
* commit protocol).
16+
*
17+
* @author Aaron Mulder (ammulder@alumni.princeton.edu)
18+
* @version $Revision: 1.1 $
19+
*/
20+
public class Jdbc3ConnectionPool extends ConnectionPool
21+
{
22+
/**
23+
* Gets a description of this DataSource.
24+
*/
25+
public String getDescription()
26+
{
27+
return "Jdbc3ConnectionPool from " + org.postgresql.Driver.getVersion();
28+
}
29+
30+
/**
31+
* Gets a connection which may be pooled by the app server or middleware
32+
* implementation of DataSource.
33+
*
34+
* @throws java.sql.SQLException
35+
* Occurs when the physical database connection cannot be established.
36+
*/
37+
public PooledConnection getPooledConnection() throws SQLException
38+
{
39+
return new Jdbc3PooledConnection(getConnection(), isDefaultAutoCommit());
40+
}
41+
42+
/**
43+
* Gets a connection which may be pooled by the app server or middleware
44+
* implementation of DataSource.
45+
*
46+
* @throws java.sql.SQLException
47+
* Occurs when the physical database connection cannot be established.
48+
*/
49+
public PooledConnection getPooledConnection(String user, String password) throws SQLException
50+
{
51+
return new Jdbc3PooledConnection(getConnection(user, password), isDefaultAutoCommit());
52+
}
53+
54+
/**
55+
* Generates a JDBC object factory reference.
56+
*/
57+
protected Reference createReference()
58+
{
59+
return new Reference(getClass().getName(), Jdbc3ObjectFactory.class.getName(), null);
60+
}
61+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.postgresql.jdbc3;
2+
3+
import java.util.*;
4+
import javax.naming.*;
5+
import org.postgresql.jdbc2.optional.PGObjectFactory;
6+
7+
/**
8+
* JDBC3 version of the Object Factory used to recreate objects
9+
* from their JNDI references.
10+
*
11+
* @author Aaron Mulder (ammulder@alumni.princeton.edu)
12+
* @version $Revision: 1.1 $
13+
*/
14+
public class Jdbc3ObjectFactory extends PGObjectFactory
15+
{
16+
/**
17+
* Dereferences a PostgreSQL DataSource. Other types of references are
18+
* ignored.
19+
*/
20+
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
21+
Hashtable environment) throws Exception
22+
{
23+
Reference ref = (Reference) obj;
24+
if (ref.getClassName().equals(Jdbc3SimpleDataSource.class.getName()))
25+
{
26+
return loadSimpleDataSource(ref);
27+
}
28+
else if (ref.getClassName().equals(Jdbc3ConnectionPool.class.getName()))
29+
{
30+
return loadConnectionPool(ref);
31+
}
32+
else if (ref.getClassName().equals(Jdbc3PoolingDataSource.class.getName()))
33+
{
34+
return loadPoolingDataSource(ref);
35+
}
36+
else
37+
{
38+
return null;
39+
}
40+
}
41+
42+
private Object loadPoolingDataSource(Reference ref)
43+
{
44+
// If DataSource exists, return it
45+
String name = getProperty(ref, "dataSourceName");
46+
Jdbc3PoolingDataSource pds = Jdbc3PoolingDataSource.getDataSource(name);
47+
if (pds != null)
48+
{
49+
return pds;
50+
}
51+
// Otherwise, create a new one
52+
pds = new Jdbc3PoolingDataSource();
53+
pds.setDataSourceName(name);
54+
loadBaseDataSource(pds, ref);
55+
String min = getProperty(ref, "initialConnections");
56+
if (min != null)
57+
{
58+
pds.setInitialConnections(Integer.parseInt(min));
59+
}
60+
String max = getProperty(ref, "maxConnections");
61+
if (max != null)
62+
{
63+
pds.setMaxConnections(Integer.parseInt(max));
64+
}
65+
return pds;
66+
}
67+
68+
private Object loadSimpleDataSource(Reference ref)
69+
{
70+
Jdbc3SimpleDataSource ds = new Jdbc3SimpleDataSource();
71+
return loadBaseDataSource(ds, ref);
72+
}
73+
74+
private Object loadConnectionPool(Reference ref)
75+
{
76+
Jdbc3ConnectionPool cp = new Jdbc3ConnectionPool();
77+
return loadBaseDataSource(cp, ref);
78+
}
79+
80+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.postgresql.jdbc3;
2+
3+
import org.postgresql.jdbc2.optional.PooledConnectionImpl;
4+
5+
import java.sql.Connection;
6+
7+
/**
8+
* JDBC3 implementation of PooledConnection, which manages
9+
* a connection in a connection pool.
10+
*
11+
* @author Aaron Mulder (ammulder@alumni.princeton.edu)
12+
* @version $Revision: 1.1 $
13+
*/
14+
public class Jdbc3PooledConnection extends PooledConnectionImpl
15+
{
16+
Jdbc3PooledConnection(Connection con, boolean autoCommit)
17+
{
18+
super(con, autoCommit);
19+
}
20+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy