Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a java application with multiple thread started and they are all sharing a single db connection created in the main function. So far things have been running smoothly. I am worried will there be any problem in future like corruption etc?

share|improve this question
up vote 2 down vote accepted

You will have problem if:
- you use transactions. Two threads starting transactions on the same connection would not be nice for your datas. Think about what mysql will do if one thread want a rollback
- you use auto increment and LAST_INSERT_ID. If two threads work on the same table at the same moment, the last id is the same for both if they share the same connection
- and maybe much more problems if you don't only use "simple" things (aka select/insert/update)

share|improve this answer
thank you for the input yes each of my thread is using one transaction and sure enough I am using last_insert_id too. So what is your best suggestion then use individual connection is it but that will end up with too many connection problems right? – user111196 Mar 11 '12 at 4:55
so far I have been experimenting with 20 thread looks ok on the last_insert_id not much conflicts? But do you expect in future there might be a problem is it? – user111196 Mar 11 '12 at 5:07
I just saw your comments. You can use really lots of connections before this will be a problem. Just adjust mysql config file in case you have really too many, but in this case you'll have a big server so no problem at all. – Gregory MOUSSAT May 23 '12 at 2:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.