JDBC连接池:连接重用? [英] JDBC Connection Pooling: Connection Reuse?

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

问题描述

根据我的理解,JDBC连接池(在基本级别)以这种方式工作:

As per my understanding, JDBC Connection Pooling (at a basic level) works this way:


  1. 在应用初始化期间创建连接并放置在缓存中

  2. 按需向应用程序提供这些缓存的连接

  3. 一个单独的线程维护连接池,执行以下活动:


    • 丢弃已使用的连接(已关闭)

    • 创建新连接并添加到缓存以维持特定的连接数

  1. create connections during app initialization and put in a cache
  2. provide these cached connections on demand to the app
  3. a separate thread maintains the Connection Pool, performing activities like:
    • discard connections that have been used (closed)
    • create new connections and add to the cache to maintain a specific count of connections

但是,每当我在JDBC连接池中听到连接重用一词时讨论,我感到困惑。何时进行连接重用?

But, whenever I hear the term "connection reuse" in a JDBC Connection Pooling discussion, I get confused. When does the connection reuse occurs?

这是否意味着连接池为两个不同的数据库交互提供相同的连接(不关闭它)?或者,有没有办法继续使用连接,即使它在数据库调用后关闭?

Does it means that Connection Pool provides the same connection for two different database interactions (without closing it)? Or, is there a way to continue using a connection even after it gets closed after a DB call?

推荐答案

连接池的工作原理重新使用连接。应用程序从池中借用连接,然后在完成时返回它。然后将连接再次分发给应用程序的另一部分,甚至是另一个应用程序。

Connection pooling works by re-using connections. Applications "borrow" a connection from the pool, then "return" it when finished. The connection is then handed out again to another part of the application, or even a different application.

只要不使用相同的连接,这是完全安全的两个线程同时存在。

This is perfectly safe as long as the same connection is not is use by two threads at the same time.

连接池的关键点是尽可能避免创建新连接,因为它通常是一项昂贵的操作。重用连接对性能至关重要。

The key point with connection pooling is to avoid creating new connections where possible, since it's usually an expensive operation. Reusing connections is critical for performance.

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

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