File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple threads dealing db

 
mohamed hamdy
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i don't know if multiple threads attempts to query and update a data base can conflict the returning result sets or the updated or inserted records.
am i must use synchronization?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDBC drivers should be threadsafe. Beware, though, that on a given (Prepared|Callable)Statement, executing a query will normally close any open ResultSets, so best just restrict the multithreading to the Connection.
- Peter
 
mohamed hamdy
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
do you mean that i must use one connection for all threads, i intended to do that and use one ResultSet for each table or join that i restrict
(synchronize) access on it, but my supervisor conflicted me when he told me that i can use a new connection for each thread.
,thanks in advance
 
mohamed hamdy
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one another thing ,if i will not use prepared or collable statement ,do i need not synchronization.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDBC Tutorial and Reference states All operations on java.sql and javax.sql objects are required to be multithread safe. They must be able to cope correctly with having several threads simultaneously calling the same object. In other words, a statement execution in one thread should not block an execution in another thread. In particular, JDBC drivers should
operate correctly when used from multiple threads.
This would mean that multiple threads can use one and the same Connection (but don't have to), as long as they all used their own Statements (regardless of the flavour of Statement). Whether this is really what you want to do is something else. Often you'll want the threads to use separate transactions, for instance. Using multiple threads against a single Connection may also well lead to problems with drivers that aren't quite as threadsafe as they should be. I'd personally try to avoid it if I could.
- Peter
[ February 24, 2003: Message edited by: Peter den Haan ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic