我在用着:
- Aurora 无服务器数据 API (Postgres)
- 带有typeorm-aurora-data-api-driver 的 TypeORM
- AWS Lambda 与无服务器框架(TypeScript、WebPack)
我正在连接到数据库,就像它在 github 中描述的那样,
const connection = await createConnection({
type: 'aurora-data-api-pg',
database: 'test-db',
secretArn: 'arn:aws:secretsmanager:eu-west-1:537011205135:secret:xxxxxx/xxxxxx/xxxxxx',
resourceArn: 'arn:aws:rds:eu-west-1:xxxxx:xxxxxx:xxxxxx',
region: 'eu-west-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 Data API的全部意义在于您不必管理连接