Java-Database Connection Pool Technology

 

Java-Database Connection Pool Technology

Database connection pool technology

basic concepts:

The interface defined in Java—DataSource interface provides an abstraction of any set of activation framework and data.

Commonly used database connection pool technology

C3P0

step:

Code:

Druid: Provided by Alibaba

step:

Code:


Pool technology can significantly optimize the performance of server applications to a certain extent, improve program execution efficiency and reduce system resource overhead. The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one;

Database connection pool technology

basic concepts:

Database connection pool: In fact, it is a container (collection), a container for storing database connections.  After the system is initialized, the container is created, and some connection objects are applied for in the container. When a user accesses the database, the connection object is obtained from the container. After the user accesses the database, the connection object is returned to the container.        

The benefits of using pooling technology: 1.  Save resources      2.  User access is efficient 3. Improve program performance and reduce system resource overhead

The interface defined in Java—DataSource interface provides an abstraction of any set of activation framework and data.

Common methods:

        Get connection: getConnection()

        Return the connection: Connection.close(). If the connection object Connection is obtained from the connection pool, then the Connection.close() method is called, and the connection will no longer be closed. But return the connection

Commonly used database connection pool technology

C3P0

         step:

            1.  Import the jar package (two)

                  c3p0-0.9.5.2.jar 、 mchange-commons-java-0.2.12.jar

            2.  Define the configuration file:

                 Name: c3p0.properties or c3p0-config.xml

                 Path: Put the file directly in the src directory.

            3.  Create the core object database connection pool object ComboPooledDataSource

            4.  Get the connection: getConnection

        Code:

<c3p0-config>
  <!-- 使用默认的配置读取连接池对象 -->
  <default-config>
  	<!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <!--初始化申请的连接数量-->
    <property name="initialPoolSize">5</property>
    <!--最大的连接数量-->
    <property name="maxPoolSize">10</property>
    <!--超时时间-->
    <property name="checkoutTimeout">3000</property>
  </default-config>

</c3p0-config>
public static void main(String[]args)throws SQLException{
	//1.获取数据库连接池对象
	DataSource ds=new ComboPooledDataSource();
	//2.获取连接对象
	Connection conn=ds.getConnection();
	//3.打印
	System.out.println(conn);
	//4.归还资源
	conn.close();
}
 

Druid: Provided by Alibaba

     step:

              1.  Import the jar package druid-1.0.9.jar

              2.  Define the configuration file:

                    Is in the form of properties

                    It can be called by any name and can be placed in any directory

              3.  Load the configuration file. Properties

              4.  Get database connection pool object: get DruidDataSourceFactory through factory

              5.  Get connection: getConnection

   Code:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db3
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
 public static void main(String[] args) throws Exception {
    //1.导入jar包
    //2.定义配置文件
    //3.加载配置文件
    Properties pro = new Properties();
    InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
    pro.load(is);
    //4.获取连接池对象
    DataSource ds = DruidDataSourceFactory.createDataSource(pro);
    //5.获取连接
    Connection conn = ds.getConnection();
    System.out.println(conn);

}

 It is not easy to create. If this blog is helpful to you, please remember to leave a message + like it. 

Guess you like

Origin blog.csdn.net/promsing/article/details/113714712