Search code examples
javaoracle-databasesqlexceptionerror-code

Java Detecting Oracle Database Down Error Codes


I want to detect when a Oracle database is down. These below error codes are taken from http://docs.oracle.com/cd/E23943_01/apirefs.1111/e13952/pagehelp/JDBCjdbcdatasourcesjdbcdatasourceconfigconnectionpooltitle.html:

■ 3113: "end-of-file on communication channel"

■ 3114: "not connected to ORACLE"

■ 1033: "ORACLE initialization or shutdown in progress"

■ 1034: "ORACLE not available"

■ 1089: "immediate shutdown in progress - no operations are permitted"

■ 1090: "shutdown in progress - connection is not permitted"

■ 17002: "I/O exception"

I would like to detect without using DataSource resource injection without a Glassfish server when the Oracle database is down. Below is the sample code:

public class Database {
    public static void main(String[] args) {
         try {
             Connection connection = DriverManager.getConnection("url", "username", "password");
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(new String("SELECT name FROM NAMES");
         }
         catch(SQLException e) {
             // use the error code defined above to detect database is down
         }
    }
 }

As this is a standlone application, can these error codes be used and will they work or are there some other error codes that work for standlone applications?


Solution

  • Those codes are related to (and returned by) the JDBC driver and should work regardless of where the JDBC driver is deployed (whether it's a standalone app or an app server).