Java连接池 [英] Java Connection Pooling

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

问题描述

我搜索了连接池并阅读了有关内容.如果我正确理解,则连接池就像打开的连接的集合.如果建立或创建了连接,则应将其添加到连接池中;如果该连接已关闭,则应将其从连接池中删除.当它打开时,我可以一次又一次地使用它.

I searched for connection pooling and read about it. If I understand it correctly, a connection pool is like a collection of open connections. If a connection is established or created it should be added to the connection pool, and if that connection is closed it should be removed in connection pool; while it is open I can use it again and again.

在阅读这些教程和有关连接池的说明时,我有一些疑问:

While reading these tutorials and explanations about connection pooling I have some questions:

  1. 连接池只能在特定计算机上使用吗?像计算机A 无法与ComputerB共享其连接池?

  1. Can a pool of connections only be used on a certain computer? Like ComputerA cannot share its connection pool with ComputerB?

connection.close()应该放在哪里?

选择/加载记录时仅使用连接是否正确?得到返回的记录/数据后,我在finally语句处关闭连接.与添加,编辑和删除记录相同.在处理过程中,我放置了一个进度条,因此用户将不得不等待其完成并再次进行一些处理,这意味着我一次只能打开一个连接.

Is it correct to use a connection ONLY when selecting/loading record? After I got the returned records/data I close the connection at finally statement. Same as adding, editing and deleting records. And while it is processing I place a progress bar so the user will have to wait for it to be completed and to do some process again, which means I will only open connection one at a time.

感谢您的解释. :)

推荐答案

注意:我假设我们正在谈论

Note: I assume we're talking about the java.sql.Connection interface.

连接池只能在特定计算机上使用吗?像ComputerA无法与ComputerB共享其连接池吗?

Can a pool of connections only be used on a certain computer? Like ComputerA cannot share its connection pool with ComputerB?

正在运行的应用程序和数据库之间存在连接.自然,两台不同的计算机无法共享同一运行的应用程序,因此它们无法与数据库共享连接.

A connection exists between a running application and a database. Naturally, two different machines can't share the same running application, so they can't share connections with a database.

connection.close()应该放在哪里?

使用后,应始终确保在Connection实例上调用close()(通常在finally块中).如果使用池,则实际上会将连接返回到后台的池.参考:在池中关闭JDBC连接

You should always make sure to call close() on a Connection instance after using it (typically in a finally block). If pooling is being used, this will actually return the connection to the pool behind the scenes. Reference: Closing JDBC Connections in Pool

选择/加载记录时仅使用连接是否正确?得到返回的记录/数据后,我在finally语句处关闭连接.

Is it correct to use a connection ONLY when selecting/loading record? After I got the returned records/data I close the connection at finally statement.

是的,这是正确的.您不想手动挂接到Connection引用-使用它来执行SQL/DML,然后像在进行操作一样,通过在finally块中调用close()将其重新添加到池中.

Yes, that's correct. You don't want to manually hang on to a Connection reference - use it to execute SQL/DML and then check it back into the pool by calling close() in the finally block, just like you're doing.

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

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