正确的连接方式 [英] Proper way to close connection

查看:98
本文介绍了正确的连接方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用连接池.在连接关闭后,我们在项目中看到语句已关闭.我知道在使用连接池的情况下,关闭连接后,与数据库的物理连接不会关闭,而是返回到池中以供重用.所以我的问题是:

We are using connection pool in our project. We see in our project the statements are closed after the connection is closed. I know that in case of a connection pool, after a connection is closed the physical connection to the database is not closed but returns to the pool for reuse. So my question is:

如果在关闭连接后关闭语句,会发生什么?语句是否可以正确关闭/关闭连接会关闭所有语句并关闭语句是多余的/语句是打开的,尽管连接返回到池中,但由于打开的语句而无法重用? (我们同时使用Statement和PreparedStatement.)

What will happen if statements are closed after a connection is closed? Will the statements be closed properly/will closing the connection close all the statements and closing the statements are redundant/the statements are open and though the connection returns to the pool, it is not reusable because of open statements? (We are using both Statement and PreparedStatement).

推荐答案

如果在关闭连接后关闭语句,会发生什么?

What will happen if statements are closed after a connection is closed?

什么都没有.他们已经关门了.这是有据可查的.

Nothing. They were already closed. This is documented.

将正确关闭语句

Will the statements be closed properly

是的.这是有据可查的.

Yes. This is documented.

/将关闭连接会关闭所有语句

/will closing the connection close all the statements

是的.这是有据可查的.

Yes. This is documented.

和关闭语句是多余的

and closing the statements are redundant

是的

语句是打开的,尽管连接返回到池中,但由于打开了语句,它不可重用吗? (我们同时使用Statement和PreparedStatement.)

the statements are open and though the connection returns to the pool, it is not reusable because of open statements? (We are using both Statement and PreparedStatement).

我不能为此做头或尾.如果连接已关闭,则从其派生的StatementsResultSets也将关闭.这是有据可查的.如果Connection没有关闭,则StatementsResultSets也不会关闭,除非您将它们关闭.

I cannot make head or tail of this. If the connection is closed, the Statements and ResultSets derived from it are also closed. This is documented. If the Connection isn't closed, the Statements and ResultSets aren't closed either, unless you close them.

您必须组织代码,以免在Statement生命周期之外不依赖ResultSets,而在Connection生命周期之外不依赖Statement.这很容易.

You must organise your code so that you don't rely on ResultSets beyond the life of the Statement, and Statement beyond the life of the Connection. This is trivially easy.

这篇关于正确的连接方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