pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Connecting over UNIX domain sockets
7+ messages / 6 participants
[nested] [flat]

* Connecting over UNIX domain sockets
@ 2011-02-08 14:55  Florian Weimer <fweimer@bfk.de>
  0 siblings, 1 reply; 7+ messages in thread

From: Florian Weimer @ 2011-02-08 14:55 UTC (permalink / raw)
  To: pgsql-jdbc

I would like to have the option to connect to the database over UNIX
domain sockets (sidestepping the question of password management).

Even though the JVM knows about UNIX domain sockets, this
functionality is not exposed to user code, so we'd have to add some
JNI code.  Would you like to integrate this in some way?  If yes,
would you prefer a socket-based or libpq-based approach?

-- 
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-02-08 18:04  John R Pierce <pierce@hogranch.com>
  parent: Florian Weimer <fweimer@bfk.de>
  0 siblings, 1 reply; 7+ messages in thread

From: John R Pierce @ 2011-02-08 18:04 UTC (permalink / raw)
  To: pgsql-jdbc

On 02/08/11 6:55 AM, Florian Weimer wrote:
> I would like to have the option to connect to the database over UNIX
> domain sockets (sidestepping the question of password management).
>
> Even though the JVM knows about UNIX domain sockets, this
> functionality is not exposed to user code, so we'd have to add some
> JNI code.  Would you like to integrate this in some way?  If yes,
> would you prefer a socket-based or libpq-based approach?

socket based would require less work, as you'd just add that to the 
existing socket code, while using libpq would essentially end up with an 
entirely different JDBC.

managing and installing JNI based code across platforms is a pain in the 
sternum.







^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-02-09 10:14  Florian Weimer <fweimer@bfk.de>
  parent: John R Pierce <pierce@hogranch.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Florian Weimer @ 2011-02-09 10:14 UTC (permalink / raw)
  To: John R Pierce <pierce@hogranch.com>; +Cc: pgsql-jdbc

* John R. Pierce:

> On 02/08/11 6:55 AM, Florian Weimer wrote:
>> I would like to have the option to connect to the database over UNIX
>> domain sockets (sidestepping the question of password management).
>>
>> Even though the JVM knows about UNIX domain sockets, this
>> functionality is not exposed to user code, so we'd have to add some
>> JNI code.  Would you like to integrate this in some way?  If yes,
>> would you prefer a socket-based or libpq-based approach?
>
> socket based would require less work, as you'd just add that to the
> existing socket code, while using libpq would essentially end up with
> an entirely different JDBC.

I would try to add a low-level read/write API to libpq.  libpq would
only handle configuration processing, authentication and the transport
layer (which may include TLS).

> managing and installing JNI based code across platforms is a pain in
> the sternum.

Some people do have working software distribution platforms. 8-)

-- 
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-06-27 19:02  Phil Clay <pilf_b@yahoo.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Phil Clay @ 2011-06-27 19:02 UTC (permalink / raw)
  To: pgsql-jdbc@postgresql.org, Florian Weimer <fweimer@bfk.de>; pierce@hogranch.com

On 02/08/11 6:55 AM, Florian Weimer wrote:

>I would like to have the option to connect to the database over UNIX
>domain sockets (sidestepping the question of password management).
>
>Even though the JVM knows about UNIX domain sockets, this
>functionality is not exposed to user code, so we'd have to add some
>JNI code.  Would you like to integrate this in some way?  If yes,
>would you prefer a socket-based or libpq-based approach?
>
>-- 
>Florian Weimer                <fweimer(at)bfk(dot)de>
>BFK edv-consulting GmbH       http://www.bfk.de/
>Kriegsstraße 100              tel: +49-721-96201-1
>D-76133 Karlsruhe             fax: +49-721-96201-99



Apologies for resurrecting an old thread, but I'm also interested in using 
domain sockets with postgres' jdbc driver.

To any potential implementors, I wanted to point out something that might be 
useful...

