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

如何在JavaScript/AWS Lambda中正确使用/关闭Gremlin事务

使用Gremlin操作Neptune时的WebSocket与事务问题

我在JavaScript中使用Gremlin操作Neptune事务时,遇到了WebSocket连接一直处于打开状态的异常情况。已查阅AWS相关文档,但仍无法完全理解事务的运行机制及正确操作方式。根据文档说明,Lambda环境中似乎无需调用await dc.close(),但本地运行时出现了问题。

本地执行node testNeptune运行以下代码后,后续所有写入操作均失败:

const makeHangingTransaction = async function () {
    let dc = new DriverRemoteConnection(
        '<myurl>',
        { pingInterval: 5000, pingTimeout: 5000 }
    );
    let graph = new Graph();
    let g = graph.traversal().withRemote(dc);
    await dropAll(g);
    let tx = g.tx();
    let gtx = tx.begin();
    try {
        console.log('transaction test1');
        console.log(await gtx.addV('person').next());
        console.log(await gtx.V().toList());
        console.log();
        await tx.commit();
    } finally {
        tx.close();
    }
    try {
        console.log('transaction test1');
        console.log(await gtx.addV('person').next());
        console.log(await gtx.V().toList());
        console.log();
        await tx.commit();
    } finally {
        tx.close();
    }
};
makeHangingTransaction();

脚本运行后报错Error: Connection has been closed.,推测tx.close()执行后,父级dc连接失效但实际WebSocket连接并未终止,且我知道tx.close()需要使用await执行。

这导致了“挂起”事务,此后所有写入操作均无法执行——无论是AWS Jupyter还是本地环境,都会挂起并报错Failed to complete Insert operation for a VertexProperty due to conflicting concurrent operations. Please retry. 0 transactions are currently rolling back ,必须等待数分钟WebSocket超时后才能恢复写入。

现提出以下问题:

  • 若代码出错导致WebSocket连接处于“打开”状态,是否有办法强制重置或创建新连接?
  • 为何AWS Jupyter的写入会失败?它应使用与Neptune服务器的独立连接。
  • 本地环境是否应始终手动调用await dc.close()?
  • 是否需要使用await tx.close()?该操作对我无作用,脚本会一直等待,必须调用await dc.close()才能退出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:35:11