Is it alright to inlcude connect() inside the lambda_handler in order to close the connection after use?

Viewed 32

I wrote one lambda function to access the MySQL database and fetch the data i.e to fetch the number of users, but any real-time update is not fetched, unless the connection is re-established. And closing the connection inside the lambda_handler before returning, results in connection error upon its next call.

The query which I am using is -> select count(*) from users

import os
import pymysql
import json
import logging

endpoint = os.environ.get('DBMS_endpoint')
username = os.environ.get('DBMS_username')
password = os.environ.get('DBMS_password')
database_name = os.environ.get('DBMS_name')
DBport = int(os.environ.get('DBMS_port'))

logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
    connection = pymysql.connect(endpoint, user=username, passwd=password, db=database_name, port=DBport)
    logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")

def lambda_handler(event, context):
    try:
        cursor = connection.cursor()
        ............some.work..........
        ............work.saved..........
        cursor.close()
        connection.close()
        return .....

    except:
        print("ERROR")

The above code results in connection error after its second time usage, First time it works fine and gives the output but the second time when I run the lambda function it results in connection error.

Upon removal of this line ->

connection.close()

The code works fine but the real-time data which was inserted into the DB is not fetched by the lambda, but when I don't use the lambda function for 2 minutes, then after using it again, the new value is fetched by it.

So, In order to rectify this problem, I placed the connect() inside the lambda_handler and the problem is solved and it also fetches the real-time data upon insertion.

import os
import pymysql
import json
import logging

endpoint = os.environ.get('DBMS_endpoint')
username = os.environ.get('DBMS_username')
password = os.environ.get('DBMS_password')
database_name = os.environ.get('DBMS_name')
DBport = int(os.environ.get('DBMS_port'))

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    try:
        try:
            connection = pymysql.connect(endpoint, user=username, passwd=password, db=database_name, port=DBport)
        except:
            logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
        cursor = connection.cursor()
        ............some.work..........
        ............work.saved..........
        cursor.close()
        connection.close()
        return .....

    except:
        print("ERROR")

So, I want to know, whether is it right to do this, or there is some other way to solve this problem, I trying to solve this for few-days and finally this solution is working, but not sure whether will it be a good practice to do this or not.
Any problems will occur if the number of connections to database increases? Or any kind of resource problem?

0 Answers
Related