File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes multiple threads dealing db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "multiple threads dealing db" Watch "multiple threads dealing db" New topic
Author

multiple threads dealing db

mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Feb 13, 2003
Posts: 72
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

Joined: Feb 13, 2003
Posts: 72
one another thing ,if i will not use prepared or collable statement ,do i need not synchronization.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
 
GeeCON Prague 2014
 
subject: multiple threads dealing db