How we can use JDBC connection pooling with AWS Lambda?

aws lambda rds connection pool java
sequelize connection pool lambda
aws lambda share database connection
sqlalchemy aws lambda
dynamodb connection pool
pgbouncer aws lambda
aws lambda singleton
redshift connection pooling

Can we use JDBC connection pooling with AWS Lambda ? AS AWS lambda function get called on a specific event, so its life time persist even after it finishing one of its call ?

No. Technically, you could create a connection pool outside of the handler function but since you can only make use of any one single connection per invocation so all you would be doing is tying up database connections and allocating a pool of which you could only ever use 1.

After uploading your Lambda function to AWS, the first time it is invoked AWS will create a container and run the setup code (the code outside of your handler function that creates the pool- let's say N connections) before invoking the handler code.

When the next request arrives, AWS may re-use the container again (or may not. It usually does, but that's down to AWS and not under your control).

Assuming it reuses the container, your handler function will be invoked (the setup code will not be run again) and your function would use one of N the connections to your database from the pool (held at the container level). This is most likely the first connection from the pool, number 1 as it is guaranteed to not be in use, since it's impossible for two functions to run at the same time within the same container. Read on for an explanation.

If AWS does not reuse the container, it will create a new container and your code will allocate another pool of N connections. Depending on the turnover of containers, you may exhaust the database pool entirely.

If two requests arrive concurrently, AWS cannot invoke the same handler at the same time. If this were possible, you'd have a shared state problem with the variables defined at the container scope level. Instead, AWS will use two separate containers and these will both allocate a pool of N connections each, i.e. 2N connections to your database.

It's never necessary for a single invocation function to require more than one connection (unless of course you need to communicate to two independent databases within the same context).

The only time a connection pool would be useful is if it were at one level above the container scope, that is, handed down by the AWS environment itself to the container. This is not possible.

The best case you can hope for is to have a single connection per container. Even then you would have to manage this single connection to ensure the database server hasn't disconnect or rebooted. If it does, your container's connection will die and your handler will never be able to connect again (until the container dies), unless you write some code in your function to check for dropped connections. On a busy server, the container might take a long time to die.

Also keep in mind that if your handler function fails, for example half way through a transaction or having locked a table, the next request invocation will get the dirty connection state from the container. The first invocation may have opened a transaction and died. The second invocation may commit and include all the previous queries up to the failure.

I recommend not managing state outside of the handler function at all, unless you have a specific need to optimise. If you do, then use a single connection, not a pool.

How we can use JDBC connection pooling, Expected behavior Actual behavior Steps to reproduce Full log output. awslabs / aws-serverless-java-container JDBC Connection Pools - Recommendation for Lambda runtime #238 We use C3P0 for connection pooling (Java) but when we see the UnknownHostException issue in Lambdas, it's  MongoDB Connection Pools in AWS Lambda. In this post, we’ll show you examples involving both Node.js and Java driver for MongoDB. For this tutorial, we use MongoDB hosted on ScaleGrid using AWS EC2 instances. It takes less than 5 minutes to set up, and you can create a free 30-day trial here to get started.

Yes, the lambda is mostly persistent, so JDBC connection pooling should work. The first time a lambda function is invoked, the environment will be created and it may or may not get reused. But in practice, subsequent invocations will often reuse the same lambda process along with all program state if your triggering events occur often.

This short lambda function demonstrates this:

package test;


public class TestLambda implements RequestHandler<String, String> {

    private int invocations = 0;

    public String handleRequest(String request, Context context) {
        System.out.println("invocations = " + invocations);
        return request;

Invoke this from the AWS console with any string as the test event. In the CloudWatch logs, you'll see the invocations number increment each time.

JDBC Connection Pools, AWS Lambda integration with MySQL Relational Database. aws-lambda The connection to the database is obtained using the DataSource interface. AWS Lambda For this reason the HikariCP JDBC connection pool is used. View all of  You can use any of the other Progress DataDirect JDBC drivers to connect to Oracle Eloqua, Oracle Sales Cloud, Oracle Service Cloud, SQL Server, DB2, Mongo DB etc., from AWS Lambda. If you are using Node.js, you can use the jdbc module to connect to these data sources via DataDirect JDBC driver .

It has caveat There is no destroy method which ensures closing pool. One may say DB connection idle time would handle. What if same DB being used for other use cases like pool maintain in regular machine Luke EC2. As many say, if there is sudden spike in requests, create chaos to DB as there will be always some maximum connection setting at database side per user.

sixhours-team/aws-lambda-jdbc-java: AWS Lambda , I work with AWS Lambda quite a bit. The ability to use this Functions-as-a-Service (FaaS) has dramatically reduced the complexity and hardware  Update 9/2/2018: I wrote an NPM module that manages MySQL connections for you in serverless environments. Check it out here.. I work with AWS Lambda quite a bit. The ability to use this Functions-as-a-Service (FaaS) has dramatically reduced the complexity and hardware needs of the apps I work on.

How To: Reuse Database Connections in AWS Lambda, In our example, we use the AWS API gateway to trigger the Lambda functions. What is a Connection Pool? Opening and closing a database  Connection pooling using AWS EC2 is easier to manage because a single instance can track the state of its connection pool without any issue. Thus, using AWS EC2 significantly reduces the risk of

How to Use MongoDB Connection Pooling on AWS Lambda, Connection Pooling Is key for speedy database calls; here's how to do a lot of smart people using a lot of strong logic to explain why it can't be done. “​Lambda functions are stateless and asynchronous which is great, But if you look at the second image taken from AWS, the connection to the database  1. The most reliable way of handling database connections in AWS Lambda is to connect and disconnect from the database within the invocation itself which is what your code is already doing. There are known ways to reuse an existing connection but success rates for that vary widely depending on database server configuration (idle connections, etc.) and production load.

Best practices for Serverless: Connection Pooling your database, In our example, we use the AWS API gateway to trigger the Lambda functions. What Is a Connection Pool? Opening and closing a database  AWS Lambda JDBC Java. This is a sample project showing the AWS Lambda integration with MySQL relational database. When triggered the lambda function will access the employee table ( schema) in the MySQL RDS instance that you created in the VPC, and return the list of all employees found in the table. The connection to the database is obtained using the DataSource interface.

  • So is the best approach to use close connections after they are used? And reopen when the next invocation is made? That seems the simplest approach.
  • @rharriso That would be my approach. You could leave a single connection open so long as your Lambda function doesn't bail out half way through and leave the connection dirty, for example with a transactional query.
  • It's probably still a good idea to use a connection pool of size 1, just so you don't have to manage the logic to test and replace connections.
  • Thanks for the answer, Its Okay to use BoneCP connection pooling […, the connection pool will persist in frequent invokes of lambda function, if yes then how it can be verified ?
  • I haven't tried BoneCP, but I don't see why not. To verify, configure log4j to log BoneCP at the debug level and examine the logs.
  • Thanks, Let me do it, and then update the status here ! perfect :-)
  • Instance variables usually persist across invocations. I've updated the answer with a short lambda function that demonstrates this.
  • Unfortunately, this answer is partially incorrect. Pooling wont work. Two concurrent invocation would cause two containers to be used. See my answer below.
  • canwe not use shutdown hook?