mysql连接池DataSource,DruidDataSource的理解及其使用

36 阅读7分钟

mysql连接池DataSource,DruidDataSource的理解及其使用

在这里插入图片描述

博主 默语带您 Go to New World.
个人主页—— 默语 的博客👦🏻
《java 面试题大全》
🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭
《MYSQL从入门到精通》数据库是开发者必会基础之一~
🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄之助。苟未尽善尽美,敬请批评指正,以资改进。!💻⌨

1.DataSource理论知识

什么是数据源-DataSource

简单理解为数据源头,提供了应用程序所需要数据的位置。数据源保证了应用程序与目标数据之间交互的规范和协议,它可以是数据库,文件系统等等。其中数据源定义了位置信息,用户验证信息和交互时所需的一些特性的配置,同时它封装了如何建立与数据源的连接,向外暴露获取连接的接口。应用程序连接数据库无需关注其底层是如何如何建立的,也就是说应用业务逻辑与连接数据库操作是松耦合的。
以下只讨论当数据源为数据库的情况,且为Java环境下JDBC规范下的如何建立与数据库的连接,其他情况类似。

JDBC2.0

提供了javax.sql.DataSource接口,它负责建立与数据库的连接,当在应用程序访问数据库时不必编写连接数据库的代码,直接引用DataSource获取数据库的连接对象即可。用于获取操作数据库Connection对象。

数据源与数据连接池的关系

数据源DataSource建立多个数据库连接池Connection Pool,这些数据库连接(Connection)会保存在数据连接池中,当需要访问数据库时,只需要你从数据库连接池中获取空闲的数据库的连接,当程序员访问数据库结束时,数据连接会放回数据库连接池中。

数据库连接池的优势

传统的JDBC访问数据库技术,每次访问数据库都需要通过数据库驱动器Driver和数据库名称以及密码等等资源建立数据库连接。这样的连接存在俩大问题:1.频繁的建立数据库连接与断开数据库,会消耗大量的资源和时间,降低效率。2,数据库的连接需要用户名和密码等等。这些需要一定的内存和cpu一定开销。

在这里插入图片描述

DataSource source = new DataSource();

我们进入到他的源码去看下

原版

/*
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

 */

package javax.sql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Wrapper;

/**
 * <p>A factory for connections to the physical data source that this
 * {@code DataSource} object represents.  An alternative to the
 * {@code DriverManager} facility, a {@code DataSource} object
 * is the preferred means of getting a connection. An object that implements
 * the {@code DataSource} interface will typically be
 * registered with a naming service based on the
 * Java&trade; Naming and Directory (JNDI) API.
 * <P>
 * The {@code DataSource} interface is implemented by a driver vendor.
 * There are three types of implementations:
 * <OL>
 *   <LI>Basic implementation -- produces a standard {@code Connection}
 *       object
 *   <LI>Connection pooling implementation -- produces a {@code Connection}
 *       object that will automatically participate in connection pooling.  This
 *       implementation works with a middle-tier connection pooling manager.
 *   <LI>Distributed transaction implementation -- produces a
 *       {@code Connection} object that may be used for distributed
 *       transactions and almost always participates in connection pooling.
 *       This implementation works with a middle-tier
 *       transaction manager and almost always with a connection
 *       pooling manager.
 * </OL>
 * <P>
 * A {@code DataSource} object has properties that can be modified
 * when necessary.  For example, if the data source is moved to a different
 * server, the property for the server can be changed.  The benefit is that
 * because the data source's properties can be changed, any code accessing
 * that data source does not need to be changed.
 * <P>
 * A driver that is accessed via a {@code DataSource} object does not
 * register itself with the {@code DriverManager}.  Rather, a
 * {@code DataSource} object is retrieved though a lookup operation
 * and then used to create a {@code Connection} object.  With a basic
 * implementation, the connection obtained through a {@code DataSource}
 * object is identical to a connection obtained through the
 * {@code DriverManager} facility.
 * <p>
 * An implementation of {@code DataSource} must include a public no-arg
 * constructor.
 *
 * @since 1.4
 */

public interface DataSource  extends CommonDataSource, Wrapper {

  /**
   * <p>Attempts to establish a connection with the data source that
   * this {@code DataSource} object represents.
   *
   * @return  a connection to the data source
   * @exception SQLException if a database access error occurs
   * @throws java.sql.SQLTimeoutException  when the driver has determined that the
   * timeout value specified by the {@code setLoginTimeout} method
   * has been exceeded and has at least tried to cancel the
   * current database connection attempt
   */
  Connection getConnection() throws SQLException;

  /**
   * <p>Attempts to establish a connection with the data source that
   * this {@code DataSource} object represents.
   *
   * @param username the database user on whose behalf the connection is
   *  being made
   * @param password the user's password
   * @return  a connection to the data source
   * @exception SQLException if a database access error occurs
   * @throws java.sql.SQLTimeoutException  when the driver has determined that the
   * timeout value specified by the {@code setLoginTimeout} method
   * has been exceeded and has at least tried to cancel the
   * current database connection attempt
   * @since 1.4
   */
  Connection getConnection(String username, String password)
    throws SQLException;
}

翻译版本

/*
版权所有(c) 2000年,2013年,甲骨文和/或其附属公司。保留所有权利。
* ORACLE专有/保密。使用受许可条款的约束。
 */

package javax.sql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Wrapper;

/**

<p>连接到此物理数据源的工厂

* {@code DataSource}对象表示。的替代方案

* {@code DriverManager}工具,一个{@code DataSource}对象

*是获得连接的首选方式。一个对象,它实现

{@code DataSource}接口通常是

注册了一个命名服务

* Java&trade;命名和目录(JNDI) API。

* < P >

{@code DataSource}接口由驱动程序供应商实现。

*有三种实现类型:

* < OL >

基本实现——生成一个标准的{@code连接}

*对象

* <LI>连接池实现——产生一个{@code连接}

*将自动参与连接池的对象。这

*实现与中间层连接池管理器一起工作。

* <LI>分布式事务实现——生成一个

*可用于分布式的{@code Connection}对象

*事务和几乎总是参与连接池。

这个实现与中间层一起工作

*事务管理器,几乎总是与连接

*池经理。

* < / OL >

* < P >

一个{@code DataSource}对象具有可以修改的属性

*在必要的时候。例如,如果数据源被移动到不同的

* server,服务器的属性可以更改。好处是

*因为数据源的属性可以修改,任何代码都可以访问

*该数据源不需要更改。

* < P >

通过{@code DataSource}对象访问的驱动程序不会

*注册自己的{@code DriverManager}。相反,一个

*通过查找操作检索{@code DataSource}对象

*,然后用来创建一个{@code Connection}对象。一个基本

通过{@code DataSource}获得的连接


对象与通过

* {@code DriverManager}设施。

* < p >

{@code DataSource}的实现必须包含一个公共的无参数

*构造函数。

以上翻译结果来自有道神经网络翻译(YNMT)

逐句对照
 *
 * @since 1.4
 */

public interface DataSource  extends CommonDataSource, Wrapper {

  /**
 * <p>尝试建立与数据源的连接

{@code DataSource}对象表示。

*

返回到数据源的连接

如果数据库访问错误发生,@exception SQLException

当驱动程序确定java.sql.SQLTimeoutException时

*由{@code setLoginTimeout}方法指定的超时值

*已超过,并至少试图取消

当前的数据库连接尝试

以上翻译结果来自有道神经网络翻译(YNMT)
   */
  Connection getConnection() throws SQLException;

  /**
  * <p>尝试建立与数据源的连接

{@code DataSource}对象表示。

*

@param用户名代表连接的数据库用户

*由

@param password用户的密码

返回到数据源的连接

如果数据库访问错误发生,@exception SQLException

当驱动程序确定java.sql.SQLTimeoutException时

*由{@code setLoginTimeout}方法指定的超时值

*已超过,并至少试图取消

当前的数据库连接尝试

* @since 1.4

以上翻译结果来自有道神经网络翻译(YNMT)

   */
  Connection getConn* <p>尝试建立与数据源的连接

{@code DataSource}对象表示。

*

@param用户名代表连接的数据库用户

*@param password用户的密码

返回到数据源的连接

如果数据库访问错误发生,@exception SQLException

当驱动程序确定java.sql.SQLTimeoutException时

*由{@code setLoginTimeout}方法指定的超时值

*已超过,并至少试图取消

当前的数据库连接尝试

* @since 1.4
  */


以上翻译结果来自有道神经网络翻译(YNMT)
ection(String username, String password)
    throws SQLException;
}

我们可以根据上面的内容得出

DataSource 是作为为数据源,它是 jdk 提供的一个接口,然后只提供了两个 getConnection 方法,分别是无参数构造(无需参数)和有参构造(需要传入用户名和密码)。所以说它是跟数据库连接有关的东西,可以通过它来获取数据库连接。

datasource的实现方式

  • 基本数据源(不支持连接池和分布式
  • 连接池的数据源(支持连接池的处理连接,连接能够重复利用)
  • 分布式的数据源(支持分布式的事务,一个事务能够访问更多数据库服务)

但是这只是一个接口,具体怎么获取到数据库连接,还是由实现它的子类来决定。本文就是来讲一下 DruidDataSource

未完;…

如对本文内容有任何疑问、建议或意见,请联系作者,作者将尽力回复并改进📓;(联系微信:Solitudemind )