Simple use of quick learning C3p0 database connection (with configuration file)

Use of c3p0 database connection pool (I)

Theoretical knowledge of database connection pool

If we want to use c3p0 database connection pool, we must first understand what database connection pool is and why it is introduced

  • When we use JDBC to interact with the database, a Connection will be created every time we access the database. After the operation of the database is completed, the Connection will be closed immediately, and it will be reconnected in the next access. This will lead to a disadvantage. Applying to create a Connection is to deal with the bottom of the computer, and each application Connection operation will consume a lot of time.

  • Therefore, in order to solve the above problems, programmers not only want to get the connection, but also don't want to spend a lot of time and resources waiting for the application connection in the operation process. They should spend a lot of time on the business processing level. So there is a database connection pool. The "pool" of the data connection pool can be imagined as a pool. Many of the same things can be placed in the pool and its activity can be maintained. The database connection pool is a pool for database connections. Many database connections can be placed in it. When we need to use the database connection, we only need to take it from the pool (through the address), and the application for database connection in the database connection pool is also a time-consuming operation. What's the difference from before? Indeed, the database connection in the database connection pool also needs to be applied. The only difference is that the database is connected to the application in advance during application initialization, and the connection is not closed. In this way, we no longer need to wait for applying for database connection when we conduct business processing, but "take" it directly from the database connection pool

  • Database connection pool technology is an idea. Many companies implement this idea. Different companies have different underlying technologies of database connection pool, but the implementation effect is the same. What we need is to use them. Common database connection pool technologies on the market are: c3p0, druid, DBCP... What we learn today is c3p0

Specific code implementation C3p0

Database creation

Since c3p0 is a third party, we need to use the jar package of c3p0. (maven projects can import dependencies directly) https://mvnrepository.com/artifact/com.mchange/c3p0 (this is the specific connection in maven. After finding the corresponding version, you can download it directly or import the dependency)

After downloading, you can import the jar package into the lib directory of the project. Since the blogger uses maven, this one will not be displayed.

First, we create a database with any name. The name I create here is (test)!

Then create a table. The specific code and table structure are as follows:


/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50734
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50734
 File Encoding         : 65001

 Date: 24/09/2021 16:32:12
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for cakeshop
-- ----------------------------
DROP TABLE IF EXISTS `cakeshop`;
CREATE TABLE `cakeshop`  (
  `id` int(11) NOT NULL DEFAULT 0,
  `name` varchar(20) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL DEFAULT NULL,
  `price` double NULL DEFAULT NULL,
  `size` varchar(30) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM CHARACTER SET = gb2312 COLLATE = gb2312_chinese_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of cakeshop
-- ----------------------------
INSERT INTO `cakeshop` VALUES (1, 'Strawberry Cake', 157, 'Tuba');
INSERT INTO `cakeshop` VALUES (2, 'Blueberry cake', 121, 'medium , please');
INSERT INTO `cakeshop` VALUES (3, 'Tiramisu', 154, 'trumpet');
INSERT INTO `cakeshop` VALUES (4, 'Chocolate Almond Cake ', 253, 'Tuba');
INSERT INTO `cakeshop` VALUES (5, 'Schwarzwald cake', 130, 'medium , please');
INSERT INTO `cakeshop` VALUES (6, 'Sand rack cake', 156, 'Tuba');
INSERT INTO `cakeshop` VALUES (7, 'Stollen cake', 214, 'trumpet');
INSERT INTO `cakeshop` VALUES (8, 'Castella Cake', 221, 'Tuba');
INSERT INTO `cakeshop` VALUES (9, 'Saha cake', 234, 'trumpet');
INSERT INTO `cakeshop` VALUES (10, 'Wood cake', 310, 'medium , please');
INSERT INTO `cakeshop` VALUES (11, 'Boston School', 244, 'trumpet');
INSERT INTO `cakeshop` VALUES (12, 'Chiffon Cake', 112, 'trumpet');
INSERT INTO `cakeshop` VALUES (13, 'Swiss volume', 110, 'Tuba');
INSERT INTO `cakeshop` VALUES (14, 'Caramel Macchiato', 241, 'trumpet');
INSERT INTO `cakeshop` VALUES (15, 'Grain turning cake', 173, 'Tuba');
INSERT INTO `cakeshop` VALUES (16, 'Vienna chocolate almond cake', 223, 'trumpet');
INSERT INTO `cakeshop` VALUES (17, 'Cheese Cake', 154, 'Tuba');
INSERT INTO `cakeshop` VALUES (18, 'Chocolate cake', 100, 'medium , please');
INSERT INTO `cakeshop` VALUES (19, 'Cappuccino', 113, 'trumpet');
INSERT INTO `cakeshop` VALUES (20, 'Flame stone', 353, 'medium , please');
INSERT INTO `cakeshop` VALUES (21, 'Chocolate snow', 190, 'medium , please');
INSERT INTO `cakeshop` VALUES (22, 'Caramel bowl', 89, 'Tuba');
INSERT INTO `cakeshop` VALUES (23, 'Brownie Brownies', 311, 'Tuba');
INSERT INTO `cakeshop` VALUES (24, 'Red Velvet Cake Red Velvet Cake', 401, 'medium , please');
INSERT INTO `cakeshop` VALUES (25, 'Financier Financier ', 388, 'medium , please');

SET FOREIGN_KEY_CHECKS = 1;

Injection of c3p0 configuration information

c3p0 information about the database can be injected through the configuration file or the method.

  • First, inject the presentation configuration file:

Create a c3p0-config.xml (must be this name) under resource, and its content is

<c3p0-config>
  <!-- Read the connection pool object using the default configuration -->
  <default-config>
  	<!--  Connection parameters -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://47.93.197.238:3306/test</property>
    <property name="user">root</property>
    <property name="password">root123456</property>
    
    <!-- Connection pool parameters -->
    <!--Number of connections initialized-->
    <property name="initialPoolSize">5</property>
    <!--Maximum number of connections-->
    <property name="maxPoolSize">10</property>
    <!--Connection timeout-->
    <property name="checkoutTimeout">3000</property>
  </default-config>

  <named-config name="otherc3p0"> 
    <!--  Connection parameters -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://47.93.197.238:3306/test</property>
    <property name="user">root</property>
    <property name="password">root123456</property>
    
    <!-- Connection pool parameters -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">8</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>

Keywords: Java Database Maven IDEA c3p0

Added by jkmcgrath on Fri, 24 Sep 2021 11:10:02 +0300