Before the text

In the previous article, we talked about the traditional JDBC Connect MySQL The way , But this way, when making multiple connections , It's inefficient , Obviously not as efficient as connection pooling , So let's talk about it this time JDBC One of the connection pools :c3p0


1. preparation

  • IntelliJ IDEA
  • c3p0-
  • MySQL

2. To configure c3p0-config.xml


Before configuration , You need to build a database first , This article still uses the name in the previous article as customer The database of

And then in Engineering src Create a new directory named c3p0-config Of XML file ( The file name cannot be customized )

Then start configuration :

 <!-- Database address -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/customer</property>
<!-- Database driven -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- user name -->
<property name="user">root</property>
<!-- password -->
<property name="password">3865933</property>

Write basic database information first , Then we'll write the common connection pool properties , To query all properties , You can read the official website guide , Attributes are also included in :

 <!-- Number of connections created during connection pool initialization -->
<property name="initialPoolSize">1</property>
<!-- The minimum number of connections in the connection pool -->
<property name="minPoolSize">1</property>
<!-- The minimum number of connections in the connection pool -->
<property name="maxPoolSize">2</property>
<!-- When no idle connection is available , The number of new connections that can be created at one time -->
<property name="acquireIncrement">1</property>
<!-- Gets the number of repeated attempts after the database connection failed -->
<property name="acquireRetryAttempts">1</property>
<!-- The interval between two connections , The unit is millisecond -->
<property name = "acquireRetryDelay">1000</property>

Give a complete picture of the file :


You can add named-config To increase database configuration , Content and default-config Agreement , I connected to another database product :

3. Write a demo

Take care of the data source first :

 private static ComboPooledDataSource dataSource1 = new ComboPooledDataSource("test");
private static ComboPooledDataSource dataSource2 = new ComboPooledDataSource();

