Efficient SQL test query or validation query that will work across all (or most) databases

SqlConnection Pooling

Sql Problem Overview


Many database connection pooling libraries provide the ability to test their SQL connections for idleness. For example, the JDBC pooling library c3p0 has a property called preferredTestQuery, which gets executed on the connection at configured intervals. Similarly, Apache Commons DBCP has validationQuery.

Many example queries I've seen are for MySQL and recommend using SELECT 1; as the value for the test query. However, this query doesn't work on some databases (e.g. HSQLDB, for which SELECT 1 expects a FROM clause).

Is there a database-agnostic query that's equivalently efficient but will work for all SQL databases?

Edit:

If there's not (which seems to be the case), can somebody suggest a set of SQL queries that will work for various database providers? My intention would be to programmatically determine a statement I can use based on my database provider configuration.

Sql Solutions


Solution 1 - Sql

After a little bit of research along with help from some of the answers here:

SELECT 1

  • H2
  • MySQL
  • Microsoft SQL Server (according to NimChimpsky)
  • PostgreSQL
  • SQLite
  • Hive


SELECT 1 FROM DUAL

  • Oracle


SELECT 1 FROM any_existing_table WHERE 1=0

or

SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS

or

CALL NOW()

  • HSQLDB (tested with version 1.8.0.10)

    Note: I tried using a WHERE 1=0 clause on the second query, but it didn't work as a value for Apache Commons DBCP's validationQuery, since the query doesn't return any rows


VALUES 1 or SELECT 1 FROM SYSIBM.SYSDUMMY1

SELECT 1 FROM SYSIBM.SYSDUMMY1

  • DB2

select count(*) from systables

  • Informix

Solution 2 - Sql

If your driver is JDBC 4 compliant, there is no need for a dedicated query to test connections. Instead, there is Connection.isValid to test the connection.

JDBC 4 is part of Java 6 from 2006 and you driver should support this by now!

Famous connection pools, like HikariCP, still have a config parameter for specifying a test query but strongly discourage to use it:

> ï” connectionTestQuery > > If your driver supports JDBC4 we strongly > recommend not setting this property. This is for "legacy" databases > that do not support the JDBC4 Connection.isValid() API. This is the > query that will be executed just before a connection is given to you > from the pool to validate that the connection to the database is still > alive. Again, try running the pool without this property, HikariCP > will log an error if your driver is not JDBC4 compliant to let you > know. Default: none

Solution 3 - Sql

Unfortunately there is no SELECT statement that will always work regardless of database.

Most databases support:

SELECT 1

Some databases don't support this but have a table called DUAL that you can use when you don't need a table:

SELECT 1 FROM DUAL

MySQL also supports this for compatibility reasons, but not all databases do. A workaround for databases that don't support either of the above is to create a table called DUAL that contains a single row, then the above will work.

HSQLDB supports neither of the above, so you can either create the DUAL table or else use:

SELECT 1 FROM any_table_that_you_know_exists_in_your_database

Solution 4 - Sql

The jOOQ manual's section about the DUAL table lists the following for jOOQ's select(inline(1)) query:

-- Access
SELECT 1 FROM (SELECT count(*) dual FROM MSysResources) AS dual

-- BigQuery, CockroachDB, Exasol, H2, Ignite, MariaDB, MySQL, PostgreSQL, 
-- Redshift, Snowflake, SQLite, SQL Server, Sybase ASE, Vertica
SELECT 1

-- MemSQL, Oracle
SELECT 1 FROM DUAL

-- CUBRID
SELECT 1 FROM db_root

-- Db2
SELECT 1 FROM SYSIBM.DUAL

-- Derby
SELECT 1 FROM SYSIBM.SYSDUMMY1

-- Firebird
SELECT 1 FROM RDB$DATABASE

-- HANA, Sybase SQL Anywhere
SELECT 1 FROM SYS.DUMMY

-- HSQLDB
SELECT 1 FROM (VALUES(1)) AS dual(dual)

-- Informix
SELECT 1 FROM (SELECT 1 AS dual FROM systables WHERE (tabid = 1)) AS dual

-- Ingres, Teradata
SELECT 1 FROM (SELECT 1 AS "dual") AS "dual"

Solution 5 - Sql

I use this one:

select max(table_catalog) as x from information_schema.tables

to check connection and ability to run queries (with 1 row as result) for postgreSQL, MySQL and MSSQL.

Solution 6 - Sql

I use

Select COUNT(*) As X From INFORMATION_SCHEMA.SYSTEM_USERS Where 1=0

for hsqldb 1.8.0

Solution 7 - Sql

For tests using select count(*), it should be more efficient to use select count(1) because * can cause it to read all the column data.

Solution 8 - Sql

select 1 would work in sql server, not sure about the others.

Use standard ansi sql to create a table and then query from that table.

Solution 9 - Sql

Assuming the OP wants a Java answer:

As of JDBC3 / Java 6 there's the isValid() method which should be used rather than inventing one's own method.

The implementer of the driver is required to execute some sort of query against the database when this method id called. You - as a mere JDBC user - do not have to know or understand what this query is. All you have to do is to trust that the creator of the JDBC driver has done his/her work properly.

