pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JDBC causing the connection to close after executing a COPY command
9+ messages / 3 participants
[nested] [flat]

* JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 15:24  Altaf Malik <mmalik_altaf@yahoo.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Altaf Malik @ 2007-12-13 15:24 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,
 JDBC is causing the connection to close on executing a simple COPY query which the driver does not support. The code shows that driver throws an exception however explicitly the connection is not closed. Consider the following case.
<code>
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/postgres","altaf","");

Statement stmt = con.createStatement();
stmt.execute("create table abc(a int4)");
try{
        stmt.execute("COPY abc TO STDOUT WITH DELIMITER '|'");
        }catch(Exception e) {
            e.printStackTrace();
        }
        stmt.execute("Select * from abc");

</code>

Can anybody tell what's happening?

Thanks,
Altaf Malik
EnterpriseDB
http://www.enterprisedb.com/

       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 15:37  Dave Cramer <pg@fastcrypt.com>
  parent: Altaf Malik <mmalik_altaf@yahoo.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Dave Cramer @ 2007-12-13 15:37 UTC (permalink / raw)
  To: Altaf Malik <mmalik_altaf@yahoo.com>; +Cc: pgsql-jdbc

Altaf,
On 13-Dec-07, at 10:24 AM, Altaf Malik wrote:

