Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes Can statement be used in multi-thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Can statement be used in multi-thread?" Watch "Can statement be used in multi-thread?" New topic

Can statement be used in multi-thread?

William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69
Hi ranchers,
I am new to JDBC,
I wonder if the statement can be used in multi-thread. I mean i create a statement object by Connection. And pass the reference to mutli-thread, then mutli-thread uses the statement to executeQuery, can it work? And can preparedStatement work that way?

i write some code to test

It seems to work fine, every thread get the correct result.
But is there any guarantee?

I know Connection can be used concurrently by many users, but how many is appropriate?

Thank you in advance,

Java is better and better, yet bitter and bitter.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
SCJP is the wrong place to post this question. Moving to somewhere where topic is likely to be answered. Let's try JDBC.

- dumplicated here!
[ November 28, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

In my view.. you should NOT use same statement in muti threaded environment. A statement is tightly coupled with Connection, and any commit/rollback will affect operation of every thread...


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
I agree. Here's the link:
subject: Can statement be used in multi-thread?
It's not a secret anymore!