Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure data connection Pool in druid

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to configure data connection pool in druid. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Springboot project pom.xml druid part com.alibaba druid-spring-boot-starter 1.1.21 application.xml druid part spring: # Integration database layer datasource: driver-class-name: com.mysql.cj.jdbc.Driver name: demo url: jdbc:mysql: / / 127.0.0.1:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource druid: # initial number of connections initialSize: 5 # minimum number of connection pools minIdle: 10 # maximum number of connection pools maxActive: 20 # configuration to obtain connection wait timeout MaxWait: 60000 # configure how often the test is performed. Detect idle connections that need to be closed in milliseconds timeBetweenEvictionRunsMillis: 60000 # configure the minimum survival time of a connection in the pool, in milliseconds minEvictableIdleTimeMillis: 300000 # configure the maximum survival time of a connection in the pool Unit: millisecond maxEvictableIdleTimeMillis: 900000 # configure to detect whether the connection is valid validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false webStatFilter: enabled: true statViewServlet: enabled: true # set whitelist If left empty, all access to allow: url-pattern: / druid/* # console management username and password login-username: easy login-password: 1 filter: stat: enabled: true # slow SQL record log-slow-sql: true slow-sql-millis: 20000 merge- Sql: true wall: config: multi-statement-allow: trueDruidDataSourceDecorator.java reads yaml configuration Modify DataSource@Configuration@ConfigurationProperties (prefix = "spring.datasource.druid") public class DruidDataSourceDecorator {/ / read Druid configuration / / initial number of connections private int initialSize / / minimum number of connection pools private int minIdle; / / maximum number of connection pools private int maxActive; / / configure the time to get the connection wait for timeout private int maxWait; / / configure how often does it take to detect idle connections that need to be closed, in millisecond private int timeBetweenEvictionRunsMillis / / configure the minimum survival time of a connection in the pool, in milliseconds private int minEvictableIdleTimeMillis; / / configure the maximum survival time of a connection in the pool, in milliseconds private int maxEvictableIdleTimeMillis; / / configure to detect whether the connection is valid private String validationQuery; / / private boolean testWhileIdle; / / private boolean testOnBorrow / / private boolean testOnReturn; omits set, get... Public DruidDataSource decorat (DruidDataSource source) {/ * * configure initialization size, minimum, maximum * / source.setInitialSize (initialSize); source.setMaxActive (maxActive); source.setMinIdle (minIdle); / * * configure the time to get connection wait timeout * / source.setMaxWait (maxWait) / * * configure how often to detect idle connections that need to be closed in milliseconds * / source.setTimeBetweenEvictionRunsMillis (timeBetweenEvictionRunsMillis); / * * configure the minimum and maximum survival time of a connection in the pool, in milliseconds * / source.setMinEvictableIdleTimeMillis (minEvictableIdleTimeMillis); source.setMaxEvictableIdleTimeMillis (maxEvictableIdleTimeMillis) The sql used to check whether the connection is valid is required to be a query statement, which is commonly used as select *'x'. If validationQuery is null,testOnBorrow, testOnReturn, testWhileIdle will not work. * / source.setValidationQuery (validationQuery); / * it is recommended to configure it as true, which does not affect performance and ensures security. When applying for a connection, it is tested that if the idle time is greater than timeBetweenEvictionRunsMillis, validationQuery is performed to check whether the connection is valid. * / source.setTestWhileIdle (testWhileIdle); / * * perform validationQuery to check whether the connection is valid when applying for a connection. This configuration will degrade the performance. * / source.setTestOnBorrow (testOnBorrow); / * * perform validationQuery to check whether the connection is valid when returning the connection. This configuration will degrade the performance. * / source.setTestOnReturn (testOnReturn); return source;}} DataSourceConfig.java data source configuration @ Configurationpublic class DataSourceConfig {/ / embellished DataSource @ Autowired private DruidDataSourceDecorator decorator @ Bean @ Primary @ ConfigurationProperties (prefix = "spring.datasource") DataSource dataSource () {DruidDataSource dataSource = DruidDataSourceBuilder.create () .build (); return decorator.decorat (dataSource) }} mybaties configuration @ Configuration@MapperScan (basePackages = "com.xxx.mapper", sqlSessionTemplateRef = "sqlSessionTemplate") public class HySessionFactory {@ Resource (name = "dataSource") DataSource dataSource; SqlSessionFactory hySqlSessionFactory () {SqlSessionFactory sessionFactory = null; try {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource) / / scanned XML bean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath*:mapper/*.xml")); sessionFactory = bean.getObject ();} catch (Exception e) {e.printStackTrace () } return sessionFactory;} @ Bean SqlSessionTemplate sqlSessionTemplate () {return new SqlSessionTemplate (sqlSessionFactory ());}} above is how to configure data connection pool in druid. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report