> Hi,
>  JDBC is causing the connection to close on executing a simple COPY  
> query which the driver does not support. The code shows that driver  
> throws an exception however explicitly the connection is not closed.  
> Consider the following case.
> <code>
> Class.forName("org.postgresql.Driver");
> Connection con = DriverManager.getConnection("jdbc:postgresql:// 
> localhost/postgres","altaf","");
>
> Statement stmt = con.createStatement();
> stmt.execute("create table abc(a int4)");
> try{
>         stmt.execute("COPY abc TO STDOUT WITH DELIMITER '|'");
>         }catch(Exception e) {
>             e.printStackTrace();
>         }
>         stmt.execute("Select * from abc");
>
> </code>
>
> Can anybody tell what's happening?
>
At what point can you determine the connection is closed ? Can you do  
con.createStatement after the exception ?

Dave
> Thanks,
> Altaf Malik
> EnterpriseDB
> http://www.enterprisedb.com/
>
> Looking for last minute shopping deals? Find them fast with Yahoo!  
> Search.

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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 15:54  Altaf Malik <mmalik_altaf@yahoo.com>
  parent: Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Altaf Malik @ 2007-12-13 15:54 UTC (permalink / raw)
  To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc

Hi,
At what point can you determine the connection is closed ? Can you do con.createStatement after the exception ?
If you see, i have placed the the 
stmt.execute("COPY abc TO STDOUT WITH DELIMITER '|'");
in a try-catch block, and after the try-catch block, i have executed again
stmt.execute("Select * from abc")
which throws an IO Exception. Also if i enable logging in JDBC, and only execute the COPY statement, log shows that FrontEnd sent a Terminate message.


Thanks,
Altaf Malik
EnterpriseDB
http://www.enterprisedb.com/

       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 16:26  Kris Jurka <books@ejurka.com>
  parent: Altaf Malik <mmalik_altaf@yahoo.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Kris Jurka @ 2007-12-13 16:26 UTC (permalink / raw)
  To: Altaf Malik <mmalik_altaf@yahoo.com>; +Cc: pgsql-jdbc



On Thu, 13 Dec 2007, Altaf Malik wrote:

> JDBC is causing the connection to close on executing a simple COPY query 
> which the driver does not support. The code shows that driver throws an 
> exception however explicitly the connection is not closed. Consider the 
> following case.

The JDBC driver doesn't even understand enough of the COPY protocol to 
ignore it.  Perhaps we could respond to any copy requests with copyfail, 
but it would make more sense to me to look at integrating the copy patch.

So, I'm not sure what your complaint is.  Do you want the driver to be 
able to continue after a copy operation is issued or do you just want a 
better error message?  How would client code be better able to handle a 
"Connection has been closed behind the scenes because something illegal 
was done" vs an IOException.

Kris Jurka



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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 16:36  Kris Jurka <books@ejurka.com>
  parent: Altaf Malik <mmalik_altaf@yahoo.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Kris Jurka @ 2007-12-13 16:36 UTC (permalink / raw)
  To: Altaf Malik <mmalik_altaf@yahoo.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc



On Thu, 13 Dec 2007, Altaf Malik wrote:

> Also if i enable logging in JDBC, and only execute the COPY statement, 
> log shows that FrontEnd sent a Terminate message.
>

What's happening is that after receiving the server's message that it 
wants to do a COPY, the driver doesn't understand the protocol and dies 
with an "java.io.IOException: Unexpected packet type: " error.  Once this 
happens the driver can't do anything but close the connection.  As part of 
the connection closing process it issues a Terminate message to the 
server, in case it's still listening and the protocol is in a good enough 
state that the server might receive it.  The driver is not explicitly 
terminating the connection upon receiving a copy request, that's just what 
happens when it gets confused.

Kris Jurka



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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 19:09  Altaf Malik <mmalik_altaf@yahoo.com>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Altaf Malik @ 2007-12-13 19:09 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc

I debugged little more and found that this is not because of incomplete copy support. When a response to any of the copy operations is received, JDBC reads next few bytes to ignore the response. But unfortunately driver reads extra four bytes which is the RFQ message so the RFQ message gets corrupted and IOException is thrown considering it as an unknown packet.

I am attaching a patch that fixes this issue and driver works smoothly even after giving the error on copy response.

Regards,
Altaf Malik
EnterpriseDB
http://www.enterprisedb.com/

Kris Jurka <books@ejurka.com> wrote: 

On Thu, 13 Dec 2007, Altaf Malik wrote:

> Also if i enable logging in JDBC, and only execute the COPY statement, 
> log shows that FrontEnd sent a Terminate message.
>

What's happening is that after receiving the server's message that it 
wants to do a COPY, the driver doesn't understand the protocol and dies 
with an "java.io.IOException: Unexpected packet type: " error.  Once this 
happens the driver can't do anything but close the connection.  As part of 
the connection closing process it issues a Terminate message to the 
server, in case it's still listening and the protocol is in a good enough 
state that the server might receive it.  The driver is not explicitly 
terminating the connection upon receiving a copy request, that's just what 
happens when it gets confused.

Kris Jurka


       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

Attachments:

pg_copy.diff (text/x-patch, 972B) download *** org/postgresql/core/v3/QueryExecutorImpl.java 2007-10-15 13:06:24.000000000 +0500 --- org/postgresql/core/v3/QueryExecutorImpl.java 2007-12-13 20:37:28.000000000 +0500 *************** *** 1439,1445 **** int l_len = pgStream.ReceiveInteger4(); /* discard */ ! pgStream.Receive(l_len); handler.handleError(new PSQLException(GT.tr("The driver currently does not support COPY operations."), PSQLState.NOT_IMPLEMENTED)); } --- 1439,1446 ---- int l_len = pgStream.ReceiveInteger4(); /* discard */ ! // read l_len-4 (length includes the 4 bytes for message length itself ! pgStream.Receive(l_len - 4); handler.handleError(new PSQLException(GT.tr("The driver currently does not support COPY operations."), PSQLState.NOT_IMPLEMENTED)); }
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-13 21:31  Kris Jurka <books@ejurka.com>
  parent: Altaf Malik <mmalik_altaf@yahoo.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Kris Jurka @ 2007-12-13 21:31 UTC (permalink / raw)
  To: Altaf Malik <mmalik_altaf@yahoo.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc



On Thu, 13 Dec 2007, Altaf Malik wrote:

> I debugged little more and found that this is not because of incomplete 
> copy support. When a response to any of the copy operations is received, 
> JDBC reads next few bytes to ignore the response. But unfortunately 
> driver reads extra four bytes which is the RFQ message so the RFQ 
> message gets corrupted and IOException is thrown considering it as an 
> unknown packet.

