Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Open source components: (1) DBCP and C3P0

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The emergence of a technology is either to solve practical problems or to optimize existing technologies. The emergence of database connection pool technology is to optimize the performance of database connection operations.

When using JDBC for database development, you generally go through the following process:

1) load the driver of the database

2) establish a connection to the database (Connection)

3) create a SQL statement declaration (Statement)

4) perform an update (executeUpdate) or query (executeQuery)

The database connection pooling described in this article is only a partial optimization for Connection.

Learning connection pool

a. Customize a connection pool

b. Learn excellent connection pooling components

1) DBCP

2) C3P0

1. Introduction

Consider: how is the Connection connection managed in the program?

The operations involved in the Connection of the database are: a) the database operation begins, the connection is created, and b) the operation ends and the connection is closed.

We know that connection resources are valuable, so we need to manage them. If the connection is opened and closed frequently, it will affect the running efficiency of the program!

The idea of connection management: create a set of connections in advance, take out one at a time when you use it, and put the connection back after use.

2. Custom connection pool

The first version

Package com.rk.pool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.LinkedList;/** * Custom connection pool, manage connection (Connection) * global parameters: initialization number, maximum number of connections, current connection number, connection pool set * * when launched, there are 3 (init_count) initial connections Connection * 1. The method to create a connection createConnection () * 2. Provide a method to obtain a connection getConnection () * 2.1 if there is a free connection, return directly * 2.2 if there are no idle connections and the maximum number of connections is not reached, then create a connection to return * 2.3 if there are no idle connections and the connection data reaches the maximum limit, the connection cannot be obtained Return null * 3. Provide a method for releasing connections releaseConnection (Connection conn) * 3.1 if the number of idle connections in the connection pool is less than the initial number of connections, the current connection is returned to the connection pool * 3.2 if the number of idle connections in the connection pool is equal to the initial number of connections Then close the current connection * @ author lsieun * * / public class MyConnectionPool {private final static String url = "jdbc:mysql:///testdb" Private final static String driverClassName = "com.mysql.jdbc.Driver"; private final static String user = "root"; private final static String password = "root"; private LinkedList freeConnections = null;//// connection pool (for all initialized connections) private final int init_count = 3 / / initial number of connections (Connection) (minimum) private final int max_count = 6 / maximum number of connections (maximum) private int current_count = 0 / / number of connections currently owned (current value) / * static code block Load the driver for the database * / static {try {Class.forName (driverClassName) } catch (ClassNotFoundException e) {throw new RuntimeException (e);} / / 1. Initialize public MyConnectionPool () throws SQLException {/ / initialize the connection pool freeConnections = new LinkedList (); / / add the specified number (init_cont) to the connection pool for (int ipool 0) I0) {return freeConnections.removeFirst ();} else if (current_count

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report