In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to configure the use of HikariCP data source". In the daily operation, I believe many people have doubts about how to configure the use of HikariCP data source. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to configure the use of HikariCP data source". Next, please follow the editor to study!
Working with scen
HikariCP is claimed to be the fastest data source. In fact, I think the performance must still be in SQL statements and databases. And there are not many functions of this data source.
Individuals do not like to use it as the main data source.
Comparison of druid data sources
However, if you use HikariCP as a read read third-party database, that is, multiple data sources, I think it is very appropriate.
Multi-data source configuration package com.door.remote.dataSource;import cn.hutool.cache.CacheUtil;import cn.hutool.cache.impl.TimedCache;import cn.hutool.db.Db;import com.door.common.constants.DbConst;import com.door.common.constants.biz.dr.DbSetType;import com.door.entity.dr.AcDrDbSource;import com.door.utils.db.CfsDatabase;import com.zaxxer.hikari.HikariDataSource;import lombok.extern.log4j.Log4j2;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Component Import javax.annotation.PostConstruct;import javax.sql.DataSource;import java.sql.SQLException;/** * data Source Manager * * @ author oumin * @ date on 2021-1-23 10:01 * / @ Component@Log4j2public class DataSourceManager {/ / 24 hour timeout private static final long timeOut = 24 * 60 * 60 * 1000L; private final Object createLock = new Object (); private static final TimedCache timedDataSourceCache = CacheUtil.newTimedCache (timeOut) The @ PostConstruct / / initialization method is annotated in the same way as init-method=init public void init () {/ / starts a scheduled task to check the expired timedDataSourceCache.schedulePrune (timeOut) every xx millisecond;} / * * get the data source * * @ return * / public DataSource getAndMaybePut (CfsDatabase cfsDatabase) {String key = cfsDatabase.toString (); DataSource curr = timedDataSourceCache.get (key) If (curr = = null) {synchronized (this.createLock) {curr = timedDataSourceCache.get (key); if (curr = = null) {timedDataSourceCache.put (key, this.createDataSource (cfsDatabase));} return timedDataSourceCache.get (key);} else {return curr;}} private DataSource createDataSource (CfsDatabase cfsDatabase) {HikariDataSource dataSource = new HikariDataSource () DataSource.setDriverClassName (DbConst.getDbDriverByDbType (cfsDatabase.getDbType ()); if (DbSetType.DB_MYSQL.getName (). EqualsIgnoreCase (cfsDatabase.getDbType () {/ / compatible MYSQL8 time zone, and specified connection timeout connectTimeout=20000, and do not use SSL: characterEncoding=utf- 8&useSSL=false&serverTimezone=Hongkong String timeZone = cfsDatabase.getTimeZone (); String url = cfsDatabase.getUrl (); String urlDb = "" If (StringUtils.isNoneBlank (timeZone)) {urlDb = DbConst.MYSQL_URL + url + DbConst.MYSQL_STR + timeZone + DbConst.MYSQL_TIME_OUT;} else {/ / if timeZone is empty, mysql uses the default time zone urlDb = DbConst.MYSQL_URL + url + DbConst.MYSQL_STR_ZOOR + DbConst.MYSQL_TIME_OUT;} dataSource.setJdbcUrl (urlDb) } else {dataSource.setJdbcUrl (cfsDatabase.getUrl ());} dataSource.setUsername (cfsDatabase.getUserName ()); dataSource.setPassword (cfsDatabase.getPassword ()); / / dataSource.setPassword (PasswordKey.getDecodePassword (source.getDatasourcePassword (); dataSource.setMaximumPoolSize (10); dataSource.setMinimumIdle (1); / / default 30s / / dataSource.setConnectionTimeout (source.getConnTimeout ()) / / repair connection timeout exception / / default false. At present, the business is only query / / dataSource.setReadOnly (true); read-only mode, if the database supports read-only mode, there is no dataSource.setIdleTimeout (60 * 1000L) whose units are less than milliseconds; dataSource.setValidationTimeout (3 * 1000L); dataSource.setMaxLifetime (300 * 1000L) / / mysql and Oracle use SELECT 1 FROM DUAL, while other databases use select 1 / / although the official website does not recommend configuring this TestQuery but not if the database supports the JDBC4 specification. But to avoid trouble, dataSource.setConnectionTestQuery (DbConst.getTestSQL (cfsDatabase.getDbType (); dataSource.setPoolName ("data Source ID" + cfsDatabase.getSourceId ()); return dataSource;} / * * Test SQL * * @ param sql Test SQL * @ param dbSource Test data Source * / public void testSQL (String sql, AcDrDbSource dbSource) throws SQLException {log.info ("Test SQL: {}", sql) CfsDatabase database = CfsDatabase.converterByDataSource (dbSource); DataSource dataSource = this.getAndMaybePut (database); Db.use (dataSource) .query (sql);} at this point, the study on "how to configure the use of HikariCP data sources" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.