0

I have a AWS Lambda function written using Java 8. I am establishing an oracle connection from my Lambda. Should I explicitly close the connection , statement & resultsets or would it get destroyed automatically when lambda terminates?

1
  • I am also having the same requirement to connect oracle DB connection in AWS lambda how can we achieve this? sorry I am very new to AWS. Commented May 15, 2020 at 12:10

1 Answer 1

2

Close your Statement and ResultSet explicitly always.

Closing connection is about your implementation. If you plan to reuse it, you can do it on your own, but keep in mind that...

AWS Lambda function is a container (with one JVM running in it). It stays alive for some time (default is 5 min idle as I remember).

It means if next request to AWS Lambda function comes before 5 min. that container is going to be used again.

If there are no more available containers to serve AWS Lambda function new container/JVM will spin up - with new DB connection.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Vadim. What is the potential impact if I don't close statement and resultset? Wouldn't this be destroyed when lambda shuts down?
Yes it will when container/jvm gone, but you have no control on it and you cannot predict when it will happen. it can stay alive for long time if requests keep coming. Still regardless of implementation (AWS Lamda or anything else) JDBC statement and resultset must be closed explicitly when they do not need anymore. And it is better to do in the same class and thread where they opened. Otherwise behavior can be very bad. I cannot explain all behind that now. I do not work with JDBC anymore for long enough time, but... it is a main rule I remember well from previous experience. :-)
Thanks @Vadim. This helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.