Try-catch-finally block in java

As per my understanding, I want to follow the best practice to free up resources at the end to prevent connection leakage. Here is my code in HelperClass.

public static DynamoDB getDynamoDBConnection()
{   
    try
    {
        dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
    }
    catch(AmazonServiceException ase)
    {
        //ase.printStackTrace();
        slf4jLogger.error(ase.getMessage());
        slf4jLogger.error(ase.getStackTrace());
        slf4jLogger.error(ase);
    }
    catch (Exception e)
    {
        slf4jLogger.error(e);
        slf4jLogger.error(e.getStackTrace());
        slf4jLogger.error(e.getMessage());
    }
    finally
    {
        dynamoDB.shutdown();
    }
    return dynamoDB;
}

      

I doubt that since the finally block will execute regardless of whether dynamoDB returns an empty connection because it will be closed in the finally block and then execute the return statement? TIA.

+3


source to share


4 answers


Your understanding is correct. dynamoBD.shutdown()

will always run before return dynamoDB

.

I'm not familiar with the framework you are working with, but I would probably order the code like this:

public static DynamoDB getDynamoDBConnection()
        throws ApplicationSpecificException {   
    try {
        return new DynamoDB(new AmazonDynamoDBClient(
                                    new ProfileCredentialsProvider()));
    } catch(AmazonServiceException ase) {
        slf4jLogger.error(ase.getMessage());
        slf4jLogger.error(ase.getStackTrace());
        slf4jLogger.error(ase);
        throw new ApplicationSpecificException("some good message", ase);
    }
}

      

and use it like



DynamoDB con = null;
try {
    con = getDynamoDBConnection();
    // Do whatever you need to do with con
} catch (ApplicationSpecificException e) {
    // deal with it gracefully
} finally {
    if (con != null)
        con.shutdown();
}

      

You can also create a wrapper AutoCloseable

for your dynamoDB connection (which calls shutdown

inside close

) and do

try (DynamoDB con = getDynamoDBConnection()) {
    // Do whatever you need to do with con
} catch (ApplicationSpecificException e) {
    // deal with it gracefully
}

      

+8


source


Yes, dynamoDB will return an empty connection as it dynamoBD.shutdow()

will execute before the return statement. Is always.



+1


source


While I am not answering your question that the last block is always executed (there are several answers to this question), I would like to share some information on how DynamoDB clients will be used.

The DynamoDB client is a thread-safe object and is designed to be shared by multiple threads - you can create a global one for your application and reuse the object when you need it. Typically, client creation is managed by some kind of IoC container (like Spring IoC container) and then exposed by the container to whatever code needs it through dependency injection.

Under the hood, the DynamoDB client maintains a pool of HTTP connections to push the DynamoDB endpoint and uses connections from that pool. Various pool settings can be configured by passing in an instance of the ClientConfiguration object when constructing the client. For example, one of the parameters is the maximum number of open HTTP connections allowed.

With that said, I would argue that since the DynamoDB client manages the lifecycle of HTTP connections, resource leaks shouldn't really be the cause of code using the DynamoDB client.

+1


source


How about we "mimic" the error and see what happens? This is what I mean:

___ Case 1 ___

try{
  // dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
  throw new AmazonServiceException("Whatever parameters required to instantiate this exception");
}    catch(AmazonServiceException ase)
    {
        //ase.printStackTrace();
        slf4jLogger.error(ase.getMessage());
        slf4jLogger.error(ase.getStackTrace());
        slf4jLogger.error(ase);

    }
    catch (Exception e)
    {
        slf4jLogger.error(e);
        slf4jLogger.error(e.getStackTrace());
        slf4jLogger.error(e.getMessage());
    }
    finally
    {
        //dynamoDB.shutdown();
        slf4jLogger.info("Database gracefully shutdowned");
    }

      

___ Case 2 ___

try{
  // dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
  throw new Exception("Whatever parameters required to instantiate this exception");
}    catch(AmazonServiceException ase)
    {
        //ase.printStackTrace();
        slf4jLogger.error(ase.getMessage());
        slf4jLogger.error(ase.getStackTrace());
        slf4jLogger.error(ase);

    }
    catch (Exception e)
    {
        slf4jLogger.error(e);
        slf4jLogger.error(e.getStackTrace());
        slf4jLogger.error(e.getMessage());
    }
    finally
    {
        //dynamoDB.shutdown();
        slf4jLogger.info("Database gracefully shutdowned");
    }

      

These exercises can be a perfect place to use unit tests and more specifically mock tests . I suggest you take a close look at JMockit to help you write such tests more easily.

0


source







All Articles