Aws lambda 如何使用无服务器框架管理Lambda内部Aurora无服务器数据api的类型连接
我正在使用:Aws lambda 如何使用无服务器框架管理Lambda内部Aurora无服务器数据api的类型连接,aws-lambda,serverless-framework,typeorm,aws-aurora-serverless,Aws Lambda,Serverless Framework,Typeorm,Aws Aurora Serverless,我正在使用: Aurora无服务器数据API(Postgres) 打字 AWS Lambda与无服务器框架(TypeScript,网页包) 我正在连接数据库,就像github中描述的那样 const connection=wait createConnection({ 类型:“极光数据api pg”, 数据库:“测试数据库”, 秘书长:“arn:aws:secretsmanager:eu-west-1:537011205135:机密:xxxxxx/xxxxxx/xxxxxx”, resour
- Aurora无服务器数据API(Postgres)
- 打字
- AWS Lambda与无服务器框架(TypeScript,网页包)
const connection=wait createConnection({
类型:“极光数据api pg”,
数据库:“测试数据库”,
秘书长:“arn:aws:secretsmanager:eu-west-1:537011205135:机密:xxxxxx/xxxxxx/xxxxxx”,
resourceArn:'arn:aws:rds:eu-west-1:xxxxx:xxxxxx:xxxxxx',
地区:“欧盟-西部-1”
})
这就是我在Lambda函数中使用它的方式
export const testConfiguration: APIGatewayProxyHandler = async (event, _context) => {
let response;
try {
const connectionOptions: ConnectionOptions = await getConnectionOptions();
const connection = await createConnection({
...connectionOptions,
entities,
});
const userRepository = connection.getRepository(User);
const users = await userRepository.find();
response = {
statusCode: 200,
body: JSON.stringify({ users }),
};
} catch (e) {
response = {
statusCode: 500,
body: JSON.stringify({ error: 'server side error' }),
};
}
return response;
};
当我第一次执行时,它工作得很好
但是第二次和下一次我会出错
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
那么,管理这种连接的正确方法是什么?
是否应该以某种方式重复使用
我已经找到了一些简单RDS的解决方案,但是Aurora Serverless数据API的全部要点是,您不必管理连接。当您尝试建立连接时,您需要检查是否已经有它可以使用的连接。这是我的
数据库
类,用于处理连接
export default class Database {
private connectionManager: ConnectionManager;
constructor() {
this.connectionManager = getConnectionManager();
}
async getConnection(): Promise<Connection> {
const CONNECTION_NAME = 'default';
let connection: Connection;
if (this.connectionManager.has(CONNECTION_NAME)) {
logMessage(`Database.getConnection()-using existing connection::: ${CONNECTION_NAME}`);
connection = await this.connectionManager.get(CONNECTION_NAME);
if (!connection.isConnected) {
connection = await connection.connect();
}
} else {
logMessage('Database.getConnection()-creating connection ...');
logMessage(`DB host::: ${process.env.DB_HOST}`);
const connectionOptions: ConnectionOptions = {
name: CONNECTION_NAME,
type: 'postgres',
port: 5432,
logger: 'advanced-console',
logging: ['error'],
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
namingStrategy: new SnakeNamingStrategy(),
entities: Object.keys(entities).map((module) => entities[module]),
};
connection = await createConnection(connectionOptions);
}
return connection;
}
}
导出默认类数据库{
私有连接管理器:连接管理器;
构造函数(){
this.connectionManager=getConnectionManager();
}
异步getConnection():承诺{
const CONNECTION_NAME='default';
让连接:连接;
if(this.connectionManager.has(CONNECTION_NAME)){
logMessage(`Database.getConnection()-使用现有连接::${connection\u NAME}`);
connection=wait this.connectionManager.get(连接名称);
如果(!connection.isConnected){
连接=等待连接。连接();
}
}否则{
logMessage('Database.getConnection()-正在创建连接…');
logMessage(`DB host:::${process.env.DB_host}`);
常量连接选项:连接选项={
名称:连接名称,
类型:“postgres”,
港口:5432,
记录器:“高级控制台”,
日志记录:['error'],
主机:process.env.DB_主机,
用户名:process.env.DB_用户名,
数据库:process.env.DB_数据库,
密码:process.env.DB_password,
namingStrategy:new SnakeNamingStrategy(),
实体:Object.keys(entities).map((module)=>entities[module]),
};
连接=等待createConnection(connectionOptions);
}
回路连接;
}
}
您确定问题与Aurora有关而与Lambda无关吗?在我看来,冷启动后连接保持打开状态,并且由于您不存储连接,因此每次连续调用createConnection
都会尝试创建一个具有相同名称的新TypeORM连接,因此失败。