jdbc连接池工作原理

The JDBC Connection interface is a part of java.sql package. The java.sql.Connection interface defines the contract for relational database vendors. The vendor connection classes must implement these methods. An instance of Connection is used to communicate with the Database.

JDBC Connection接口是java.sql包的一部分。 java.sql.Connection接口定义关系数据库供应商的合同。 供应商连接类必须实现这些方法。 Connection的一个实例用于与数据库进行通信。

The implementation of the Connection interface depends on the database vendor. So we don’t need to worry about implementation details as long as the vendor follows the contract.

Connection接口的实现取决于数据库供应商。 因此,只要供应商遵守合同,我们就不必担心实施细节。

如何建立JDBC连接 (How to Establish JDBC Connection)

Let’s have a look at the below steps to establish JDBC Connection.

让我们看一下以下建立JDBC连接的步骤。

1.注册JDBC驱动程序 (1. Register JDBC Driver)

JDBC driver is registered using below method.

使用以下方法注册JDBC驱动程序。

Class.forName(String className): This method is used to load the JDBC driver class into JVM memory. The string argument is the fully classified name of the Connection implementation class.

Class.forName(String className) :此方法用于将JDBC驱动程序类加载到JVM内存中。 字符串参数是Connection实现类的完全分类的名称。

// for MySQL
Class.forName("com.mysql.jdbc.Driver");// for PostgreSQL
Class.forName("org.postgresql.Driver");

Let’s understand what is happening behind the scene. How JDBC Driver gets registered by using this syntax.

让我们了解幕后发生的事情。 使用此语法如何注册JDBC驱动程序。

First of all, the JDBC driver’s jar file is included in the classpath of the java application. The DriverManager class uses service provider mechanism to search for jar files having a file named java.sql.Driver. This file must be present in the META-INF/services folder of the driver’s jar.

首先,JDBC驱动程序的jar文件包含在java应用程序的类路径中。 DriverManager类使用服务提供者机制来搜索具有名为java.sql.Driver的文件的jar文件。 该文件必须存在于驱动程序jar的META-INF / services文件夹中。

This is a simple text file that contains the full name of the class that implements the java.sql.Driver interface. For example, full name for MySQL Driver is com.mysql.jdbc.Driver. For PostgreSQL Driver, it is org.postgresql.Driver.

这是一个简单的文本文件,其中包含实现java.sql.Driver接口的类的全名。 例如,MySQL驱动程序的全名是com.mysql.jdbc.Driver。 对于PostgreSQL驱动程序,它是org.postgresql.Driver。

Jdbc Connection Implementation Jar

Jdbc连接实现Jar

When the Driver class is loaded into memory, its static initialization block is executed. The static method of the DriverManager class is called to register the driver.

将Driver类加载到内存时,将执行其静态初始化块。 调用DriverManager类的静态方法来注册驱动程序。

public class VendorDriver implements java.sql.Driver
{
static {VendorDriver driver = new VendorDriver();DriverManager.registerDriver(driver);}
}

Jdbc Connection Register Driver

Jdbc连接寄存器驱动程序

This will register an instance of Driver class into the DriverManager.

这会将Driver类的实例注册到DriverManager中。

2.创建JDBC连接 (2. Create JDBC Connection)

We can get the JDBC connection using DriverManager.getConnection() method.

我们可以使用DriverManager.getConnection()方法获得JDBC连接。

Connection conn = DriverManager.getConnection(url, user, password);

Let’s understand the parameters that we have passed to get the JDBC connection.

让我们了解为获取JDBC连接而传递的参数。

  • url: The JDBC URL which is used to determine which driver implementation to use for a given connection.

    jdbc:postgresql://hostname:port/databasenamejdbc:postgresql://127.0.0.1:5432/javadb

    The first part indicates that this is the JDBC URL.

    The second part indicates the driver vendor which is PostgreSQL in our case.

    The third part indicates the hostname or IP address with the port of the database.

    The last part indicates the name of the database.

    url :JDBC URL,用于确定用于给定连接的驱动程序实现。

    第一部分指示这是JDBC URL。

    第二部分说明驱动程序供应商,在本例中为PostgreSQL。

    第三部分用数据库端口指示主机名或IP地址。

    最后一部分指示数据库的名称。

  • user: user of the database for which we get the connection.user :我们为其连接的数据库的用户。
  • password: password to access the given database.password :访问给定数据库的密码。

Creating Jdbc Connection

创建Jdbc连接

JDBC连接示例 (JDBC Connection Example)

Let’s have a look at the below example program. In below program, I have used PostgreSQL database so I have used the respective jar for PostgreSQL.

让我们看一下下面的示例程序。 在下面的程序中,我使用了PostgreSQL数据库,所以我在PostgreSQL中使用了相应的jar。

