java 什么是 JDBC 中的连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/838518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:58:48  来源:igfitidea点击:

What is a Connection in JDBC?

javanetworkingjdbctcpconnection

提问by Geek

What is a Connection Object in JDBC ? How is this Connection maintained(I mean is it a Network connection) ? Are they TCP/IP Connections ? Why is it a costly operation to create a Connection every time ? Why do these connections become stale after sometime and I need to refresh the Pool ? Why can't I use one connection to execute multiple queries ?

什么是 JDBC 中的连接对象?这个连接是如何维护的(我的意思是它是一个网络连接)?它们是 TCP/IP 连接吗?为什么每次创建一个 Connection 都是一个代价高昂的操作?为什么这些连接在一段时间后变得陈旧,我需要刷新池?为什么我不能使用一个连接来执行多个查询?

采纳答案by Norbert Hartl

These connections are TCP/IP connections. To not have to overhead of creating every time a new connection there are connection pools that expand and shrink dynamically. You can use one connection for multiple queries. I think you mean that you release it to the pool. If you do that you might get back the same connection from the pool. In this case it just doesn't matter if you do one or multiple queries

这些连接是 TCP/IP 连接。为了不必每次创建新连接都产生开销,有动态扩展和收缩的连接池。您可以将一个连接用于多个查询。我认为您的意思是将其释放到池中。如果您这样做,您可能会从池中取回相同的连接。在这种情况下,执行一个或多个查询并不重要

The cost of a connection is to connect which takes some time. ANd the database prepares some stuff like sessions, etc for every connection. That would have to be done every time. Connections become stale through multiple reasons. The most prominent is a firewall in between. Connection problems could lead to connection resetting or there could be simple timeouts

连接的成本是连接需要一些时间。并且数据库为每个连接准备了一些东西,比如会话等。每次都必须这样做。由于多种原因,连接变得陈旧。最突出的是中间的防火墙。连接问题可能导致连接重置或可能有简单的超时

回答by sleske

To add to the other answers:

要添加到其他答案:

Yes, you can reuse the same connection for multiple queries. This is even advisable, as creating a new connection is quite expensive.

是的,您可以为多个查询重用同一个连接。这甚至是可取的,因为创建新连接的成本非常高。

You can even execute multiple queries concurrently. You just have to use a new java.sql.Statement/PreparedStatement instance for every query. Statements are what JDBC uses to keep track of ongoing queries, so each parallel query needs its own Statement. You can and should reuse Statements for consecutive queries, though.

您甚至可以同时执行多个查询。您只需要为每个查询使用一个新的 java.sql.Statement/PreparedStatement 实例。JDBC 使用语句来跟踪正在进行的查询,因此每个并行查询都需要自己的语句。不过,您可以而且应该对连续查询重用 Statements。

回答by ante.sabo

since I cannot comment yet, wil post answer just to comment on Vinegar's answer, situation with setAutoCommit() returning to default state upon returning connection to pool is not mandatory behaviour and should not be taken for granted, also as closing of statements and resultsets; you can read that it should be closed, but if you do not close them, they will be automatically closed with closing of connection. Don't take it for granted, since it will take up on your resources on some versions of jdbc drivers.

由于我还不能发表评论,我将发布答案只是为了评论 Vinegar 的答案,setAutoCommit() 在将连接返回到池时返回默认状态的情况不是强制性行为,不应被视为理所当然,也作为语句和结果集的关闭;您可以读到它应该关闭,但是如果您不关闭它们,它们将在连接关闭时自动关闭。不要认为这是理所当然的,因为它会占用您在某些版本的 jdbc 驱动程序上的资源。

We had serious problem on DB2 database on AS400, guys needing transactional isolation were calling connection.setAutoCommit(false) and after finishing job they returned such connection to pool (JNDI) without connection.setAutoCommit(old_state), so when another thread got this connection from pool, inserts and updates have not commited, and nobody could figure out why for a long time...

我们在 AS400 上的 DB2 数据库上遇到了严重的问题,需要事务隔离的人正在调用 connection.setAutoCommit(false) 并且在完成工作后他们将这样的连接返回到池 (JNDI) 没有 connection.setAutoCommit(old_state),所以当另一个线程获得这个连接时从池中,插入和更新没有提交,很长一段时间没人能弄清楚为什么......

回答by Alohci

The answers to your questions is that they are implementation defined. A JDBC connection is an interface that exposes methods. What happens behind the scenes can be anything that delivers the interface. For example, consider the Oracle internal JDBC driver, used for supporting java stored procedures. Simultaneous queries are not only possible on that, they are more or less inevitable, since each request for a new connection returns the one and only connection object. I don't know for sure whether it uses TCP/IP internally but I doubt it.

您的问题的答案是它们是实现定义的。JDBC 连接是一个公开方法的接口。幕后发生的事情可以是提供界面的任何事情。例如,考虑用于支持 java 存储过程的 Oracle 内部 JDBC 驱动程序。同时查询不仅在这方面是可能的,而且或多或少是不可避免的,因为对新连接的每个请求都返回一个且唯一的连接对象。我不确定它是否在内部使用 TCP/IP,但我对此表示怀疑。

So you should not assume implementation details, without being clear about precisely which JDBC implementation you are using.

所以你不应该假设实现细节,而不是明确你正在使用的 JDBC 实现。