gpt4 book ai didi

java - Play Framework : closing JDBC connection

转载 作者:搜寻专家 更新时间:2023-10-30 21:50:46 25 4
gpt4 key购买 nike

是否需要在 Play! 中手动关闭数据库连接 (JDBC)?每次请求后框架(2.3.0)?

这里的例子只是做了一个 DB.getConnection() 并没有说任何关于关闭连接的事情:http://www.playframework.com/documentation/2.3.x/JavaDatabase

所以我假设为每个 GET/POST 请求执行 DB.getConnection() 是可以接受的,并且我不需要关闭任何东西。 sql语句呢?

conn = ds.getConnection();
stmt = conn.createStatement();
stmt.execute("SOME SQL QUERY");
stmt.close();

最佳答案

如果您仔细阅读文档,您会发现它们确实回答了您的第一个问题:

It is important to note that resulting Connections are not automatically disposed at the end of the request cycle. In other words, you are responsible for calling their close() method somewhere in your code so that they can be immediately returned to the pool.

关于 SQL 语句,我想您不必显式关闭它。在 Connection.close() 的 Javadoc 中他们说:

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

如果我理解正确,Connection.close() 也应该释放语句资源...

关于java - Play Framework : closing JDBC connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24184059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com