Thread: Multi-thread use of a connection
The documentation states the jdbc driver is thread safe allowing multiple threads to share the same connection. The documentation only talks about queries. So my question is: What if 2 threads are doing transactions and one wants to commit its data, does this mean that the other threads data will also be committed even though it hasn't completed it's transaction or is the driver aware of this?
Chris White
The 7.2 Programmers Guide Chapter 8, JDBC Interface section 8.8. -----Original Message----- From: Dave Cramer [mailto:davec@ebox.com] Sent: Monday, July 22, 2002 3:05 PM To: cjwhite@cisco.com Cc: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Multi-thread use of a connection Chris, AFAIK, two threads cannot share a connection. Which documentation are you referring to? Either way you can only have one transaction open on a connection. Dave On Mon, 2002-07-22 at 17:12, Chris White wrote: > The documentation states the jdbc driver is thread safe allowing multiple > threads to share the same connection. The documentation only talks about > queries. So my question is: What if 2 threads are doing transactions and one > wants to commit its data, does this mean that the other threads data will > also be committed even though it hasn't completed it's transaction or is the > driver aware of this? > > Chris White
Chris White wrote: > The documentation states the jdbc driver is thread safe allowing > multiple threads to share the same connection. The documentation only > talks about queries. So my question is: What if 2 threads are doing > transactions and one wants to commit its data, does this mean that the > other threads data will also be committed even though it hasn't > completed it's transaction or is the driver aware of this? > This is really more of a generic jdbc question and isn't really postgres specific, but I will attempt to answer. The postgres jdbc driver should be thread safe (but I don't believe it has received a lot of testing in multi-threaded situations so there may be some bugs). This applies to both queries and updates. When it comes to transactions and threads, I think you misunderstand how jdbc is supposed to work. The connection defines the scope of the transaction. Threads really have nothing to do with transactions. Thus if two threads are both using the same connection there is one transaction (one connection = one transaction). If either thread ends the transaction (by calling either commit or rollback on the connection) it is ended for both threads. It is for this reason that you generally don't have multiple threads accessing the same connection because it becomes very difficult to keep track of the transaction. thanks, --Barry
That is what I understood about transactions and a connection and have coded it to ensure only one transaction is going on a connection. However, but somebody told me postgres was thread safe and was able handle multiple transactions on the same connection. -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Barry Lind Sent: Monday, July 22, 2002 7:35 PM To: cjwhite@cisco.com Cc: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Multi-thread use of a connection Chris White wrote: > The documentation states the jdbc driver is thread safe allowing > multiple threads to share the same connection. The documentation only > talks about queries. So my question is: What if 2 threads are doing > transactions and one wants to commit its data, does this mean that the > other threads data will also be committed even though it hasn't > completed it's transaction or is the driver aware of this? > This is really more of a generic jdbc question and isn't really postgres specific, but I will attempt to answer. The postgres jdbc driver should be thread safe (but I don't believe it has received a lot of testing in multi-threaded situations so there may be some bugs). This applies to both queries and updates. When it comes to transactions and threads, I think you misunderstand how jdbc is supposed to work. The connection defines the scope of the transaction. Threads really have nothing to do with transactions. Thus if two threads are both using the same connection there is one transaction (one connection = one transaction). If either thread ends the transaction (by calling either commit or rollback on the connection) it is ended for both threads. It is for this reason that you generally don't have multiple threads accessing the same connection because it becomes very difficult to keep track of the transaction. thanks, --Barry ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
Chris, AFAIK, two threads cannot share a connection. Which documentation are you referring to? Either way you can only have one transaction open on a connection. Dave On Mon, 2002-07-22 at 17:12, Chris White wrote: > The documentation states the jdbc driver is thread safe allowing multiple > threads to share the same connection. The documentation only talks about > queries. So my question is: What if 2 threads are doing transactions and one > wants to commit its data, does this mean that the other threads data will > also be committed even though it hasn't completed it's transaction or is the > driver aware of this? > > Chris White