Mysql single connection share by multiple java thread?
772
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?
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)
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
)