Is it safe to use a static java.sql.Connection instance in a multithreaded system?

后端 未结 2 2082
长情又很酷
长情又很酷 2020-11-22 04:43

I\'m running a web application on Tomcat. I have a class that handles all DB queries. This class contains the Connection object and methods that returns query r

2条回答
  •  旧巷少年郎
    2020-11-22 05:06

    If you are only running Select queries (searchUser sounds like only selecting data) there will be no issues, apart from thread contention.

    As far as I know, a Connection can only handle one query at a time, so by using a single instance you will essentially serialize database access. But this does not necessarily mean, it is always safe to access a database like this in a multi threaded environment. There might still be issues if concurrent accesses are interleaving.

提交回复
热议问题