为什么我们需要 JDBC 的连接池? [英] Why we need a connection pooling for JDBC?

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

问题描述

如果有一个用户的小型 CRUD 应用程序,我们是否可以只创建一个连接会话作为单身 ?

in case of a small CRUD application with one user, can we just create one connection session as a singleton ?

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

PS: I'm building a small javafx application back-ended with tiny h2 database (5 tables).

推荐答案

来自 Jon Skeet 对 连接和语句池有什么好处?:

From Jon Skeet's answer to What is the benefit of Connection and Statement Pooling?:

创建到数据库服务器的网络连接是(相对)昂贵的.同样要求服务器准备 SQL 语句是(相对)昂贵.

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

使用连接/语句池,您可以重用现有的连接/准备好的语句,避免启动一个成本连接,解析SQL等

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

以下内容来自 Kent Boogaart答案:

我对c3p0不熟悉,但是池化连接的好处和声明包括:

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

  1. 性能.连接到数据库既昂贵又缓慢.池连接可以物理连接到数据库,并在需要数据库访问的各种组件之间共享.这样,连接成本一次性支付并在所有情况下分摊消费组件.

  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.

诊断.如果你有一个子系统负责连接数据库,那么诊断和分析数据库就会变得更容易连接使用情况.

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

可维护性.同样,如果您有一个子系统负责分发数据库连接,您的代码将更易于维护而不是每个组件都连接到数据库本身.

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.

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

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