The use of flink with druid connection pool

Before bloggers used flink integrated connection pools, there has always been a misunderstanding. It is to reduce the creation of resources by the program. All loading resources or obtaining connections are placed in the open method of flink, although this can reduce the use of resources. It can meet most scenarios, but for example, if the mysql connection is not used for a long time, the connection will be recycled by the mysql database itself.

Let me briefly introduce Druid:
DRUID is a database connection pool implementation on Alibaba's open source platform. It combines the advantages of C3P0, DBCP, PROXOOL and other DB pools, and also adds log monitoring, which can monitor DB pool connections and SQL well. The implementation can be said to be the DB connection pool for monitoring (it is said to be the best connection pool at present, I don't know if the speed is faster than BoneCP).

Configuration parameters
The DataSource class of DRUID is the same as other connection pools: com.alibaba.druid.pool.DruidDataSource. The basic configuration parameters are as follows:

Insert picture description here
Insert picture description here
Insert picture description here
My use:

 @Override
    public void open(Configuration parameters) throws Exception {
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("****");
        dataSource.setUsername("***");
        dataSource.setPassword("****");
        dataSource.setInitialSize(10);   //初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
        dataSource.setMinIdle(10);  //最小连接池数量
        dataSource.setMaxActive(20);  //最大连接池数量
        dataSource.setMaxWait(1000 * 20); //获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
        dataSource.setTimeBetweenEvictionRunsMillis(1000 * 60);  //有两个含义:1) Destroy线程会检测连接的间隔时间2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
        dataSource.setMaxEvictableIdleTimeMillis(1000 * 60 * 60 * 10);  //<!-- 配置一个连接在池中最大生存的时间,单位是毫秒 -->
        dataSource.setMinEvictableIdleTimeMillis(1000 * 60 * 60 * 9);  //<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        dataSource.setTestWhileIdle(true);  // <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(false);
        dataSource.setValidationQuery("select 1");
    }

Create a connection pool in the open method of flink, and get the connection through getConnection when using it. After using it, call the .close method to return the connection to the connection pool.

Guess you like

Origin blog.csdn.net/qq_44962429/article/details/106406954