Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gi0HH-0005Af-O2 for pgsql-jdbc@arkaria.postgresql.org; Fri, 11 Jan 2019 17:07:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gi0HG-0003h8-9Z for pgsql-jdbc@arkaria.postgresql.org; Fri, 11 Jan 2019 17:07:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gi0HF-0003dZ-Uh for pgsql-jdbc@lists.postgresql.org; Fri, 11 Jan 2019 17:07:26 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gi0HD-0003SR-38 for pgsql-jdbc@lists.postgresql.org; Fri, 11 Jan 2019 17:07:24 +0000 Received: by mail-pf1-x42e.google.com with SMTP id w73so7217936pfk.10 for ; Fri, 11 Jan 2019 09:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=x0yfY/BqLAUYuiCOeikOf7nfHkvOrsBHjXVPH5bRuSM=; b=gkhgwzxLz6NtUOrjyTm7jjsor6XNcagT/zBsJg40jh7TIb2Iwm2m8wFaNreiNSlrK9 fPkf+Ll75izCr0NtuvNlEV8a9emkmo6TXl+yj8tEfgF90ayaLDISF3tKqkBbEQH9allK UiOOp3l2J4kjgyfa/eJCSQ31mIjrVc2kTLuc4U/OrdHQQINNuJ27/iEuQgUf6ycxWyCq e4PHpeaCtNKK6om4D4f27ufwkLd1F3tUOKd2moE+d4n7+qA11hluHImWHHFZc66HlrJ9 dY+HI4ULtFUuBqzPC5Rpv2+uThwkHhRUSfbTM60hGZhdmXcJlHidoVI5sadLjJQm9gHh yx8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=x0yfY/BqLAUYuiCOeikOf7nfHkvOrsBHjXVPH5bRuSM=; b=Cu3zfMiAqlAJhSF6Fep6Pavt6CB7RUbqtwf/fxL8MiuGSzTj4gXV4KOSgVNpAHa3+q EECmCIED1qT6vKbgX/OQAmVDWHU7o4Lwb/vdRODUlh/PxycgUYGYZa3CUgP3a8AeXq8L e95bW4O2lBtaTl/I83RDrzmP3r5dKzpwaiJsrieBMELdrgClwHLROxO5InRv72zT5PMc Axl4vs0pmZ44DlDsMiZ46Y8EXEAW7mKcACHxZGbevauSZ3y/jVnWUDofzHyVfSkgQeeP mGvSopm8tKIAsEgQy/Y9FA26QtgivhJ1fXZ78yhkiDY6x7ZL0cQQSDLubSdTB0zb5d6k nvWA== X-Gm-Message-State: AJcUukfVFoG5doU6o2YbOsvoIsEsgo/vzHA1Sq4dZDCyQo8DyZoFsalR okUNL+iSsUHSbM1G+RXKw5SoVtWo X-Google-Smtp-Source: ALg8bN6GtUJ/FHhNVHBsUYDa7ABqY7fp0yVL3sh8/cPH7CeI6uad51PyW91HKgJR13t47jH1H+X9kg== X-Received: by 2002:a62:9f1b:: with SMTP id g27mr14894136pfe.87.1547226441553; Fri, 11 Jan 2019 09:07:21 -0800 (PST) Received: from [10.104.134.14] ([155.100.47.6]) by smtp.gmail.com with ESMTPSA id v15sm112250218pfn.94.2019.01.11.09.07.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Jan 2019 09:07:20 -0800 (PST) Subject: Re: PGCopyOutputStream close() v. endCopy() To: Sehrope Sarkuni Cc: pgsql-jdbc@lists.postgresql.org References: <25bf5fc6-1dd0-0944-61e7-0eae6c6614c7@gmail.com> From: Rob Sargent Message-ID: <2bc9ac77-062a-bcd5-7037-41dcc766f620@gmail.com> Date: Fri, 11 Jan 2019 10:07:19 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------FE12E3999158BA37C8F4C15C" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------FE12E3999158BA37C8F4C15C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Thanks you, very much. I'll revert to using close() (even though the try-with will also as it make me more comfortable). On 1/11/19 6:36 AM, Sehrope Sarkuni wrote: > You're getting that error because endCopy() is being called twice. > Once by you explicitly in deliverSegments() and again when the > PGCopyOutputStream gets automatically closed by the > try-with-resources: > https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/copy/PGCopyOutputStream.java#L98-L112 > > PGCopyOutputStream is a wrapper CopyIn (write to OutputStream => COPY > TO STDIN) that adds some buffering. You shouldn't be calling endCopy() > on it. > > You can either: > > 1. Remove the PGCopyOutputStream.endCopy() entirely as close() will > handle it. If do not need the final row count then you're done. > 2. Use CopyIn directly, manage the buffer yourself, and call endCopy() > on it. > 3. Manually manage the resources for PGCopyOutputStream so you can > reference it for getHandledRowCount() after the close(). > 4. Call close() instead of endCopy() on PGCopyOutputStream as it > handles being invoked more than once (it's a no-op for subsequent > close() calls). > > I'm going to take a look why PGCopyOutputStream.endCopy() is declared > public as currently there's no way to call both it and close() without > getting an error, and you're definitely supposed to call close(). > Either it should not be public or it should be performing the same > work as close(), i.e. clearing the internal "op" (copy operation) > member to indicate that the close is complete so subsequent close() > calls are not errant. > > Regards, > -- Sehrope Sarkuni > Founder & CEO | JackDB, Inc. | https://www.jackdb.com/ --------------FE12E3999158BA37C8F4C15C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Thanks you, very much. 

I'll revert to using close() (even though the try-with will also as it make me more comfortable).

On 1/11/19 6:36 AM, Sehrope Sarkuni wrote:
You're getting that error because endCopy() is being called twice. Once by you explicitly in deliverSegments() and again when the PGCopyOutputStream gets automatically closed by the try-with-resources: https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/copy/PGCopyOutputStream.java#L98-L112

PGCopyOutputStream is a wrapper CopyIn (write to OutputStream => COPY TO STDIN) that adds some buffering. You shouldn't be calling endCopy() on it.

You can either:

1. Remove the PGCopyOutputStream.endCopy() entirely as close() will handle it. If do not need the final row count then you're done.
2. Use CopyIn directly, manage the buffer yourself, and call endCopy() on it.
3. Manually manage the resources for PGCopyOutputStream so you can reference it for getHandledRowCount() after the close().
4. Call close() instead of endCopy() on PGCopyOutputStream as it handles being invoked more than once (it's a no-op for subsequent close() calls).

I'm going to take a look why PGCopyOutputStream.endCopy() is declared public as currently there's no way to call both it and close() without getting an error, and you're definitely supposed to call close(). Either it should not be public or it should be performing the same work as close(), i.e. clearing the internal "op" (copy operation) member to indicate that the close is complete so subsequent close() calls are not errant.

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/
--------------FE12E3999158BA37C8F4C15C--