Re: Postgres driver bug

From: Nikos Viorres <nviorres(at)gmail(dot)com>
To: Dave Cramer <pg(at)fastcrypt(dot)com>
Cc: List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Postgres driver bug
Date: 2015-02-06 14:31:11
Message-ID: CAHSf1Y2PvXHPTyrqFc9bZNY5y4WqE+p0dofVnZ75wnib9uKtFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Well, the driver shouldn't be waiting for input since the InpuStream has
been closed, this is a try with resources. I think that the lock should be
released as soon as the IS is closed.

regards

On Fri, Feb 6, 2015 at 4:27 PM, Dave Cramer <pg(at)fastcrypt(dot)com> wrote:

> IIRC this is due to the driver waiting for input from the client ?
>
> What would you suggest it do ?
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
> On 6 February 2015 at 09:19, Nikos Viorres <nviorres(at)gmail(dot)com> wrote:
>
>> Hi Dave,
>>
>> I am using 9.3-1102-jdbc4 version of the driver. You are correct, getOutputStream()
>> will always throw an exception after getInputStream has been called so as
>> to avoid reading the bytes from the IS and releasing the internal lock that
>> the driver keeps and thus always resulting in an infinite wait.
>>
>> regards
>>
>> On Fri, Feb 6, 2015 at 3:23 PM, Dave Cramer <pg(at)fastcrypt(dot)com> wrote:
>>
>>> Nikos,
>>>
>>> What version of the driver are you using ?
>>>
>>> From what I see getOutputStream() will always throw an exception ?
>>>
>>> Dave
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>> On 6 February 2015 at 03:57, Nikos Viorres <nviorres(at)gmail(dot)com> wrote:
>>>
>>>> Hi,
>>>>
>>>> The attached simple piece of code results always in a the driver going
>>>> into a wait state that never receives a wake up signal and by looking at
>>>> the code, even if it does it will loop again into the same wait state. If
>>>> anyone else thinks this is a problem i could provide a fix
>>>>
>>>> regards
>>>>
>>>>
>>>>
>>>> package nvrs.test;
>>>>
>>>> import org.postgresql.PGConnection;
>>>> import org.postgresql.copy.PGCopyInputStream;
>>>>
>>>> import java.io.FileNotFoundException;
>>>> import java.io.IOException;
>>>> import java.io.InputStream;
>>>> import java.io.OutputStream;
>>>> import java.sql.Connection;
>>>> import java.sql.DriverManager;
>>>> import java.sql.SQLException;
>>>>
>>>> /**
>>>> * Date: 5/2/2015
>>>> */
>>>> public class PostgresDriverBug {
>>>>
>>>> public static void main(String...args) throws SQLException {
>>>> PostgresConnectionFactory connectionFactory
>>>> = new PostgresConnectionFactory("localhost", 5432, "postgres",
>>>> "postgres", "postgres");
>>>>
>>>> Connection con = connectionFactory.getNewConnection();
>>>>
>>>>
>>>> try (InputStream is = getPostgresInputStream("select * from
>>>> public.some_table", con);
>>>> OutputStream os = getOutputStream()) {
>>>>
>>>> final byte[] buffer = new byte[1024 * 4];
>>>> int read = is.read(buffer);
>>>>
>>>> while (read > 0) {
>>>> os.write(buffer, 0, read);
>>>> read = is.read(buffer);
>>>> }
>>>>
>>>> } catch (IOException e) {
>>>> System.err.println("Something bad happened " + e.getMessage());
>>>>
>>>> con.rollback();
>>>> }
>>>>
>>>> System.out.println("Done!");
>>>> }
>>>>
>>>> private static OutputStream getOutputStream() throws IOException {
>>>> throw new FileNotFoundException("ooops");
>>>> }
>>>>
>>>> private static InputStream getPostgresInputStream(String selectQuery,
>>>> Connection con)
>>>> throws SQLException {
>>>>
>>>> return new PGCopyInputStream(PGConnection.class.cast(con),
>>>> "COPY (" + selectQuery +") TO STDOUT");
>>>> }
>>>>
>>>> public static class PostgresConnectionFactory {
>>>> private final String host;
>>>> private final int port;
>>>> private final String database;
>>>> private final String user;
>>>> private final String password;
>>>>
>>>> public PostgresConnectionFactory(final String host, final int port,
>>>> final String database,
>>>> final String user, final String
>>>> password) {
>>>> this.host = host;
>>>> this.port = port;
>>>> this.database = database;
>>>> this.user = user;
>>>> this.password = password;
>>>> }
>>>>
>>>> private String getConnectionUrl() {
>>>> return "jdbc:postgresql://" + host + ":" + port + "/" + database;
>>>> }
>>>>
>>>> public Connection getNewConnection() throws SQLException {
>>>> Connection con = DriverManager.getConnection(getConnectionUrl(),
>>>> user,
>>>> password);
>>>>
>>>> con.setAutoCommit(false);
>>>> return con;
>>>> }
>>>> }
>>>>
>>>> }
>>>>
>>>
>>>
>>
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2015-02-06 14:40:30 Re: Postgres driver bug
Previous Message Dave Cramer 2015-02-06 14:27:02 Re: Postgres driver bug