According to the data source , Connect

 private static void getConnection(){
Connection connection = dataSource1.getConnection();
System.out.println(" Connect to database product success !");
System.out.println(" Database connection closed !");
}catch(Exception e){
System.out.println(" Failed to connect to database !");
Connection connection = dataSource2.getConnection();
System.out.println(" Connect to database customer success !");
System.out.println(" Database connection closed !");
}catch(Exception e){
System.out.println(" Failed to connect to database !");
public static void main(String[] args){

Running results :

If multiple connections are needed , Using connection pooling is the best choice , About c3p0 This is the end of my explanation , thank you .

【JavaWeb】c3p0 Connection pool and MySQL More articles about

  1. dbcp/c3p0 Connection pool Settings mysql Session variables

    We have several timing tasks to calculate the risk control value , Almost every 5 Seconds will update the current total assets of all accounts and inform risk control accordingly , One or two are produced every hour G Of binlog, Dozens of servers .. The database is public , Code is generated automatically by tools , Direct modification process ...

  2. Network protocol finally{ return problem Injection problem jdbc Registration driven issues PreparedStatement Connection pool purpose 1.2.1DBCP Connection pool C3P0 Connection pool MYSQL Two ways of physical management JDBC Business DBUtils Business ThreadLocal Transaction features Concurrent access Isolation level

    1.1.1 API Detailed explanation : Registration drive DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Not recommended There are reasons 2 individual : > Cause driver to be registered 2 ...

  3. c3p0 Connection pool ]

    <c3p0-config> <!-- The default configuration --> <default-config> <property name="jdbcUrl" ...

  4. c3p0 The connection pool gets Connection perform close Whether the method is really destroyed Connection object ?

    Problem description : jfinal It's done api In the system , After a period of normal interface calls , When you suddenly call the interface again , The request did not respond api There is no error message in the background of the system ( The interface call is normal at the beginning , Suddenly there was no response ) So , Start looking for mistakes . ...

  5. C3P0 Connection pooling issues ,APPARENT DEADLOCK!!! Creating emergency..... [ Question points :20 branch , Tie tie person lovekong]

    use c3p0 Connection pool , Every time you debug the program , On first visit (Tomcat Access after the server is restarted ) There will be the following mistakes , Then it takes a long time to connect to the library , It can be connected in the end , It's OK to visit after that , Please consult the experts , I hope I can help you solve this problem ...

  6. HQL Inquiry and Hibernate Yes c3p0 Connection pooling support

    //HQL Inquire about // auto-import To set up true, If it is false, Write HQL To specify the full name of the class // Query all columns Query query = session.createQuery(" ...

  7. Use c3p0 Connection pool

    First, we need to know why we use connection pooling : because jdbc No ability to stay connected , Once it has not been used for a certain period of time ( About a few hundred milliseconds ), The connection is automatically released , Every time you create a new connection, you need 140 Millisecond or so, and C3P0 Connection pooling pools connections , along with ...

  8. Maven Under the project Spring MVC Site configuration ( 3、 ... and ) C3P0 Connection pool and @Autowired Application

    Maven Under the project Spring MVC Site configuration ( One ) Maven Under the project Spring MVC Site configuration ( Two ) Mybatis Data manipulation The first two articles mainly demonstrate the site and database operation configuration , If only ...

  9. Spring In the frame To configure c3p0 Connection pool Complete the access to the database

    The development of preparation : 1. Import jar package : ioc basic jar jdbcTemplate basic jar c3p0 basic jar Don't forget mysql Database driven jar The original program code : Do not use profile mode (IOC) Generate access database objects ...

Random recommendation

  1. iOS------- Apply performance tuning 25 Here's a piece of advice and tips

    Performance vs iOS Application development is particularly important , If your app is unresponsive or slow , Disappointments are filled with disappointments App Store The comments . However, due to the iOS Limitations of equipment , Sometimes it's hard to do well in performance . You will have a lot to pay attention to in the development process ...

  2. php Object oriented static Static properties and static method calls

    This article mainly introduces php Object oriented static Static properties and static method calls , Examples are analyzed static The principle and calling skill of static property and static method , Friends in need can refer to This article gives an example of php in static Static properties and static ...

  3. POJ 3071 Football

    I saw it a long time ago ... The most basic probability DP... Division coordination operation can easily determine who to compete with in the next field .    from——Dinic Algorithm                         Football Time ...

  4. ACM/ICPC And Expensive betrothal gifts - The shortest path solution (POJ1062)

    // Transfer to the shortest path problem , Enumerate the shortest path of every person who must pass no less than the chieftain level //Time:16Ms Memory:208K #include<iostream> #include<cstring ...

  5. Cassandra1.2 Document learning (14)—— Transaction and concurrency control

    Reference documents : ...

  6. POJ 3692 The largest independent set

    The question : Yes G A girl ,B A boy , All the girls know each other , All the boys know each other , also N For men and women , They know each other . Ask the largest number of people to choose from , Yes, they all know each other . Ideas : The composition of this problem is very clever , For his complementary composition , For all ...

  7. JAVA in , character string STRING And STRINGBUILDER The efficiency difference between the two

    If there are more variable string operations , use STRINGBUILDER Obviously, there are many advantages . public class HelloJava { public static void main(String[] args) ...

  8. SQL server 2008 Database backup and restore 、 Separate ( turn )

    SQL server 2008 Database backup and restore . Separate ( turn )   One .SQL Backup of database : 1. In turn, open The start menu → Program → Microsoft SQL Server 2008 → SQL Ser ...

  9. C# Efficient string connection StringBuilder Introduce

    Introducing StringBuilder Before , You have to understand string Characteristics of . string stay .NET Is a basic data type , It is also the only reference type in the basic data type . Strings can be declared as constants , But it's in the pile . One : Cannot be changed ...

  10. python Before climbing WeChat official account 10 A history article (4)- Regular expressions RegularExpressionPattern

    Regular expressions (regular expression) Describes a pattern of string matching (pattern), Can be used to check if a string contains a certain seed string . Replace a matched substring or remove a substring from a string that meets a condition, etc . Pytho ...