Good catch.

> I am attaching a patch that fixes this issue and driver works smoothly 
> even after giving the error on copy response.
>

Sort of.  In the case of copying data from the server, if you do have a 
large table, you'll generate a SQLException for every row of the copy 
result which isn't great, but I guess it's better than failing completely. 
Trying to copy out a single column table of 100k integers took about seven 
minutes and 150 MB of memory here.

Also, it doesn't work for copying data to the server.  In that case the 
server is sitting waiting for data and the driver is waiting for the 
server and nothing happens.  That was the case before, so this is no 
worse.

Do you have plans to work on this further or shall I apply just this minor 
improvement?

Kris Jurka



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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-14 11:11  Altaf Malik <mmalik_altaf@yahoo.com>
  parent: Kris Jurka <books@ejurka.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Altaf Malik @ 2007-12-14 11:11 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc

Yes i'll take care of that and send another patch.

Regards,
Altaf Malik
EnterpriseDB
http://www.enterprisedb.com/

Kris Jurka <books@ejurka.com> wrote: 

On Thu, 13 Dec 2007, Altaf Malik wrote:

> I debugged little more and found that this is not because of incomplete 
> copy support. When a response to any of the copy operations is received, 
> JDBC reads next few bytes to ignore the response. But unfortunately 
> driver reads extra four bytes which is the RFQ message so the RFQ 
> message gets corrupted and IOException is thrown considering it as an 
> unknown packet.

Good catch.

> I am attaching a patch that fixes this issue and driver works smoothly 
> even after giving the error on copy response.
>

Sort of.  In the case of copying data from the server, if you do have a 
large table, you'll generate a SQLException for every row of the copy 
result which isn't great, but I guess it's better than failing completely. 
Trying to copy out a single column table of 100k integers took about seven 
minutes and 150 MB of memory here.

Also, it doesn't work for copying data to the server.  In that case the 
server is sitting waiting for data and the driver is waiting for the 
server and nothing happens.  That was the case before, so this is no 
worse.

Do you have plans to work on this further or shall I apply just this minor 
improvement?

Kris Jurka


       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

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

* Re: JDBC causing the connection to close after executing a COPY command
@ 2007-12-14 21:50  Altaf Malik <mmalik_altaf@yahoo.com>
  parent: Kris Jurka <books@ejurka.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Altaf Malik @ 2007-12-14 21:50 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc

I am attaching the new patch.

For CopyInResponse, client will send a CopyFail message and server won't wait for the client input. For CopyOutResponse, exception is generated for only CopyOutResponse and not for every CopyData message. Also slightly changed the 'ignoring' mechanism from reading the message to ignore it to skipping the message. I hope this
 will also improve the performance.

Altaf Malik
EnterpriseDB
http://www.enterprisedb.com/

Kris Jurka <books@ejurka.com> wrote:

 In the case of copying data from the server, if you do have a 
large table, you'll generate a SQLException for every row of the copy 
result which isn't great, but I guess it's better than failing completely. 

Trying to copy out a single column table of 100k integers took about seven 
minutes and 150 MB of memory here.

Also, it doesn't work for copying data to the server.  In that case the 
server is sitting waiting for data and the driver is waiting for the 
server and nothing happens.  That was the case before, so this is no 
worse.

Do you have plans to work on this further or shall I apply just this minor 
improvement?

Kris Jurka



          Best Regards
  Muhammad Altaf Malik 
Sr. Software Engineer, 
EnterpriseDB (SMC) (pvt) Ltd.
  3rd floor, Software Technology Park, 
Islamabad. Ph: +92 (051) 2873815
  Cell: 0321-5177961






       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

Attachments:

