MySQL connection validity test in datasource : SELECT 1 or something better?

无人久伴 提交于 2019-11-30 18:41:56
Sebas

Quoting this link: Ping MySQL Server Using JDBC

you have to:

The MySQL JDBC driver (Connector/J) provides a ping mechanism.

If you do a SQL query prepended with /* ping */ such as:

"/* ping */ SELECT 1" This will actually cause the driver send a ping to the server and return a fake, light-weight, result set.

(You can find this buried fairly deep in the Connector/J documentation; search for "ping" on that page. Read it carefully: this mechanism is very sensitive to the syntax used. Unlike most SQL, the "parsing" of the "ping" marker happens in the client-side JDBC driver itself.).

The query we use at work to check the connection to a MySQL server is SHOW databases; which is nice and simple as it doesn't need you to have selected a particular database. I'd say it's pretty reliable and a query like that is pretty efficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!