package com.journaldev.examples;import java.sql.Connection;
import java.sql.DriverManager;/*** JDBC Connection Example* * @author pankaj**/
public class JDBCConnection {public static void main(String[] args) {Connection connection = null;try {System.out.println("------JDBC Connection Example--------");Class.forName("org.postgresql.Driver");connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/journaldev", "postgres", "admin");System.out.println("----JDBC Connection Established------");System.out.println("Catalog: "+connection.getCatalog());System.out.println("Schema: "+connection.getSchema());System.out.println("Info: "+connection.getClientInfo());} catch (Exception e) {e.printStackTrace();}finally {try {if (connection!=null) {connection.close();System.out.println("Connection is Closed.");}} catch (Exception e2) {e2.printStackTrace();}}}
}

Output:

输出

Establish Jdbc Connection Program Output

建立Jdbc连接程序输出

翻译自: https://www.journaldev.com/29142/how-does-jdbc-connection-actually-work

jdbc连接池工作原理

jdbc连接池工作原理_JDBC连接实际上如何工作?相关推荐

  1. mysql连接池的工作原理_连接池工作原理

    连接池工作原理 连接池技术的核心思想是连接复用,通过建立一个数据库连接池以及一套连接使用.分配和管理策略,使得该连接池中的连接可以得到高效.安全的复用,避免了数据库连接频繁建立.关闭的开销. 连接池的 ...

  2. 连接池的原理以及分析

    day21-连接池和DBUtils 今日内容 连接池 自定义连接池---------------了解即可(熟练装饰者设计模式) 难点 使用第三方连接池---------重点\掌握 C3P0 DRUID ...

  3. JDBC实例--JDBC连接池技术解密,连接池对我们不再陌生

    一.为什么我们要用连接池技术? 前面的数据库连接的建立及关闭资源的方法有些缺陷.统舱传统数据库访问方式:一次数据库访问对应一个物理连接,每次操作数据库都要打开.关闭该物理连接, 系统性能严重受损. 解 ...

  4. python连接池原理_python redis之连接池的原理

    python redis之连接池的原理 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下, 这是没 ...

  5. 连接池:别让连接池帮了倒忙

    今天,我再与你说说另一种很重要的池化技术,即连接池. 我先和你说说连接池的结构.连接池一般对外提供获得连接.归还连接的接口给客户端使用,并暴露最小空闲连接数.最大连接数等可配置参数,在内部则实现连接建 ...

  6. 连接池超时配置_HttpClient连接池的一些思考

    前言 使用apache的httpclient进行http的交互处理已经很长时间了,而httpclient实例则使用了http连接池,想必大家也没有关心过连接池的管理.事实上,通过分析httpclien ...

  7. 04 | 连接池:别让连接池帮了倒忙

    04 | 连接池:别让连接池帮了倒忙 连接池一般对外提供获得连接.归还连接的接口给客户端使用,并暴露最小空闲连接数.最大连接数等可配置参数,在内部则实现连接建立.连接心跳保持.连接管理.空闲连接回收. ...

  8. springboot2整合redis使用lettuce连接池(解决lettuce连接池无效问题)

    lettuce客户端 Lettuce 和 Jedis 的都是连接Redis Server的客户端程序.Jedis在实现上是直连redis server,多线程环境下非线程安全(即多个线程对一个连接实例 ...

  9. druid连接池mysql5.7_MySQL Druid连接池,Apache的DbUtils使用

    一.Druid连接池在程序初始化时,预先创建指定数量的数据库连接对象存储在池中. 当需要连接数据库时,从连接池中取出现有连接: 使用完毕后, 也不会进行关闭,而是放回池中,实现复用,节省资源. 1.1 ...

最新文章

  1. 潜在狄利克雷分配(LDA,Latent Dirichlet Allocation)模型(一)
  2. Linux常见的一些性能监控命令
  3. python类定义中、对象字符串的特殊方法是_python中自定义类对象json字符串化的方法_python json转字符串、...
  4. SAP CRM OData模型里的addressable为true的含义
  5. ZooKeeper編程02--多線程的分佈式鎖
  6. Python 数据分析三剑客之 Matplotlib(六):直方图 / 柱状图 / 条形图的绘制
  7. 两次被裁之后,我终于解决了数据库缓存一致性问题
  8. 计算机英语讲课笔记01
  9. Spring MVC如何测试Controller(使用springmvc mock测试)
  10. (43)System Verilog模块变量数据位宽扩展
  11. 李大维:互联网人做硬件创业容易产生的七大误解【转载】
  12. 自然语言处理(NLP)与自然语言理解(NLU)的区别
  13. SQL必知必会【极客时间笔记】
  14. 三维重建之环境搭建1-VS2017安装
  15. 《未来世界的幸存者》 读后感言
  16. 数据库系统的主要组成部分
  17. uniapp 导航栏滚动渐显渐隐,自定义返回按钮,返回图标背景色透明
  18. 敏捷迭代开发——Time-Boxing时间盒
  19. Obama's speech in Chicago
  20. R语言 如何合并csv文件(批量读取csv文件)

热门文章

  1. IOS一些常用的越狱渠道
  2. 试用了GIMP的Smart remove selection,结果有些失望啊,哈哈
  3. pip “Cannot uninstall ‘pip包‘. It is a distutils installed project...“ 解决方法
  4. Mysql常用的几种join连接方式
  5. 前端几个常用简单的开发手册拿走不谢
  6. codeforces 702A A. Maximum Increase(水题)
  7. jQuery1.9.1源码分析--Animation模块
  8. 正则解析多重循环模板
  9. 虚拟函数-1、静态联编与动态联编,引入虚函数
  10. python是交互式语言吗_什么是Python交互式解释器