Solution 10 - Sql

How about

SELECT user()

I use this before.MySQL, H2 is OK, I don't know others.

Solution 11 - Sql

Just found out the hard way that it is

SELECT 1 FROM DUAL

for MaxDB as well.

Solution 12 - Sql

For Oracle the high performing query will be

select 'X' from <your_small_table> where <primay_key_coulmn> = <some_value>

This is from a performance perspective.

Solution 13 - Sql

I use this for Firebird

select 1 from RDB$RELATION_FIELDS rows 1

Solution 14 - Sql

For MSSQL.

This helped me determine if linked servers were alive. Using an Open Query connection and a TRY CATCH to put the results of the error to something useful.

IF OBJECT_ID('TEMPDB..#TEST_CONNECTION') IS NOT NULL DROP TABLE #TEST_CONNECTION
IF OBJECT_ID('TEMPDB..#RESULTSERROR') IS NOT NULL DROP TABLE #RESULTSERROR
IF OBJECT_ID('TEMPDB..#RESULTSGOOD') IS NOT NULL DROP TABLE #RESULTSGOOD

DECLARE @LINKEDSERVER AS VARCHAR(25)	SET @LINKEDSERVER = 'SERVER NAME GOES HERE'
DECLARE @SQL AS VARCHAR(MAX)
DECLARE @OPENQUERY AS VARCHAR(MAX)

--IF OBJECT_ID ('dbo.usp_GetErrorInfo', 'P' ) IS NOT NULL DROP PROCEDURE usp_GetErrorInfo;  
--GO  

---- Create procedure to retrieve error information.  
--CREATE PROCEDURE dbo.usp_GetErrorInfo  
--AS  
--SELECT 	 
--    ERROR_NUMBER() AS ErrorNumber  
--    ,ERROR_SEVERITY() AS ErrorSeverity  
--    ,ERROR_STATE() AS ErrorState  
--    ,ERROR_PROCEDURE() AS ErrorProcedure  
--    ,ERROR_LINE() AS ErrorLine  
--    ,ERROR_MESSAGE() AS Message;  
--GO  


BEGIN TRY
SET @SQL='
SELECT 1 
'''
--SELECT @SQL
SET @OPENQUERY = 'SELECT * INTO ##TEST_CONNECTION FROM OPENQUERY(['+ @LINKEDSERVER +'],''' + @SQL + ')'
--SELECT @OPENQUERY
EXEC(@OPENQUERY)
SELECT * INTO #TEST_CONNECTION FROM ##TEST_CONNECTION
DROP TABLE ##TEST_CONNECTION
--SELECT * FROM #TEST_CONNECTION
END TRY

BEGIN CATCH
-- Execute error retrieval routine.
IF OBJECT_ID('dbo.usp_GetErrorInfo') IS NOT NULL -- IT WILL ALWAYS HAVE SOMTHING... 
	BEGIN
		CREATE TABLE #RESULTSERROR (
		[ErrorNumber]		INT
		,[ErrorSeverity]	INT
		,[ErrorState]		INT
		,[ErrorProcedure]	INT
		,[ErrorLine]		INT
		,[Message]			NVARCHAR(MAX) 
		)
		INSERT INTO #RESULTSERROR
		EXECUTE dbo.usp_GetErrorInfo
	END
END CATCH

BEGIN 
	IF (Select ERRORNUMBER FROM #RESULTSERROR WHERE ERRORNUMBER = '1038') IS NOT NULL --'1038' FOR ME SHOWED A CONNECTION ATLEAST. 
		SELECT
		'0'	AS [ErrorNumber]		
		,'0'AS [ErrorSeverity]	
		,'0'AS [ErrorState]		
		,'0'AS [ErrorProcedure]	
		,'0'AS [ErrorLine]		
		, CONCAT('CONNECTION IS UP ON ', @LINKEDSERVER) AS [Message]			
	ELSE 
		SELECT * FROM #RESULTSERROR
END

docs.microsoft.com

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob HruskaView Question on Stackoverflow
Solution 1 - SqlRob HruskaView Answer on Stackoverflow
Solution 2 - SqlTim BütheView Answer on Stackoverflow
Solution 3 - SqlMark ByersView Answer on Stackoverflow
Solution 4 - SqlLukas EderView Answer on Stackoverflow
Solution 5 - SqlWojciechkView Answer on Stackoverflow
Solution 6 - SqlthinkbaseView Answer on Stackoverflow
Solution 7 - SqlNathan NiesenView Answer on Stackoverflow
Solution 8 - SqlNimChimpskyView Answer on Stackoverflow
Solution 9 - SqlpeterhView Answer on Stackoverflow
Solution 10 - SqlwenerView Answer on Stackoverflow
Solution 11 - SqlLars DeckerView Answer on Stackoverflow
Solution 12 - SqlJoby KurianView Answer on Stackoverflow
Solution 13 - SqlclaudsanView Answer on Stackoverflow
Solution 14 - SqlDeFlankoView Answer on Stackoverflow