Thread: Clarification on thread-safety
The docs have a very brief page on thread-safety: https://jdbc.postgresql.org/documentation/head/thread.html
It states, pretty categorically: "The PostgreSQL™ JDBC driver is not thread safe".
However, it's common to want to open new connections from multiple threads.
A cursory look at `PGSimpleDataSource` and `org.postgresql.Driver.connect` suggests both of these methods are thread safe.
Perhaps this is an "obvious" level of thread-safety, but I think the doc page spelling it out would save other paranoid folks like me some time.
The term "The PostgreSQL™ JDBC driver" refers to the project as a whole. Obviously certain classes and methods *may* be (inherently) thread-safe, but "The PostgreSQL™ JDBC driver" project is not developed for a multi-threaded use, and the majority of the functionality (Connections, Statements, Result Sets, etc.) can *only* be used by a single thread. На пт, 27.05.2022 г. в 10:24 ч. Martin Pärtel <martin.partel@gmail.com> написа: > > The docs have a very brief page on thread-safety: https://jdbc.postgresql.org/documentation/head/thread.html > It states, pretty categorically: "The PostgreSQL™ JDBC driver is not thread safe". > However, it's common to want to open new connections from multiple threads. > > A cursory look at `PGSimpleDataSource` and `org.postgresql.Driver.connect` suggests both of these methods are thread safe. > Perhaps this is an "obvious" level of thread-safety, but I think the doc page spelling it out would save other paranoidfolks like me some time. >
Yes, that was my understanding.
The docs page says "The driver makes no guarantees that methods on connections are synchronized." and mentions "TimestampUtils" as a "notable exception".
I'm just suggesting that it would be good to document "getConnection" and "connect" and other similar methods as thread-safe too, since that property is especially useful (and I'd guess often assumed) for them.
On Fri, May 27, 2022 at 11:59 AM Lachezar Dobrev <l.dobrev@gmail.com> wrote:
The term "The PostgreSQL™ JDBC driver" refers to the project as a
whole. Obviously certain classes and methods *may* be (inherently)
thread-safe, but "The PostgreSQL™ JDBC driver" project is not
developed for a multi-threaded use, and the majority of the
functionality (Connections, Statements, Result Sets, etc.) can *only*
be used by a single thread.
На пт, 27.05.2022 г. в 10:24 ч. Martin Pärtel <martin.partel@gmail.com> написа:
>
> The docs have a very brief page on thread-safety: https://jdbc.postgresql.org/documentation/head/thread.html
> It states, pretty categorically: "The PostgreSQL™ JDBC driver is not thread safe".
> However, it's common to want to open new connections from multiple threads.
>
> A cursory look at `PGSimpleDataSource` and `org.postgresql.Driver.connect` suggests both of these methods are thread safe.
> Perhaps this is an "obvious" level of thread-safety, but I think the doc page spelling it out would save other paranoid folks like me some time.
>
On Fri, 27 May 2022 at 12:19, Martin Pärtel <martin.partel@gmail.com> wrote:
Yes, that was my understanding.The docs page says "The driver makes no guarantees that methods on connections are synchronized." and mentions "TimestampUtils" as a "notable exception".I'm just suggesting that it would be good to document "getConnection" and "connect" and other similar methods as thread-safe too, since that property is especially useful (and I'd guess often assumed) for them.On Fri, May 27, 2022 at 11:59 AM Lachezar Dobrev <l.dobrev@gmail.com> wrote:The term "The PostgreSQL™ JDBC driver" refers to the project as a
whole. Obviously certain classes and methods *may* be (inherently)
thread-safe, but "The PostgreSQL™ JDBC driver" project is not
developed for a multi-threaded use, and the majority of the
functionality (Connections, Statements, Result Sets, etc.) can *only*
be used by a single thread.
На пт, 27.05.2022 г. в 10:24 ч. Martin Pärtel <martin.partel@gmail.com> написа:
>
> The docs have a very brief page on thread-safety: https://jdbc.postgresql.org/documentation/head/thread.html
> It states, pretty categorically: "The PostgreSQL™ JDBC driver is not thread safe".
> However, it's common to want to open new connections from multiple threads.
>
> A cursory look at `PGSimpleDataSource` and `org.postgresql.Driver.connect` suggests both of these methods are thread safe.
> Perhaps this is an "obvious" level of thread-safety, but I think the doc page spelling it out would save other paranoid folks like me some time.
>
The above seems reasonable.
Martin, could you perhaps propose some language ?
Dave
Sure:
"""
Most classes and methods in the PostgreSQL™ JDBC driver are not thread safe. For instance, you may not call methods on the same connection object from multiple threads without synchronizing access to the connection yourself. However it is often more convenient and performant to open a separate connection for each thread or use a connection pooling library.
The following classes and methods are guaranteed to be thread-safe:
* org.postgresql.Driver
* Getter methods on DataSource implementations, including methods getConnection and getPooledConnection. Setter methods are not thread-safe.
* org.postgresql.jdbc.TimestampUtils
The following classes and methods are guaranteed to be thread-safe:
* org.postgresql.Driver
* Getter methods on DataSource implementations, including methods getConnection and getPooledConnection. Setter methods are not thread-safe.
* org.postgresql.jdbc.TimestampUtils
"""
On Fri, May 27, 2022 at 8:45 PM Dave Cramer <davecramer@postgres.rocks> wrote:
On Fri, 27 May 2022 at 12:19, Martin Pärtel <martin.partel@gmail.com> wrote:Yes, that was my understanding.The docs page says "The driver makes no guarantees that methods on connections are synchronized." and mentions "TimestampUtils" as a "notable exception".I'm just suggesting that it would be good to document "getConnection" and "connect" and other similar methods as thread-safe too, since that property is especially useful (and I'd guess often assumed) for them.On Fri, May 27, 2022 at 11:59 AM Lachezar Dobrev <l.dobrev@gmail.com> wrote:The term "The PostgreSQL™ JDBC driver" refers to the project as a
whole. Obviously certain classes and methods *may* be (inherently)
thread-safe, but "The PostgreSQL™ JDBC driver" project is not
developed for a multi-threaded use, and the majority of the
functionality (Connections, Statements, Result Sets, etc.) can *only*
be used by a single thread.
На пт, 27.05.2022 г. в 10:24 ч. Martin Pärtel <martin.partel@gmail.com> написа:
>
> The docs have a very brief page on thread-safety: https://jdbc.postgresql.org/documentation/head/thread.html
> It states, pretty categorically: "The PostgreSQL™ JDBC driver is not thread safe".
> However, it's common to want to open new connections from multiple threads.
>
> A cursory look at `PGSimpleDataSource` and `org.postgresql.Driver.connect` suggests both of these methods are thread safe.
> Perhaps this is an "obvious" level of thread-safety, but I think the doc page spelling it out would save other paranoid folks like me some time.>The above seems reasonable.Martin, could you perhaps propose some language ?Dave