2

I have a simple database connection method that I want to add a condition that if the connection fails then an email is sent to a list of people telling that it failed to connect.

My current method is this:

public Connection createInitialConnection() throws ClassNotFoundException, SQLException, PollingException
{
    DBConnectionDetails conDetails = new DBConnectionDetails("INITIAL");

    Class.forName(conDetails.getDriver());      
    Connection connInitial = DriverManager.getConnection(conDetails.getUrl(),
                                      conDetails.getUser(), conDetails.getPassword());
    logger.info("Initial connection created" + conDetails.getUrl());

    return connInitial;
}

Currently, there is no checking to see if the connection was successful, if it does not connect then the program just keeps going.

I'm not sure about the best way to do this would be. An if/else or try/catch?

Joel
  • 4,732
  • 9
  • 39
  • 54
Safi
  • 131
  • 1
  • 2
  • 8
  • 1
    IllegalStateException or null and where you use createInitialConnection check if != null – Marco Acierno May 29 '14 at 14:15
  • I think you should catch SQLException , and then try to send mail. – Kasper Ziemianek May 29 '14 at 14:16
  • What database technology are you using there? You should try to identify what type of exception is thrown when the connection fails. And then yes, use try/catch statements. – E_net4 May 29 '14 at 14:17
  • 1
    It is off topic, but i'd like to ask. Are you sure about this system of sending email for each failed attempts? If this is a webpage etc, then if connection fails (due to a database shutdown etc) then your system might start to send so many emails per minute, than it will basically turns into a nice a spammer machine ;) – T.G May 29 '14 at 14:32
  • @T.G. being able to throttle the emails would be good. also using a connection pool could reduce the amount of connection retrying. – Nathan Hughes May 30 '14 at 17:41
  • Please remember to identify your accepted solution, @Safi. – Ian Stevens Jun 10 '14 at 14:18

6 Answers6

2

Logging libraries like log4j allow you to add a logging appender that sends emails for each log entry (of course you can filter by severity, category, etc.). So when you attempt to get a connection and an exception is thrown it should get caught by the exception handler and logged, and the logger will send emails.

That assumes your application has some kind of exception handler that logs uncaught exceptions. Where that happens depends on your application's architecture. It could be in a servlet, in try-catch code in the jsp, or in a dedicated handler in a webapp framework like struts or spring-mvc.

If you implement this you will have a way to be notified when any exceptions are getting logged, it won't be limited to database connectivity problems.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I recommend this approach vs sending e-mails in a try-catch; that way, all your "doom" e-mails are launched from the logging appender, and you can disable it or substitute it for another mechanism for testing purposes or reusing the code in another project. – tucuxi May 29 '14 at 14:34
0

You can send email on catch block.

try{
    Class.forName(conDetails.getDriver());      
    Connection connInitial = DriverManager.getConnection(conDetails.getUrl(),
                      conDetails.getUser(), conDetails.getPassword());

 }
 catch(SQLException | ClassNotFoundException ex){ // Java 7 needed for multicatch
   // Send the Email.
 }
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Maybe you should try to catch SQLExcepton and then, send notification like this :

  try {
    Connection connInitial = DriverManager.getConnection(conDetails.getUrl(), conDetails.getUser(), conDetails.getPassword());
  } catch (SQLException e) {
    //send notification here
  }
Kasper Ziemianek
  • 1,329
  • 8
  • 15
0

catch block. It is always better than if-else

try{

 // your Logic to test the connection

}
catch(Exception ex)
{
 //Code to send the mail if the connection is failed.
}
akash
  • 22,664
  • 11
  • 59
  • 87
0

You can do with try { } Catch .. This in case you have an execption like no network or somthing. after that you need to check if Connection != null.. This is all you need

Aviad
  • 1,539
  • 1
  • 9
  • 24
0

Sending an email in the catch block might be OK for your application, but then you're faced with how to configure the email. Who should receive it? Are multiple recipients possible? How should the senders, subject and format be configured and passed to your mailer? What if the mailer fails? You may find yourself reinventing an existing solution if you go that route.

If you're using Log4j, it might be a good idea to configure a SMTPAppender for your existing logger or create a logger for errors which need to be sent over email. You can configure Log4j to use the appender for only SQL exceptions if you like (ie. log4j: Log output of a specific class to a specific appender).

Community
  • 1
  • 1
Ian Stevens
  • 794
  • 4
  • 19