Re: Connecting over UNIX domain sockets - Mailing list pgsql-jdbc

From Bruno Harbulot
Subject Re: Connecting over UNIX domain sockets
Date
Msg-id j1k9ss$p4m$1@dough.gmane.org
Whole thread Raw
In response to Re: Connecting over UNIX domain sockets  (Craig Ringer <craig@postnewspapers.com.au>)
List pgsql-jdbc
On 27/06/2011 23:20, Craig Ringer wrote:
> On 06/28/2011 03:02 AM, Phil Clay wrote:
>
>> However, contrary to mysql's jdbc driver, postgres' jdbc driver does
>> not appear
>> to be designed in a way that a different Socket implementation could
>> be easily
>> "plugged in".
>
> There's already support for pluggable SSLSocketFactories to wrap
> sockets. Adding pluggable SocketFactory support wouldn't seem to be
> overly hard, I just suspect it's a combination of nobody having needed
> it and nobody having wanted it enough to take the time to write a patch.


Hello,

I'm not sure if anyone had done any further work on this thread, but
here is a tentative patch (I've written it against the 8.4 code base) to
be able to use a custom SocketFactory (in particular for junixsocket).

This relies on two new properties:
   - socketfactory, for the class name of the SocketFactory.
   - socketfactoryarg, for the String argument of this SocketFactory
constructor.


I've changed this constructor into two:

-    public PGStream(String host, int port)
+    public PGStream(String host, int port, SocketFactory socketFactory)
+    public PGStream(String host, int port, Properties info)

The one that uses Properties reads the properties to instantiate the
SocketFactory (via a new method PGStream.createSocketFactory(Properties
info), which is a very similar to the way the SSLSocketFactory is
instantiated).

The one that uses a SocketFactory can be used directly, but the main
reason I've introduced it was for PGStreams creates from other PGStreams
(the ones called "cancelledStream").

The only glitch was in PGStream.changeSocket(...), since AFUNIXSocket
(from junixsocket) doesn't support setTcpNoDelay (and throws an
exception). I made it catch any SocketException: although it could catch
AFUNIXSocketException (which extends SocketException), this would make
the driver depend on junixsocket.

-        connection.setTcpNoDelay(true);
+        try {
+            connection.setTcpNoDelay(true);
+        } catch (SocketException e) {
+            // Ignore if the operation wasn't supported.
+        }



I'm also attaching a sample PgJdbcUnixSocketFactory to use this with
junixsocket (which needs to be on the classpath and have
'java.library.path' configured to point to its native libraries).


The following works, using a unix socket.

Properties props = new Properties();
props.setProperty("user", "name");
props.setProperty("password", "xxxxxx");
props.setProperty("socketfactory", "jdbctest.PgJdbcUnixSocketFactory");
props.setProperty("socketfactoryarg", "/tmp/.s.PGSQL.5432");
Connection jdbcConnection =
DriverManager.getConnection("jdbc:postgresql://", props)

To specify which database is to be used, it's possible to use something
like this:

DriverManager.getConnection("jdbc:postgresql://.../dbname", props)

(What's in "..." doesn't really matter, but "jdbc:postgresql:///dbname"
doesn't work.)


I'm not sure how these changes would fit with the rest of the design of
the driver, as I must admit I don't know this code very well. I suppose
there are multiple ways of implementing this feature, but I hope this
patch may be a sufficient starting point.


Best wishes,

Bruno.

Attachment

pgsql-jdbc by date:

Previous
From: Bruno Harbulot
Date:
Subject: Re: JDBC SSL hostname verification
Next
From: alexbruy
Date:
Subject: Accessing PostgreSQL DB from Android app