How we can use JDBC connection pooling with AWS Lambda?

后端 未结 4 1114
我寻月下人不归
我寻月下人不归 2020-12-30 06:39

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

4条回答
  •  一向
    一向 (楼主)
    2020-12-30 07:36

    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;
    
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.RequestHandler;
    
    public class TestLambda implements RequestHandler {
    
        private int invocations = 0;
    
        public String handleRequest(String request, Context context) {
            invocations++;
            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.

提交回复
热议问题