The junixsocket project (http://code.google.com/p/junixsocket/) provides an 
implementation of java.net.Socket that uses domain sockets.  It also provides a 
mysql SocketFactory so that mysql's jdbc driver can be used with domain 
sockets.  
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".


If the postgres jdbc project does not want the full burden of managing the 
native code required for domain socket connectivity, perhaps one approach to 
take would be to refactor the postgres jdbc driver in such a way that a 
different mechanism for constructing sockets could be plugged in.  Then an 
implementation could be provided by junixsocket, which already has the native 
distribution mechanism.

Just a thought.

Phil



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-06-27 19:06  Hannu Krosing <hannu@2ndQuadrant.com>
  parent: Phil Clay <pilf_b@yahoo.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Hannu Krosing @ 2011-06-27 19:06 UTC (permalink / raw)
  To: Phil Clay <pilf_b@yahoo.com>; +Cc: pgsql-jdbc@postgresql.org, Florian Weimer <fweimer@bfk.de>; pierce@hogranch.com

On Mon, 2011-06-27 at 12:02 -0700, Phil Clay wrote:

> Apologies for resurrecting an old thread, but I'm also interested in using 
> domain sockets with postgres' jdbc driver.
> 
> To any potential implementors, I wanted to point out something that might be 
> useful...
> 
> The junixsocket project (http://code.google.com/p/junixsocket/) provides an 
> implementation of java.net.Socket that uses domain sockets.  It also provides a 
> mysql SocketFactory so that mysql's jdbc driver can be used with domain 
> sockets.  
> 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".

Hmm - does pg jdbc driver support IPv6 ?

If so, is it done inside java socket library ? 

> If the postgres jdbc project does not want the full burden of managing the 
> native code required for domain socket connectivity, perhaps one approach to 
> take would be to refactor the postgres jdbc driver in such a way that a 
> different mechanism for constructing sockets could be plugged in.  Then an 
> implementation could be provided by junixsocket, which already has the native 
> distribution mechanism.
> 
> Just a thought.
> 
> Phil
> 

-- 
-------
Hannu Krosing
PostgreSQL Infinite Scalability and Performance Consultant
PG Admin Book: http://www.2ndQuadrant.com/books/




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-06-27 22:20  Craig Ringer <craig@postnewspapers.com.au>
  parent: Phil Clay <pilf_b@yahoo.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Craig Ringer @ 2011-06-27 22:20 UTC (permalink / raw)
  To: Phil Clay <pilf_b@yahoo.com>; +Cc: pgsql-jdbc

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.

The JDBC driver is readable and not hard to understand, so maybe you 
could chuck a patch together?

(Fighting very hard to say "Oh, I'll do something like that" when I've 
already volunteered for way too much).

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Connecting over UNIX domain sockets
@ 2011-08-06 20:58  Bruno Harbulot <bruno@distributedmatter.net>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 0 replies; 7+ messages in thread

From: Bruno Harbulot @ 2011-08-06 20:58 UTC (permalink / raw)
  To: 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.

Attachments:

PgJdbcUnixSocketFactory.java (text/plain, 1.4K) download package jdbctest; import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import javax.net.SocketFactory; import org.newsclub.net.unix.AFUNIXSocket; import org.newsclub.net.unix.AFUNIXSocketAddress; /** * Sample SocketFactory using junixsocket for use with the PostgreSQL JDBC driver. * @author Bruno Harbulot */ public class PgJdbcUnixSocketFactory extends SocketFactory { private final String path; public PgJdbcUnixSocketFactory(String path) { this.path = path; } @Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { AFUNIXSocket socket = AFUNIXSocket.newInstance(); socket.connect(new AFUNIXSocketAddress(new File(path)), port); return socket; } @Override public Socket createSocket(InetAddress host, int port) throws IOException { throw new UnsupportedOperationException(); } @Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { throw new UnsupportedOperationException(); } @Override public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { throw new UnsupportedOperationException(); } }
socketfactory.patch (text/plain, 5.4K) download ? socketfactory.patch Index: org/postgresql/core/PGStream.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/PGStream.java,v retrieving revision 1.22.4.1 diff -r1.22.4.1 PGStream.java 18a19 > import java.lang.reflect.Constructor; 19a21 > import java.net.SocketException; 20a23,25 > import java.util.Properties; > > import javax.net.SocketFactory; 36a42 > private final SocketFactory socketFactory; 54a61 > * @param socketFactory {@link SocketFactory} used for creating the sockets 57c64 < public PGStream(String host, int port) throws IOException --- > public PGStream(String host, int port, SocketFactory socketFactory) throws IOException 61,62c68,73 < < changeSocket(new Socket(host, port)); --- > > if (socketFactory == null) { > socketFactory = SocketFactory.getDefault(); > } > this.socketFactory = socketFactory; > changeSocket(this.socketFactory.createSocket(host, port)); 67a79,135 > > /** > * Constructor: Connect to the PostgreSQL back end and return > * a stream connection. > * > * @param host the hostname to connect to > * @param port the port number that the postmaster is sitting on > * @param info properties used to create the socket factory > * @exception IOException if an IOException occurs below it. > */ > public PGStream(String host, int port, Properties info) throws IOException > { > this(host, port, createSocketFactory(info)); > } > > public static SocketFactory createSocketFactory(Properties info) { > SocketFactory factory; > > // Use the default factory if no specific factory is requested > // > String classname = info.getProperty("socketfactory"); > if (classname == null) > { > factory = SocketFactory.getDefault(); > } > else > { > Object[] args = {info.getProperty("socketfactoryarg")}; > Constructor ctor; > Class factoryClass; > > try > { > factoryClass = Class.forName(classname); > try > { > ctor = factoryClass.getConstructor(new Class[]{String.class}); > } > catch (NoSuchMethodException nsme) > { > ctor = factoryClass.getConstructor((Class[])null); > args = null; > } > factory = (SocketFactory)ctor.newInstance(args); > } > catch (Exception e) > { > throw new RuntimeException(new PSQLException(GT.tr("The SocketFactory class provided {0} could not be instantiated.", classname), PSQLState.CONNECTION_FAILURE, e)); > } > } > > return factory; > } > > public SocketFactory getSocketFactory() throws PSQLException { > return socketFactory; > } 108c176,180 < connection.setTcpNoDelay(true); --- > try { > connection.setTcpNoDelay(true); > } catch (SocketException e) { > // Ignore if the operation wasn't supported. > } Index: org/postgresql/core/v2/ConnectionFactoryImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v retrieving revision 1.18.2.1 diff -r1.18.2.1 ConnectionFactoryImpl.java 62c62 < newStream = new PGStream(host, port); --- > newStream = new PGStream(host, port, info); 163c163 < return new PGStream(pgStream.getHost(), pgStream.getPort()); --- > return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory()); Index: org/postgresql/core/v2/ProtocolConnectionImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v retrieving revision 1.12 diff -r1.12 ProtocolConnectionImpl.java 93c93 < cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); --- > cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory()); Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v retrieving revision 1.20.2.1 diff -r1.20.2.1 ConnectionFactoryImpl.java 16a17 > import java.lang.reflect.Constructor; 76c77,78 < newStream = new PGStream(host, port); --- > > newStream = new PGStream(host, port, info); 192c194 < return new PGStream(pgStream.getHost(), pgStream.getPort()); --- > return new PGStream(pgStream.getHost(), pgStream.getPort(), info); Index: org/postgresql/core/v3/ProtocolConnectionImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v retrieving revision 1.13 diff -r1.13 ProtocolConnectionImpl.java 93c93 < cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); --- > cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory());
^ permalink raw reply [nested|flat] 7+ messages in thread


end of thread, other threads:[~2011-08-06 20:58 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-02-08 14:55 Connecting over UNIX domain sockets Florian Weimer <fweimer@bfk.de>
2011-02-08 18:04 ` John R Pierce <pierce@hogranch.com>
2011-02-09 10:14   ` Florian Weimer <fweimer@bfk.de>
2011-06-27 19:02 Re: Connecting over UNIX domain sockets Phil Clay <pilf_b@yahoo.com>
2011-06-27 19:06 ` Hannu Krosing <hannu@2ndQuadrant.com>
2011-06-27 22:20 ` Craig Ringer <craig@postnewspapers.com.au>
2011-08-06 20:58   ` Bruno Harbulot <bruno@distributedmatter.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox