ConnectionPool and SimpleDataSource are marked Serializable, but their
authorKris Jurka <books@ejurka.com>
Tue, 3 Feb 2004 05:28:17 +0000 (05:28 +0000)
committerKris Jurka <books@ejurka.com>
Tue, 3 Feb 2004 05:28:17 +0000 (05:28 +0000)
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

src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java
src/interfaces/jdbc/org/postgresql/jdbc2/optional/ConnectionPool.java
src/interfaces/jdbc/org/postgresql/jdbc2/optional/SimpleDataSource.java
src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java

index 417cf87c10c50dffbe8a8d4ba7fe5644dadf9679..089dbd18da3a1f8a7a475c3dee8fda423c5f60c1 100644 (file)
@@ -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();
+   }
+
 }
index f7700698e2ea2a58aa283ffad5e6347264aea12a..58ae5b58babc29b743f166afed2f8d55cff4c710 100644 (file)
@@ -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;
  * <p>This implementation supports JDK 1.3 and higher.</p>
  *
  * @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();
+   }
 }
index 6d9ef39fffb17a1a27c88e0e812572de2acac894..58864f6ee94f68e800997d0eff2e81e4fce38284 100644 (file)
@@ -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);
+   }
 }
index 69ccc545c5cc9b6ee8776ab2eddd0bc7f78a3478..6c226d0219aa200b9d73bde0c195f7499578072e 100644 (file)
@@ -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());
+   }
+
 }