mirror of
https://github.com/postgres/postgres.git
synced 2025-05-18 17:41:14 +03:00
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
This commit is contained in:
parent
1a92a4c10c
commit
d71188860e
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user