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

How to use c3p0 database connection pool

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

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to use c3p0 database connection pool. the article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

1. Jar package

To quote an open source project, you naturally have to download someone else's jar package. I have a cloud disk here: c3p0-0.9.5-pre4.jar. There are actually three packages that I downloaded in open source China. If you still want to update them, you might as well search the Internet yourself.

II. Configuration file

As a database connection pool, there are naturally many parameters to set, of course, even if you do not set it, there is a default, but that may not meet your requirements. There are no special requirements for the configuration file here. It can be xml, properties or even txt. Of course, you can write it in the program if you want.

Here is my configuration file, which I put in the src directory called "c3p0.properties"

[java] view

Plaincopy

# jdbc basic Information

DriverClass=oracle.jdbc.driver.OracleDriver

JdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521/orcl

User=bctts

Password=bctts123

# c3p0 connection Pool Information

C3p0.minPoolSize=3

C3p0.maxPoolSize=25

# number of connections acquired by c3p0 at a time when connections in the connection pool are exhausted

C3p0.acquireIncrement=3

# define the number of repeated attempts after failed to obtain a new connection from the database

C3p0.acquireRetryAttempts=60

# interval between two connections (in milliseconds)

C3p0.acquireRetryDelay=1000

# all uncommitted operations are rolled back by default when the connection is closed

C3p0.autoCommitOnClose=false

# the time it takes for the client to wait for a new connection after calling getConnection () when the connection pool runs out. SQLException will be thrown after the timeout. If set to 0, it will wait indefinitely. Unit millisecond

C3p0.checkoutTimeout=3000

# check all free connections in the connection pool every 120 seconds. Default: 0

C3p0.idleConnectionTestPeriod=120

# maximum idle time. If it is not used within 60 seconds, the connection is discarded. If it is 0, never discard it. Default: 0

C3p0.maxIdleTime=600

# if set to true, the validity of the connection will be verified while obtaining the connection. Default: false

C3p0.testConnectionOnCheckin=true

# c3p0 will create an empty table named c3p0TestTable and test it with its own query statement.

Jdbc.automaticTestTable = c3p0TestTable

I won't explain the contents of the configuration file. I'm sure you can understand it at a glance.

Third, code implementation

The use of c3p0 is very simple, basically done in three steps.

DataSource unPooled = DataSources.unpooledDataSource (url,jdbcproperties); / / the second parameter here contains information about connecting to the database, including user, password, etc.

DataSource ds = DataSources.pooledDataSource (unPooled,c3propertis); / / the second parameter here contains the configuration information of the c3p0 connection pool

Connection conn = ds.getConnection ()

These three sentences of code are easy for everyone to understand, and they are also the core code that uses c3p0 connection pooling, which is really simple.

The following is an example I wrote for reference only.

[java] view

Plaincopy

Package com.bks.db

Import java.io.FileInputStream

Import java.sql.Connection

Import java.sql.SQLException

Import java.util.Properties

Import javax.sql.DataSource

Import com.mchange.v2.c3p0.DataSources

/ * *

* c3p0 connection pool management class

* @ author ICE

*

, /

Public class C3P0ConnentionProvider {

Private static final String JDBC_DRIVER = "driverClass"

Private static final String JDBC_URL = "jdbcUrl"

Private static DataSource ds

/ * *

* initialize the connection pool code block

, /

Static {

InitDBSource ()

}

/ * *

* initialize c3p0 connection pool

, /

Private static final void initDBSource () {

Properties c3p0Pro = new Properties ()

Try {

/ / load configuration file

C3p0Pro.load (new FileInputStream (". / bin/c3p0.properties"))

} catch (Exception e) {

E.printStackTrace ()

}

String drverClass = c3p0Pro.getProperty (JDBC_DRIVER)

If (drverClass! = null) {

Try {

/ / load driver classes

Class.forName (drverClass)

} catch (ClassNotFoundException e) {

E.printStackTrace ()

}

}

Properties jdbcpropes = new Properties ()

Properties c3propes = new Properties ()

For (Object key:c3p0Pro.keySet ()) {

String skey = (String) key

If (skey.startsWith ("c3p0.")) {

C3propes.put (skey, c3p0Pro.getProperty (skey))

} else {

Jdbcpropes.put (skey, c3p0Pro.getProperty (skey))

}

}

Try {

/ / establish connection pool

DataSource unPooled = DataSources.unpooledDataSource (c3p0Pro.getProperty (JDBC_URL), jdbcpropes)

Ds = DataSources.pooledDataSource (unPooled,c3propes)

} catch (SQLException e) {

E.printStackTrace ()

}

}

/ * *

* get database connection objects

* @ return data connection object

* @ throws SQLException

, /

Public static synchronized Connection getConnection () throws SQLException {

Final Connection conn = ds.getConnection ()

Conn.setTransactionIsolation (Connection.TRANSACTION_READ_COMMITTED)

Return conn

}

}

The following is an example of a test

[java] view

Plaincopy

Package test.demo

Import java.sql.Connection

Import java.sql.PreparedStatement

Import java.sql.ResultSet

Import java.sql.SQLException

Import com.bks.db.C3P0ConnentionProvider

Public class Demo {

Public static void main (String [] args) {

For (int iTuno Bandi)

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