Question

I have a requirement where I need to check if the database connection is down, I then send an e-mail. Below is the code:

public static void main(String[] args) {
     try {
         Connection connection = DriverManager.getConnection("url", "username", "password");
         Statement statement = connection.createStatement();
         ResultSet resultSet = statement.executeQuery("SELECT * FROM Table");
    }
    catch(SQLException e) {
        if(e.getMessage().contains(new String("ORA-12543"))) {
           // send email
        }
    }
}

I used this website to determine when the Oracle database is down: http://www.dbmotive.com/ora-12543-tnsdestination-host-unreachable/

Now, I would like to setup the JDBC Resource and JDBC Connection pool in Glassfish and use the connection from there. I have never used Glassfish. I can't seem to find what I am looking for. Once, I have setup the resources and connection pool in Glassfish I would like to know if the database is down according to the above code will this code still work or there would be any changes. I would like to know when the database is down, then send an e-mail.

Please note the above code is sample, but in my main code this code exists.

Was it helpful?

Solution

IMHO, you will need to use the following codes once you are using connection retrieved through JNDI datasource. (found here:) I have also provided details on how to retrieve the JNDI datasource below.

■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"

=========================== Code to retrieve the connection from datasource

InitialContext ic = new InitialContext();
     com.sun.appserv.DataSource ds = (com.sun.appserv.DataSOurce) ic.lookup("jdbc/PointBase"); 
     Connection con = ds.getConnection();
     Connection drivercon = ds.getConnection(con);

Glassfish datasource javadoc is here

If you dont want to deploy the application, but use the datasource of a remote glassfish server, get the initialcontext as below

Properties props=new Properties();    
            props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");    
            props.put(Context.PROVIDER_URL,"iiop://URL_OF_APP_SERVER:PORT");    
            Context ctx=new InitialContext(props);    
            DataSource datasource = (DataSource)ctx.lookup("jndi/mydatasource");   

OTHER TIPS

With the information in the question, you would like to create a jdbc resource i.e. Datasource in the Glassfish instance.You can also create a connection pool for it. Please follow this link to learn on how to do it. You have to change your code a bit like looking up using JNDI

   DataSource ds = (DataSource) new InitialContext().lookup("jdbc/YOUR_DATASOURCE_NAME");
   Connection con = ds.getConnection();

instead of getting the connection using the DriverManager with the database configuration and credentials since all these information will be configured in the Glassfish server as a Datasource. JNDI based datasource is used to decouple the work of developers and deployers. There is no need for the developer to know about the configurations of the database server. Simply knowing the Datasource name will fulfill the needs of the development.Adding to that, it is simple enough to manage multiple applications to connect to the same database using a single datasource without having the datasource configurations in each application deployed individually. I'd suggest you to look on the oracle documentation (check the correct version) on error codes to be specific on handling cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top