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

Database connection pool

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

Share

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

Connection pooling is a mechanism for managing database connections that can control the number of connections, and by default you can create available connections in advance.

There are four common connection pooling frameworks

1. DBCP connection pool of Apache (DBCP is built into Tomcat)

2. C3P0 connection pool

3. Proxcool connection pool

4. The druid framework of Ali Company.

First, introduce Maven (only record the use of DBCP and C3P0)

Mysql

Mysql-connector-java

8.0.16

Com.mchange

C3p0

0.9.5.4

Org.apache.commons

Commons-dbcp2

2.6.0

Second, create a connection pool. DataSource provides getConnection interface. Here, the factory mode is used to obtain the connection of the database connection pool.

Package com.neusoft.busmis.fatory

Import java.sql.Connection

Import javax.sql.DataSource

Import org.apache.tomcat.dbcp.dbcp2.BasicDataSource

/ / DBCP connection pool

Public class ConnectionFactoryWithDBCP {

Private static DataSource ds = null

Static {

BasicDataSource bds = new BasicDataSource ()

Bds.setDriverClassName ("com.mysql.jdbc.Driver")

Bds.setUrl ("jdbc:mysql://localhost:3306/busmis?serverTimezone=GMT%2B8")

Bds.setUsername ("root")

Bds.setPassword ("123456")

Bds.setInitialSize (1); / / set the initial number of connections

Bds.setMaxTotal (2); / / set the maximum number of connections

Bds.setMaxIdle (2)

Bds.setMaxWaitMillis (2000); / / set waiting time

Ds = bds

}

Public static Connection getConnection () throws Exception {

Return ds.getConnection ()

}

}

Package com.neusoft.busmis.fatory

Import java.sql.Connection

Import javax.sql.DataSource

Import com.mchange.v2.c3p0.ComboPooledDataSource

Public class ConnectionFactoryWithC3P0 {

Private static DataSource ds = null

Static {

ComboPooledDataSource cpds = new ComboPooledDataSource ()

Try {

Cpds.setDriverClass ("com.mysql.jdbc.Driver")

Cpds.setJdbcUrl ("jdbc:mysql://localhost:3306/busmis?serverTimezone=GMT%2B8")

Cpds.setUser ("root")

Cpds.setPassword ("123456")

Cpds.setMinPoolSize (1); / / set the minimum number of connections

Cpds.setAcquireIncrement (1); / / the number of new connections at a time

Cpds.setMaxPoolSize (10); / / set the maximum number of connections

} catch (Exception e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

Ds = cpds

}

Public static Connection getConnection () throws Exception {

Return ds.getConnection ()

}

Public static DataSource getDataSource () throws Exception {

Return ds

}

}

Third, put the connection pool to JNDI

Because in the java mechanism, if a reference is not used for too long, it will be cleared automatically, and sometimes the actions of destroying and creating will inevitably be repeated. So put the connection pooling configuration on the JNDI so that it exists when the server starts.

Configure database information in the context.xml of tomcat.

Http://www.renrendoc.com/p-21933469.html

Zhengzhou Infertility Hospital: http://www.zzchyy110.com/

The main features of DBCP are drivereClassName (database driver), url (database address), username (database user name), password (database driver), name (name of Resource), maxIdle (maximum free count), maxActive (maximum number of activities), auth (connection pool manager, Container means handing over to Tomcat management), maxWait (maximum waiting time ms), type (class DataSource).

Note: if you use JNDI, you must publish the project to Web for it to take effect.

Factory mode creates connections and uses the lookup of the Context class to find configuration information, java:/comp/env/name.

Package com.neusoft.busmis.fatory

Import java.sql.Connection

Import javax.naming.Context

Import javax.naming.InitialContext

Import javax.sql.DataSource

Public class ConnectionFactoryWithJNDI {

Private static DataSource ds = null

Static {

Try {

Context ct = new InitialContext ()

Ds = (DataSource) ct.lookup ("java:/comp/env/mysql3306busmis-c3p0")

Ct.close ()

} catch (Exception e) {

E.printStackTrace ()

}

}

Public static Connection getConnection () throws Exception {

Return ds.getConnection ()

}

}

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