如果你关闭一个关闭的连接会发生什么?

What happens if you close a closed connection?

我正在阅读我的数据库访问逻辑,发现同一个连接可能会连续关闭两次。

除了重复指令外,还有什么理由让我担心在连接已经关闭后关闭连接?

如果您使用的是 java.sql.Connection,应该没有问题。

来自Connection documentation

Calling the method close on a Connection object that is already closed is a no-op.

即它什么都不做。

这应该代表任何正确的实施。尽管可以想象某些实现在这个问题上有奇怪的行为。

来自文档:

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released. Calling the method close on a Connection object that is already closed is a no-op. It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined.

所以简而言之,它应该什么都不做。请注意,此 Connection 接口的草率实现可能无法满足此接口契约中定义的规则。您没有说明您使用的是哪个数据库,因此我无法提供有关实施细节的更多信息。