You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Python Lambda连接本地Oracle数据库报资源繁忙错误排查

问题:AWS Lambda连接Oracle数据库报"资源繁忙"错误,同VPC的Fargate可正常连接

我正在编写Python版AWS Lambda,用于连接本地Oracle数据库。该Lambda部署在可访问数据库的私有VPC及子网中,同一VPC和子网下的Fargate服务可成功连接数据库,但Lambda却报错"资源繁忙"。已开放Lambda安全组的所有出站规则,以下是我的Lambda代码及报错信息,请问可能的原因是什么?

Lambda代码

import json
import oracledb

def create_oracle_connection():
    print('Creating DB Connection')
    connection = oracledb.connect(
        user='testenv1',
        password='testenv1',
        dsn='nagTasj12-scan.ato.com:1521/?service_name=ATOODHAS2.ato.com',
    )
    print('Created DB Connection')
    return connection

def lambda_handler(event, context):
    event_uuid = "2"
    # Print the event ID
    print(f'Received event ID: {event_uuid}')
    event = {} 
    # Connect to Oracle database
    connection = create_oracle_connection()
    cursor = connection.cursor()

报错信息

{
  "errorMessage": "DPY-6005: cannot connect to database (CONNECTION_ID=sfdfsdf+fsdffsd==).\n[Errno 16] Device or resource busy",
  "errorType": "OperationalError",
  "requestId": "9ac84a89-2131-4275-b1c2-gdsgsg232sdf",
  "stackTrace": [
    "  File \"/var/task/event_processor.py\", line 20, in lambda_handler\n    connection = create_oracle_connection()\n",
    "  File \"/var/task/event_processor.py\", line 6, in create_oracle_connection\n    connection = oracledb.connect(\n",
    "  File \"/var/task/oracledb/connection.py\", line 1158, in connect\n    return conn_class(dsn=dsn, pool=pool, params=params, **kwargs)\n",
    "  File \"/var/task/oracledb/connection.py\", line 541, in __init__\n    impl.connect(params_impl)\n",
    "  File \"src/oracledb/impl/thin/connection.pyx\", line 381, in oracledb.thin_impl.ThinConnImpl.connect\n",
    "  File \"src/oracledb/impl/thin/connection.pyx\", line 377, in oracledb.thin_impl.ThinConnImpl.connect\n",
    "  File \"src/oracledb/impl/thin/connection.pyx\", line 337, in oracledb.thin_impl.ThinConnImpl._connect_with_params\n",
    "  File \"src/oracledb/impl/thin/connection.pyx\", line 318, in oracledb.thin_impl.ThinConnImpl._connect_with_description\n",
    "  File \"src/oracledb/impl/thin/connection.pyx\", line 288, in oracledb.thin_impl.ThinConnImpl._connect_with_address\n",
    "  File \"/var/task/oracledb/errors.py\", line 181, in _raise_err\n    raise error.exc_type(error) from cause\n"
  ]
}

可能的原因及解决方向

  • Lambda执行环境资源限制与连接模式冲突
    Lambda的临时执行环境与Fargate的长期容器不同,oracledb默认的thin模式在VPC场景下可能出现套接字资源竞争。尝试切换到thick模式,需在Lambda层中添加Oracle客户端库,利用本地客户端的连接管理能力规避资源问题。

  • 未正确释放数据库连接导致套接字耗尽
    Lambda每次执行后若未关闭cursor和connection,会导致大量TCP连接处于TIME_WAIT状态,占用可用套接字资源。Fargate的连接生命周期更稳定,不会频繁触发这类问题。修改代码添加资源释放逻辑:

    def lambda_handler(event, context):
        event_uuid = "2"
        print(f'Received event ID: {event_uuid}')
        connection = None
        cursor = None
        try:
            connection = create_oracle_connection()
            cursor = connection.cursor()
            # 执行数据库操作
        finally:
            if cursor:
                cursor.close()
            if connection:
                connection.close()
    
  • 子网IP或ENI资源不足
    Lambda在VPC中运行时,每个并发实例会占用一个ENI(弹性网络接口)和子网IP。若子网可用IP过少,或账户ENI配额不足,会导致无法创建新网络连接触发错误。检查子网可用IP数量及AWS账户的ENI配额。

  • Oracle数据库连接数超限
    Lambda并发执行可能瞬间创建大量连接,超出Oracle的processes或sessions参数限制。Fargate的连接数相对稳定,不会触发该阈值。检查Oracle数据库的连接数配置,调整参数或限制Lambda并发数。

  • DNS解析异常导致的伪装错误
    虽然报错显示"资源繁忙",但实际可能是Lambda执行环境的DNS解析超时或失败。尝试在DSN中直接使用数据库私有IP替代扫描域名,排除DNS影响:

    dsn='10.0.0.10:1521/?service_name=ATOODHAS2.ato.com'
    

内容的提问来源于stack exchange,提问作者Vivek Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 01:11:12