java - 为什么我们需要 JDBC 的连接池?

标签 java jdbc threadpool connection-pooling h2

<分区>

  • 使用像 DBCP 这样的 JDBC 连接池工具有什么好处?或 c3p0 ?

  • 如果是小型 CRUD 应用程序,有一个 用户,我们可以只创建一个 连接 session 作为 单例 ?

PS:我正在构建一个小型 javafx 应用程序,后端带有微型 h2 数据库(5 表)。

最佳答案

来自 Jon Skeet 对 What is the benefit of Connection and Statement Pooling? 的回答:

Creating a network connection to a database server is (relatively) expensive. Likewise asking the server to prepare a SQL statement is (relatively) expensive.

Using a connection/statement pool, you can reuse existing connections/prepared statements, avoiding the cost of initiating a connection, parsing SQL etc.

以及以下内容,来自 Kent Boogaart's answer :

I am not familiar with c3p0, but the benefits of pooling connections and statements include:

  1. Performance. Connecting to the database is expensive and slow. Pooled connections can be left physically connected to the database, and shared amongst the various components that need database access. That way the connection cost is paid for once and amortized across all the consuming components.

  2. Diagnostics. If you have one sub-system responsible for connecting to the database, it becomes easier to diagnose and analyze database connection usage.

  3. Maintainability. Again, if you have one sub-system responsible for handing out database connections, your code will be easier to maintain than if each component connected to the database itself.

关于java - 为什么我们需要 JDBC 的连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093054/

相关文章:

java - 使用 GSON 将 java.io.File 解析为 JSON

java - JDBC DriverManager 无法访问不同包中的类

java - 处理 HicariCP Oracle 连接尝试

java - ExecutorService 关闭钩子(Hook)

java - 需要在 Selenium IDE 中生成 java 语言脚本的帮助

java - 如何使用 Java 套接字读取远程服务器的响应

c# - 知道 ThreadPool 何时为空

java - Java 中每个键的线程池

java - 使用 7z 的 Commons 压缩给出的 Stream 不是 BZip2 格式

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link