当使用Python的subprocess.Popen()退出启动java程序时,为什么子进程打开的数据库连接没有关闭?

发布于 2024-09-14 13:35:49 字数 504 浏览 2 评论 0原文

我们使用 Robot Framework 进行测试自动化,并且我们的 jython 测试代码使用 subprocess.Popen() 生成一个 java 子进程:

    cmd = "java -jar program.jar"
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    process.wait()

Java 代码利用与 Oracle 数据库的 JDBC 连接,并且需要连续执行多次相同的程序。

我们的问题是,java 程序退出后,与 Oracle 的数据库连接并未关闭 - 并且在多次执行后,测试开始失败,因为 Oracle 不会接受更多连接。

netstat 显示,与 Oracle 的过时 TCP 连接归 jython 的 PID(= 父进程)所有。

为什么当java程序(=子进程)退出时连接没有关闭?

We use Robot Framework for test automation, and our jython test code spawns a java subprocess using subprocess.Popen():

    cmd = "java -jar program.jar"
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    process.wait()

Java code utilizes a JDBC connection to Oracle database, and the same program needs to be executed several times successively.

Our issue is, that after the java program exits the database connection to Oracle is not closed - and after several executions the tests start to fail because Oracle won't accept more connections.

netstat shows, that the stale TCP connections to Oracle are owned by jython's PID (=parent process).

Why aren't the connections closed when the java program (=subprocess) exits?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-09-21 13:35:49

我不确定,但有可能因为您使用的是 Jython,解释器就被赋予了连接的所有权(因此它们会一直存在,直到该进程终止)。您是否尝试过在 process.wait() 之后使用 process.terminate()

I'm not sure but it's possible that because you're using Jython, the interpreter is given ownership of the connections (and hence they survive until that process dies). Have you tried using process.terminate() after the process.wait()?

≈。彩虹 2024-09-21 13:35:49

考虑使用 os.kill (>= 2.6 中由 process.terminate 使用)。

如果这不起作用,考虑到您不寻常的设置 - JVM 调用 JVM,通常不需要 - 您可能需要使用类似 execnet 的东西(http://codespeak.net/execnet/) 启动一个 CPython 进程来控制它。与我们在 Jython 中提供的功能相比,CPython 具有更多用于访问主机操作系统服务的功能。使用 execnet 是将这些优势结合在一起的好方法。

Consider using os.kill (which is used by process.terminate in >= 2.6).

If that doesn't work, given your unusual setup - JVMs invoking JVMs, not usually necessary - you may want to use something like execnet (http://codespeak.net/execnet/) to start a CPython process to control this. CPython has more functionality for accessing host operating system services than what we have provided in Jython. Using execnet is a good way to combine these strengths together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文