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

What are the characteristics and steps of java database connection pool

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the characteristics and steps of java database connection pool, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can gain something.

Connection pool concept

The basic idea of database connection pool is to establish a "buffer pool" for database connections. Put a certain number of connections in the buffer pool in advance, and when you need to establish a database connection, you only need to take one out of the "buffer pool" and put it back after use.

Database connection pooling is responsible for allocating, managing, and releasing database connections, allowing applications to reuse an existing database connection rather than re-establish one.

The database connection pool creates a certain number of database connections into the connection pool at initialization, and the number of these database connections is set by the minimum number of database connections. Regardless of whether these database connections are used or not, the connection pool will always ensure that there are at least so many connections. The maximum number of database connections in the connection pool limits the maximum number of connections that can be occupied by the connection pool. when the number of connections requested by the application from the connection pool exceeds the maximum number of connections, these requests will be added to the waiting queue.

Disadvantages of traditional connection

1. A normal JDBC database connection is obtained using DriverManager, and each time you establish a connection to the database, you load the Connection into memory and verify the user name and password (it takes 0.05s~1s time). When you need a database connection, ask for one from the database, and then disconnect after execution. This way will consume a lot of resources and time. The connection resources of the database have not been well reused. If hundreds or even thousands of people are online at the same time, frequent database connection operations will take up a lot of system resources, and even cause the server to crash.

two。 For each database connection, it has to be disconnected after use. Otherwise, if the program fails to close due to an exception, it will cause a memory leak in the database system, which will eventually lead to a restart of the database. (recall what is the memory leak of Java in ∶?)

3. This kind of development can not control the number of connection objects created, and system resources will be allocated inadvertently, such as too many connections, which may lead to empty memory leakage and server crash.

The advantages of database connection pooling improve the response speed of programs (reduce the corresponding time for creating connections) reduce resource consumption (can be reused and provide good connections) easy connection management implementation

DBCP is a database connection pool provided by Apache. The tomcat server comes with dbcp database connection pooling. The speed is faster than c3p0, but BUG,Hibernate3 no longer provides support because of its own existence.

C3P0 is a database connection pool provided by an open source organization, which is relatively slow and stable. Hibernate officially recommends the use of

Proxool is an open source project database connection pool under sourceforge. It has the function of monitoring the status of connection pool, and its stability is a little worse than that of c3p0.

BoneCP is a fast database connection pool provided by an open source organization.

Druid is a database connection pool provided by Ali. It is said to be a database connection pool that combines the advantages of DBCP, C3P0 and Proxool, but the speed is uncertain whether it is as fast as BoneCP.

Demonstrate Druid connection

Connection steps: ① imports jar package, ② tests connection code, ③ writes configuration file

Druid is a database connection pooling implementation on Alibaba's open source platform. It combines the advantages of DB pooling such as C3P0, DBCP and Proxool, and adds log monitoring, which can well monitor DB pooled connections and SQL execution. It can be said that DB connection pooling is generated for monitoring. * * is one of the best connection pooling at present.

DRUID

① Guide package

② test connection code

③ write configuration file

QueryRunner insert operation public static void testInsert () {Connection conn = null; try {QueryRunner runner = new QueryRunner (); conn = JDBCUtils.getConnection3 (); String sql = "insert into customers (name,email,bitrh) values (?,?)"; int insertCount = runner.update (conn, sql, "Chen Zihong", "chenzihong@123", "1994-09-09") System.out.println ("added" + insertCount + "records");} catch (SQLException e) {e.printStackTrace ();} finally {JDBCUtils.colseResource (conn, null) }} QueryRunner query operation / / Test query: BeanHander: is the implementation class of ResultSetHandler interface, which is used to encapsulate a record in the table public static void testQuery1 () {Connection conn = null; try {QueryRunner runner = new QueryRunner (); conn = JDBCUtils.getConnection3 (); String sql = "select id,name,email,birth from customers where id =?" BeanHandler handler = new BeanHandler (Customer.class); Customer customer = runner.query (conn, sql, handler, 21); System.out.println (customer);} catch (SQLException e) {e.printStackTrace ();} finally {JDBCUtils.colseResource (conn, null);}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report