2

I've a plain simple aws lambda written in java(No framework involved). Currently, I've used a Singleton instance for DBConnection as I wanted to re-use the earlier DBconnection object as long as lambda is warm. Also, the best practice suggests writing code outside the handler for efficient reuse and reducing runtime. I wanted to use the same DB connection again. But I've observed that the connection is being closed every invocation of lambda so it defies the Singleton purpose. To address that, I'm not closing the created connection. (By Default it was getting closed due to Autocloseable behavior I got rid of that now.) The Singleton class for connection is below:

 public enum DatabaseConnection {
  
    INSTANCE;
   static Connection CONNECTION = null;
   static String url = "jdbc:URL";
   static String user = "user";
   static String pass = "pwd";
   static 
   {
        try {
            CONNECTION = DriverManager.getConnection(url, user, pass);
        }
        catch (SQLException e) {
        }
    }
    public Connection getConnection(LambdaLogger logger) throws SQLException
    {
        
        if(CONNECTION == null || CONNECTION.isClosed()) {
            CONNECTION = DriverManager.getConnection(url, user, pass);
        }
        
        return CONNECTION;
    }
}

My question is:

  1. Is there any repercussions on the server-side if I don't close the connection in the client?
  2. Should I need not worry as lambda invocation runs in a separate JVM itself and if the JVM shuts down, anyway the connection object is GCed?

The idea is to reuse the DB connection object in AWS lambda without any framework.

lifeline2
  • 69
  • 1
  • 15
  • Yes, always close connections. Read https://stackoverflow.com/a/70317350/4800344 & https://stackoverflow.com/a/71286667/4800344 – Ermiya Eskandary May 13 '22 at 13:40
  • 1
    @ErmiyaEskandary I've not used RDS proxy the DB is on-prem. Although I completely understand the traditional idea about closing every connection but this time, if I close there is no way to reuse the same connection in next lambda invocation even the lambda is warm! – lifeline2 May 14 '22 at 01:37

1 Answers1

3
  1. Is there any repercussions on the server-side if I don't close the connection in the client?

There should not be any repercussions. AWS Lambda functions create containers and resources to execute your code. When your Lambda function finishes executing, background processes that are still running will be reused the next time the lambda is called. However, it is not guaranteed that AWS will reuse the same container. So initialization could actually happen.

You can read about The Freeze/Thaw Cycle in Lambda functions here.

  1. Should I need not worry as lambda invocation runs in a separate JVM itself and if the JVM shuts down, anyway the connection object is GCed?

If the JVM shuts down, Garbage Collection will close any open stream. If for some reason the JVM does not close the connection, the OS will take care of it. This does not ensure that it will be appropriately dealt with, but at this point, it's up to the OS how processes and resources are managed.

chris
  • 2,490
  • 4
  • 32
  • 56
  • 2
    thanks for the great explanation indeed! clarification about point 1 and at the server-side. Can we conclude that, as eventually the connection would be closed or reused so there would be no stale or un-used connections available at DB server? I've tested with and without closing the DB Connection and reuse of same connection ONLY happens if I don't close. And no idea how DB servers generally deal with stale connections too. – lifeline2 May 15 '22 at 01:40
  • Do you mean idle connections? Idle connections just means that a connection (that was already established) is waiting for the client run a query or transaction. I guess connections will be idle in between queries that you send from the client, until the connection is closed. Then a new will be created. Did I answer your question? – chris May 15 '22 at 21:23