pg_copy.diff (text/x-patch, 3.8K) download *** org/postgresql/core/v3/QueryExecutorImpl.java 2007-10-15 13:06:24.000000000 +0500 --- org/postgresql/core/v3/QueryExecutorImpl.java 2007-12-15 02:40:49.000000000 +0500 *************** *** 1425,1447 **** break; case 'G': // CopyInResponse case 'H': // CopyOutResponse case 'c': // CopyDone ! case 'd': // CopyData ! { ! // COPY FROM STDIN / COPY TO STDOUT, neither of which are currently ! // supported. ! ! // CopyInResponse can only occur in response to an Execute we sent. ! // Every Execute we send is followed by either a Bind or a ClosePortal, ! // so we don't need to send a CopyFail; the server will fail the copy ! // automatically when it sees the next message. ! ! int l_len = pgStream.ReceiveInteger4(); ! /* discard */ ! pgStream.Receive(l_len); ! handler.handleError(new PSQLException(GT.tr("The driver currently does not support COPY operations."), PSQLState.NOT_IMPLEMENTED)); } break; --- 1425,1469 ---- break; case 'G': // CopyInResponse + if (logger.logDebug()) { + logger.debug(" <=BE CopyInResponse"); + logger.debug(" FE=> CopyFail"); + } + + //COPY sub-protocol is not implemented yet + // We'll send a CopyFail message for COPY FROM STDIN so that server does not wait for the data + // For COPY TO STDOUT we cannot abort the data transfer ... we'll just ignore the messages + + byte[] buf = GT.tr("The driver currently does not support COPY operations.").getBytes(); + pgStream.SendChar('f'); + pgStream.SendInteger4(buf.length + 4 + 1); + pgStream.Send(buf); + pgStream.SendChar(0); + pgStream.flush(); + sendSync(); // send sync message + skipCopyResponse(); // skip the response message + break; + case 'H': // CopyOutResponse + if (logger.logDebug()) + logger.debug(" <=BE CopyOutResponse"); + + skipCopyResponse(); + //In case of CopyOutResponse we cannot abort data transfer - we will just throw error and ignore CopyData messages + handler.handleError(new PSQLException(GT.tr("The driver currently does not support COPY operations."), PSQLState.NOT_IMPLEMENTED)); + break; + case 'c': // CopyDone ! skipCopyResponse(); // skip the response message ! if (logger.logDebug()) { ! logger.debug("<=BE CopyDone"); ! } ! break; ! case 'd': // CopyData ! skipCopyResponse(); // skip the response message ! if (logger.logDebug()) { ! logger.debug("<=BE CopyData"); } break; *************** *** 1452,1457 **** --- 1474,1487 ---- } } + // skipCopyResponse + // ignore the response message by just skipping the next n bytes where n is the message length in bytes + private void skipCopyResponse() throws IOException { + int l_len = pgStream.ReceiveInteger4(); + // skip l_len-4 (length includes the 4 bytes for message length itself + pgStream.Skip(l_len - 4); + } + public synchronized void fetch(ResultCursor cursor, ResultHandler handler, int fetchSize) throws SQLException { final Portal portal = (Portal)cursor;
^ permalink raw reply [nested|flat] 9+ messages in thread


end of thread, other threads:[~2007-12-14 21:50 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-12-13 15:24 JDBC causing the connection to close after executing a COPY command Altaf Malik <mmalik_altaf@yahoo.com>
2007-12-13 15:37 ` Dave Cramer <pg@fastcrypt.com>
2007-12-13 15:54   ` Altaf Malik <mmalik_altaf@yahoo.com>
2007-12-13 16:36     ` Kris Jurka <books@ejurka.com>
2007-12-13 19:09       ` Altaf Malik <mmalik_altaf@yahoo.com>
2007-12-13 21:31         ` Kris Jurka <books@ejurka.com>
2007-12-14 11:11           ` Altaf Malik <mmalik_altaf@yahoo.com>
2007-12-14 21:50           ` Altaf Malik <mmalik_altaf@yahoo.com>
2007-12-13 16:26 ` Kris Jurka <books@ejurka.com>

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