From d71188860e9a0724770fa6a1b0c2cc02119c16a6 Mon Sep 17 00:00:00 2001 From: Kris Jurka Date: Tue, 3 Feb 2004 05:28:17 +0000 Subject: [PATCH] ConnectionPool and SimpleDataSource are marked Serializable, but their superclass (which contains a number of state variables) is not. To correctly serialize these objects we need to manually implement writeObject and readObject. Per report from R. Lemos --- .../jdbc2/optional/BaseDataSource.java | 26 ++++++++++++++-- .../jdbc2/optional/ConnectionPool.java | 16 +++++++++- .../jdbc2/optional/SimpleDataSource.java | 15 +++++++++- .../jdbc2/optional/ConnectionPoolTest.java | 30 ++++++++++++++++++- 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java index 417cf87c10c..089dbd18da3 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java @@ -4,11 +4,15 @@ import javax.naming.*; import java.io.PrintWriter; import java.sql.*; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; +import java.io.IOException; + /** * Base class for data sources and related classes. * * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.3 $ + * @version $Revision: 1.3.6.1 $ */ public abstract class BaseDataSource implements Referenceable { @@ -53,7 +57,7 @@ public abstract class BaseDataSource implements Referenceable /** * Gets a connection to the PostgreSQL database. The database is identified by the - * DataAource properties serverName, databaseName, and portNumber. The user to + * DataSource properties serverName, databaseName, and portNumber. The user to * connect as is identified by the arguments user and password, which override * the DataSource properties by the same name. * @@ -262,4 +266,22 @@ public abstract class BaseDataSource implements Referenceable return ref; } + protected void writeBaseObject(ObjectOutputStream out) throws IOException + { + out.writeObject(serverName); + out.writeObject(databaseName); + out.writeObject(user); + out.writeObject(password); + out.writeInt(portNumber); + } + + protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException + { + serverName = (String)in.readObject(); + databaseName = (String)in.readObject(); + user = (String)in.readObject(); + password = (String)in.readObject(); + portNumber = in.readInt(); + } + } diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/ConnectionPool.java b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/ConnectionPool.java index f7700698e2e..58ae5b58bab 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/ConnectionPool.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/ConnectionPool.java @@ -4,6 +4,9 @@ import javax.sql.ConnectionPoolDataSource; import javax.sql.PooledConnection; import java.sql.SQLException; import java.io.Serializable; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; +import java.io.IOException; /** * PostgreSQL implementation of ConnectionPoolDataSource. The app server or @@ -21,7 +24,7 @@ import java.io.Serializable; *

This implementation supports JDK 1.3 and higher.

* * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.2 $ + * @version $Revision: 1.2.6.1 $ */ public class ConnectionPool extends BaseDataSource implements Serializable, ConnectionPoolDataSource { @@ -79,4 +82,15 @@ public class ConnectionPool extends BaseDataSource implements Serializable, Conn this.defaultAutoCommit = defaultAutoCommit; } + private void writeObject(ObjectOutputStream out) throws IOException + { + writeBaseObject(out); + out.writeBoolean(defaultAutoCommit); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException + { + readBaseObject(in); + defaultAutoCommit = in.readBoolean(); + } } diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/SimpleDataSource.java b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/SimpleDataSource.java index 6d9ef39fffb..58864f6ee94 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/optional/SimpleDataSource.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/optional/SimpleDataSource.java @@ -2,6 +2,9 @@ package org.postgresql.jdbc2.optional; import javax.sql.DataSource; import java.io.Serializable; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; +import java.io.IOException; /** * Simple DataSource which does not perform connection pooling. In order to use @@ -10,7 +13,7 @@ import java.io.Serializable; * are declared in the superclass. * * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.2 $ + * @version $Revision: 1.2.6.1 $ */ public class SimpleDataSource extends BaseDataSource implements Serializable, DataSource { @@ -21,4 +24,14 @@ public class SimpleDataSource extends BaseDataSource implements Serializable, Da { return "Non-Pooling DataSource from " + org.postgresql.Driver.getVersion(); } + + private void writeObject(ObjectOutputStream out) throws IOException + { + writeBaseObject(out); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException + { + readBaseObject(in); + } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java index 69ccc545c5c..6c226d0219a 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java @@ -4,6 +4,7 @@ import org.postgresql.jdbc2.optional.ConnectionPool; import org.postgresql.test.TestUtil; import javax.sql.*; import java.sql.*; +import java.io.*; /** * Tests for the ConnectionPoolDataSource and PooledConnection @@ -11,7 +12,7 @@ import java.sql.*; * interface to the PooledConnection is through the CPDS. * * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.6.4.2 $ + * @version $Revision: 1.6.4.3 $ */ public class ConnectionPoolTest extends BaseDataSourceTest { @@ -493,4 +494,31 @@ public class ConnectionPoolTest extends BaseDataSourceTest count = errorCount = 0; } } + + public void testSerializable() throws IOException, ClassNotFoundException + { + ConnectionPool pool = new ConnectionPool(); + pool.setDefaultAutoCommit(false); + pool.setServerName("db.myhost.com"); + pool.setDatabaseName("mydb"); + pool.setUser("user"); + pool.setPassword("pass"); + pool.setPortNumber(1111); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(pool); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + ConnectionPool pool2 = (ConnectionPool)ois.readObject(); + + assertEquals(pool.isDefaultAutoCommit(),pool2.isDefaultAutoCommit()); + assertEquals(pool.getServerName(),pool2.getServerName()); + assertEquals(pool.getDatabaseName(),pool2.getDatabaseName()); + assertEquals(pool.getUser(),pool2.getUser()); + assertEquals(pool.getPassword(),pool2.getPassword()); + assertEquals(pool.getPortNumber(),pool2.getPortNumber()); + } + }