From: egashira.yusuke@fujitsu.com <egashira.yusuke@fujitsu.com>
To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: noah@leadboat.com <noah@leadboat.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: RE: Reconnect a single connection used by multiple threads in embedded SQL in C application causes error.
Date: Fri, 4 Mar 2022 11:49:39 +0000
Message-ID: <TYWPR01MB7202C109CDE4D7A60D048F9CFF059@TYWPR01MB7202.jpnprd01.prod.outlook.com> (raw)
In-Reply-To: <20220303.101202.1438042491958386230.horikyota.ntt@gmail.com>
References: <20220223050015.GC3838703@rfd.leadboat.com>
<TYWPR01MB72023E15214B3EF77D53D0A3FF3E9@TYWPR01MB7202.jpnprd01.prod.outlook.com>
<TYWPR01MB72027307B41182F8A12524ADFF039@TYWPR01MB7202.jpnprd01.prod.outlook.com>
<20220303.101202.1438042491958386230.horikyota.ntt@gmail.com>
Hi Horiguchi-san,
Thank you for the comment.
I attached the revised patch.
> It looks like too verbose. Couldn't it be summarized as something
> like this?
>
> > Current connection is managed in a per-thread manner. For the first
> > use on a thread, it is the most recently used one in the process.
Yes.
I thought that I needed a detailed explanation of the current connection for the note below, but this certainly seems to be summable.
> This doesn't look like a API documentation but a procedual
> documentation. IMO there's no particular reason that we should
> officially concretely suggest how mulitple connectinos are managed on
> somebody's application. And part of the section is already in the
> documentation.
>
> | <para>
> | If your application uses multiple threads of execution, they cannot share a
> | connection concurrently. You must either explicitly control access to the connection
> | (using mutexes) or use a connection for each thread.
> | </para>
>
> So, it would boil down to the caution about disconnection.
>
> > Note that connections are shared among all threads. Be careful not
> > to DISCONNECT a connection that is to be used in another thread.
I wanted to make a document that could avoid the use cases that I reported, so I tried to write a note according to the occurrence conditions.
However, this was also too verbose, so I rewrote the note as you suggested.
And since there is no mention of the current connection, I changed the section to add it to "36.2.3. Closing a Connection".
I think I might be better to add "In the worst case, this may cause an application crash." to this note so that users can avoid the problem more strongly.
However, this sentence can also be read as claiming that there is an implementation problem.
Should not I add this sentence?
I am also a little concerned that the revised text will be interpreted as prohibiting the following use cases:
> - One thread does all the CONNECT and DISCONNECT commands. Other threads just use the default connection, with mutual exclusion.
Regards,
Yusuke Egashira
Attachments:v2-add-multithreaded-note-to-ecpg-connection-document.patch (application/octet-stream, 1.9K) downloaddiff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgmlindex cdc4761c60..5bc8ca1faa 100644--- a/doc/src/sgml/ecpg.sgml+++ b/doc/src/sgml/ecpg.sgml@@ -222,10 +222,15 @@ EXEC SQL CONNECT TO <replaceable>target</replaceable> <optional>AS <replaceable>
<para>
The <replaceable>connection-name</replaceable> is used to handle
multiple connections in one program. It can be omitted if a
- program uses only one connection. The most recently opened- connection becomes the current connection, which is used by default- when an SQL statement is to be executed (see later in this- chapter).+ program uses only one connection.+ </para>++ <para>+ The most recently opened connection becomes the current connection.+ The current connection is managed in a per-thread manner. For the+ first use on a thread, it is the most recently opened one in the+ process. The current connection is used by default when an SQL+ statement is to be executed (see later in this chapter).
</para>
<para>
@@ -276,8 +281,8 @@ EXEC SQL CONNECT TO :target USER :user USING :passwd;
<para>
SQL statements in embedded SQL programs are by default executed on
- the current connection, that is, the most recently opened one. If- an application needs to manage multiple connections, then there are+ the current connection, that is, the most recently opened one in each thread.+ If an application needs to manage multiple connections, then there are
three ways to handle this.
</para>
@@ -439,6 +444,13 @@ EXEC SQL DISCONNECT <optional><replaceable>connection</replaceable></optional>;
It is good style that an application always explicitly disconnect
from every connection it opened.
</para>
++ <note>+ <para>+ Connections are shared among all threads. Be careful not to+ disconnect a connection that is to be used in another thread.+ </para>+ </note>
</sect2>
</sect1>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: egashira.yusuke@fujitsu.com, horikyota.ntt@gmail.com, noah@leadboat.com
Subject: Re: Reconnect a single connection used by multiple threads in embedded SQL in C application causes error.
In-Reply-To: <TYWPR01MB7202C109CDE4D7A60D048F9CFF059@TYWPR01MB7202.jpnprd01.prod.outlook.com>
References: <20220223050015.GC3838703@rfd.leadboat.com> <TYWPR01MB72023E15214B3EF77D53D0A3FF3E9@TYWPR01MB7202.jpnprd01.prod.outlook.com> <TYWPR01MB72027307B41182F8A12524ADFF039@TYWPR01MB7202.jpnprd01.prod.outlook.com> <20220303.101202.1438042491958386230.horikyota.ntt@gmail.com> <TYWPR01MB7202C109CDE4D7A60D048F9CFF059@TYWPR01MB7202.jpnprd01.prod.outlook.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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