当前位置:网站首页>Customize MySQL connection pool

Customize MySQL connection pool

2022-06-28 12:51:00 51CTO

Recently, I learned the general pooling framework commons-pool2 After practice , Again HTTP The performance test has been carried out , The results were unexpected , It is of no use to improve performance . After my own local test , The performance is good enough .

Then I thought it over , I used the wrong place . Originally I wanted to write one by myself Redis Of the connection pool ,jedis The connection pool itself is commons-pool2 Developed , I was a little surprised , Seems to think the same .commons-pool2 It is very good for connection pooling .

I looked carefully for , It is found that there is still a lack of a local MySQL Connection pool , instead of springboot Then you need to start a service . Of course, there should be , But I really want to write one myself and then do all kinds of tests , So I didn't look for it carefully .

Poolable object

First , We need a poolable object , I chose com.funtester.db.mysql.FunMySql, This is a single link that I wrote myself MySQL object . I plan to use this as a base for poolable objects .

package com.funtester.db.mysql;

import com.funtester.base.interfaces.IMySqlBasic;
import com.funtester.config.SqlConstant;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/** * mysql Basic classes of operations  * <p> For storing data , More for reptiles </p> */
public class FunMySql extends SqlBase implements IMySqlBasic {

    /** * {@link SqlConstant#FUN_SQL_URL} Will replace IP To URL */
    String url;

    /** *  library  */
    String database;

    /** *  user  */
    String user;

    /** *  password  */
    String password;

    Connection connection;

    Statement statement;

    /** *  Private constructor  * * @param url  Connection address , Including ports  * @param database  library  * @param user  user name  * @param password  password  */
    public FunMySql(String url, String database, String user, String password) {
        this.url = url;
        this.database = database;
        this.user = user;
        this.password = password;
        getConnection(database);
    }

    /** *  Initialize connection  */
    @Override
    public void getConnection() {
        getConnection(EMPTY);
    }

    /** *  perform sql sentence , Not query sentence , Do not close the connection  * * @param sql */
    @Override
    public void executeUpdateSql(String sql) {
        SqlBase.executeUpdateSql(connection, statement, sql);
    }

    /** *  Query function  * * @param sql * @return */
    @Override
    public ResultSet executeQuerySql(String sql) {
        return SqlBase.executeQuerySql(connection, statement, sql);
    }

    /** *  close query Connect  */
    @Override
    public void over() {
        SqlBase.close(connection, statement);
    }

    @Override
    public void getConnection(String database) {
        if (connection == null)
            connection = SqlBase.getConnection(SqlConstant.FUN_SQL_URL.replace("ip", url).replace("database", database), user, password);
        if (statement == null) statement = SqlBase.getStatement(connection);
    }

}


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.

Pool chemical plant

Relative connection , establish com.funtester.db.mysql.FunMySql When , By the way, initialize MySQL Connect . And then again com.funtester.db.mysql.MysqlPool.FunTester#destroyObject The connection is recycled .

    /** *  Pool chemical plant  */
    private class FunTester extends BasePooledObjectFactory<FunMySql> {

        @Override
        FunMySql create() throws Exception {
            return new FunMySql(url, database, user, password)
        }

        @Override
        PooledObject<FunMySql> wrap(FunMySql obj) {
            return new DefaultPooledObject<FunMySql>(obj)
        }

        @Override
        void destroyObject(PooledObject<FunMySql> p) throws Exception {
            p.getObject().over()
            super.destroyObject(p)
        }
    }

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

Object pool

There is some redundancy here , In the process of later use , I will continue to optimize . By creating a com.funtester.db.mysql.MysqlPool object , Get one com.funtester.db.mysql.FunMySql Object pool .

/** *  Customize MySQL Connect pool objects  */
class MysqlPool extends PoolConstant {

    private static final Logger logger = LogManager.getLogger(MysqlPool.class);

    /** * {@link com.funtester.config.SqlConstant#FUN_SQL_URL} Will replace IP To URL*/
    String url;

    /** *  library  **/
    String database;

    /** *  user  **/
    String user;

    /** *  password  **/
    String password;

    private GenericObjectPool<FunMySql> pool

    MysqlPool(String url, String database, String user, String password) {
        this.url = url
        this.database = database
        this.user = user
        this.password = password
        init()
    }

    /** *  Initialize connection pool  * @return */
    def init() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(MAX);
        poolConfig.setMinIdle(MIN_IDLE);
        poolConfig.setMaxIdle(MAX_IDLE);
        poolConfig.setMaxWaitMillis(MAX_WAIT_TIME);
        poolConfig.setMinEvictableIdleTimeMillis(MAX_IDLE_TIME);
        pool = new GenericObjectPool<FunMySql>(new FunTester(), poolConfig);
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

API encapsulation

Since learning Go Linguistic gorm The framework and Redis frame , I found that there is no need to expose the relevant information about pooling , Directly encapsulate the original API, Exposed to users for use , In this way, users do not have to worry about the recycling of connections .


    /** *  Lending to  * @return */
    def borrow() {
        try {
            return pool.borrowObject()
        } catch (e) {
            logger.warn(" obtain ${JSONObject.class}  Failure ", e)
        } finally {
            new JSONObject()
        }
    }

    /** *  Return the object  * @param funMySql * @return */
    def back(FunMySql funMySql) {
        pool.returnObject(funMySql)
    }

    /** *  perform update SQL * @param sql * @return */
    def execute(def sql) {
        def driver = borrow()
        try {
            driver.executeUpdateSql(sql)
        } catch (e) {
            logger.warn(" perform :{} Failure ", sql)
        } finally {
            back(driver)
        }
    }

    /** *  Execute the query SQL * @param sql * @return */
    def query(def sql) {
        def driver = borrow()
        try {
            return driver.executeQuerySql(sql)
        } catch (e) {
            logger.warn(" perform :{} Failure ", sql)
        } finally {
            back(driver)
        }
    }


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

BUG excavating machinery · Performance conqueror · Head pot cover

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/179/202206281233043324.html