Java JDBC connection status

前端 未结 6 1271
太阳男子
太阳男子 2020-12-02 19:52

I am (successfully) connecting to a database using the following:

java.sql.Connection connect = DriverManager.getConnection(
  \"jdbc:mysql://localhost/some_         


        
6条回答
  •  忘掉有多难
    2020-12-02 20:32

    Your best chance is to just perform a simple query against one table, e.g.:

    select 1 from SOME_TABLE;
    

    Oh, I just saw there is a new method available since 1.6:

    java.sql.Connection.isValid(int timeoutSeconds):

    Returns true if the connection has not been closed and is still valid. The driver shall submit a query on the connection or use some other mechanism that positively verifies the connection is still valid when this method is called. The query submitted by the driver to validate the connection shall be executed in the context of the current transaction.

提交回复
热议问题