o ne of the policy of general database connection pool is creating new connection for every thread who request database connection by calling getConnection() method(normally,this method is synchronized.I wondered is there any synchronization problem if give the same connection object to mutiple threads.On the other words,is it safe if two thread use the same connection object concurrency? [ June 16, 2003: Message edited by: Sun LiWei ]
Joined: Jan 14, 2003
This policy is no where near safe. You will encounter huge problems if multiple threads are attempting to use the same connection simultaneously. The best way to use connections is to create a pool of connections then each thread can request a connection from the pool, have exclusive use of the connectiona nd then release it back into the pool when it has completed it's database operations.
Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Hi Sun, In my experience with Oracle 126.96.36.199 database on SUN [sparc] Solaris 7 and J2SE 1.3.1_02, when two threads attempt to access the same "Connection" object simultaneously, the second one will block until the first one completes its request. For example, if two separate threads invoke the "executeQuery()" method (in class "java.sql.Statement"), the second thread is blocked, until the first thread invokes the "close()" method. Then the second thread's invocation (of "executeQuery()") is executed. Note that both threads, eventually, receive their requested "ResultSet". Obviously you need to decide whether this constitutes being "thread safe" (or not). Hope this has helped. Good Luck, Avi.