Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I reuse connection ?

 
Sun LiWei
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sun,
In my experience with Oracle 8.1.7.4 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic