VPC内にLambdaを置いて、VPC内にあるRDSに接続するLambda関数を実行しているのですが、
下記のようなプログラムの場合、都度DBへ接続/切断を行うプログラムの方がいいのか、
プログラムの最初にDBへ接続を行い、最後にDBとの接続を切るプログラムの方がいいのか、ご教示頂けますでしょうか?
(下記は最初にDBへ接続を行って、最後にDBとの接続を切るという形になっております。)
nodejs
1const AWS = require('aws-sdk'); 2const mysql = require('mysql2'); 3 4const pool = mysql.createPool({ 5 host : 'xxxx', 6 user : 'xxxx', 7 password : 'xxxx', 8 database: 'xxxx', 9 port: 'xxx', 10}); 11 12// 接続 13pool.query = util.promisify(pool.query); 14 15exports.lambdaHandler = async (event, context) => { 16 const check_res = await check(); 17 const check_a_res = await check_a(); 18 const check_b_res = await check_b(); 19 const check_c_res = await check_c(); 20 21 //切断 22 pool.end(); 23 return check_c_res; 24}; 25 26function check(){ 27 const data = ["a", "b"]; 28 29 try { 30 const results = await pool.query('select id from table where id = ? and name = ?' ,data); 31 return results; 32 }catch(e){ 33 console.log("check error"); 34 pool.end(); 35 throw e; 36 } 37} 38 39//function check_a()/check_b()/check_c()もDB接続し、それぞれinsert/update/deleteを含む関数となっております。
#環境
AWS Lambda関数
node.js 8.10
#関連質問(未解決)
https://teratail.com/questions/199473

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。