Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:46:46+00:00 2026-06-05T07:46:46+00:00

Objective: ensure all code paths either throw or return a valid connection; I am

  • 0

Objective: ensure all code paths either throw or return a valid connection; I am trying to explicitly avoid at all costs the connection object to ever return as “null”.

This is the best I could come up with:

public class JdbcConnectionManager {
  public static class JdbcConnectionFailureException extends Exception {
    private static final String JDBC_CONNECTION_INVALID_MESSAGE =
        "JDBC connection invalid, checked with timeout value of: "
            + JdbcConnectionManager.JDBC_CONNECTION_VALIDATION_TIMEOUT_IN_SECONDS;
    private static final long serialVersionUID = 1L;
    public JdbcConnectionFailureException(String message) {
      super(message);
    }
    public JdbcConnectionFailureException(Throwable throwable) {
      super(throwable);
    }
  }
  private static int JDBC_CONNECTION_VALIDATION_TIMEOUT_IN_SECONDS = 3;
  public static Connection getJdbcConnection(
      JdbcConnectionParameters jdbcConnectionParameters)
      throws JdbcConnectionFailureException {
    try {
      if (jdbcConnectionParameters
          .driverNeedsHelpRegisteringUsingClassForName()) {
        Class.forName(jdbcConnectionParameters
            .getClassForNameDriverRegistrationString());
      }
      Connection jdbcConnection =
          DriverManager.getConnection(jdbcConnectionParameters
              .getJbdcConnectionUrl());
      if (!jdbcConnection
          .isValid(JdbcConnectionManager.JDBC_CONNECTION_VALIDATION_TIMEOUT_IN_SECONDS)) {
        throw new JdbcConnectionManager.JdbcConnectionFailureException(
            JdbcConnectionManager.JdbcConnectionFailureException.JDBC_CONNECTION_INVALID_MESSAGE);
      }
      // TODO: perform further validation on the connection
      return jdbcConnection;
    } catch (ClassNotFoundException classNotFoundException) {
      throw new JdbcConnectionManager.JdbcConnectionFailureException(
          classNotFoundException);
    } catch (SQLException sqlException) {
      throw new JdbcConnectionManager.JdbcConnectionFailureException(
          sqlException);
    }
  }
}

Am I rock-solid here? Is there a better, more concise/elegant/robust way to ensure “connection” is never “null”?

EDIT:

I added a jdbcConnection.isValid() check but I have left out input validation. I’ll do that in the actual code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:46:47+00:00Added an answer on June 5, 2026 at 7:46 am

    It would be much clearer to just return the connection from inside the try block:

    public static Connection getJdbcConnection(
                  JdbcConnectionParameters jdbcConnectionParameters)
                                         throws JdbcConnectionFailureException {
        try {
            if (jdbcConnectionParameters.driverNeedsHelpRegisteringUsingClassForName()) {
                Class.forName(jdbcConnectionParameters
                                    .getClassForNameDriverRegistrationString());
            }
            return DriverManager.getConnection(jdbcConnectionParameters
                       .getJbdcConnectionUrl());
            // TODO: perform further validation on the connection
        } 
        catch (ClassNotFoundException classNotFoundException) {
            throw new JdbcConnectionManager.JdbcConnectionFailureException(
                classNotFoundException);
        } 
        catch (SQLException sqlException) {
            throw new JdbcConnectionManager.JdbcConnectionFailureException(
                sqlException);
        }
    }
    

    EDIT : to answer your question about dead code:

    jdbcConnection can NOT be null at this place, because if it was null, a NPE would have been thrown at the line

    if (!jdbcConnection.isValid(...))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I normally work in Objective-C, but am now trying to integrate some C code
Objective-C uses a sophisticated message-passing system when one object calls a method on another
I have a problem with some simple code. The photo's, either picked or taken
I am trying to ensure index on a field of a document and its
what's the best way to ensure all NSDate's in my project have no sub-second
I'm looking for some style advice for testing this piece of (Objective-)C++ code, I
OBJECTIVE I'm trying to accomplish an automatic redirect depending on the users language. I
Is there a way to ensure that the serialization of an object is done
I have an Objective C static library that I am trying to import into
Can someone summarise the key pieces of objective-c code that would assist in solving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.