Technical Mybatis a database connection pool configuration (Druid)

Only a brief description, a lot of relevant content online, but here are given reference:

  Database connection pool druid configuration list:

Configuration The default value Explanation
name   Configuring this attribute significance is that if multiple data sources exist to monitor when you can distinguish by area name.
If no, will generate a name, the format is: "DataSource-" + System.identityHashCode (this )
jdbcUrl   Url connection to the database, different databases are not the same. For example:
MySQL: jdbc: MySQL: //192.168.0.1: 3306 / druid2 
the Oracle: jdbc: the Oracle: Thin: @ 192.168.0.1: 1521: ocnauto
username   User name to connect the database
password   Password for the database connection. If you do not want to write code directly in the configuration file, you can use ConfigFilter. The details look here: https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
driverClassName The automatic recognition url This feature can be a worthy, if not based on the configuration druid automatically identify dbType url, then select the appropriate driverClassName
initialSize 0 Establishing the number of physical connection initialization. Initialization occurs when the display call the init method, or the first getConnection
maxActive 8 The maximum number of connection pools
maxIdle 8 It is no longer used, equipped with no effect
minIdle   The minimum number of connection pool
maxWait   Acquires the maximum waiting time is connected, in milliseconds. After configuring maxWait, enabled by default fair locks, concurrent efficiency will decline, if necessary, by using the configuration useUnfairLock property to true unfair lock.
poolPreparedStatements false Whether the cache preparedStatement, which is PSCache. PSCache support database cursors huge performance boost, for example oracle. Under the proposed closure of mysql.
maxOpenPreparedStatements -1 To enable PSCache, must be greater than zero configuration, when greater than 0, poolPreparedStatements triggered automatically changed to true. In the Druid, the PSCache not exist too much memory for an Oracle issue, this value can be configured larger, such as 100
validationQuery   Used to detect whether the connection is valid sql, it requires a query. If validationQuery is null, testOnBorrow, testOnReturn, testWhileIdle will not its role.
testOnBorrow true Execution validationQuery detect whether the connection is valid when applying for connection, made this configuration can degrade performance.
testOnReturn false Returned connection is valid execution validationQuery detect a connection, made this configuration reduces performance
testWhileIdle false Recommended configuration is true, does not affect performance, and ensure safety. Detecting when the application connection, if the idle time is greater than timeBetweenEvictionRunsMillis, performs detection validationQuery connection is valid.
timeBetweenEvictionRunsMillis   Has two meanings:
1) Destroy interval detecting thread connection 2) is determined based testWhileIdle detailed description of the property to see testWhileIdle
numTestsPerEvictionRun   No longer in use, a DruidDataSource only support a EvictionRun
minEvictableIdleTimeMillis    
connectionInitSqls   When a physical connection initialization sql performed
exceptionSorter The automatic recognition dbType When the database to throw some unrecoverable exception, abandoned connection
filters   属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
proxyFilters   类型是List<com.alibaba.druid.filter.Filter>,如果同时配置了filters和proxyFilters,是组合关系,并非替换关系
 

  配置属性说明:

driver:JDBC具体数据库驱动
url:JDBC连接
username:用户名
password:密码
defaultTransactionIsolationLevel:默认事务隔离级别

  下面database.xml的配置:

<!-- 数据库相关配置 -->
<!--引入properties文件中,数据库配置参数--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="writeDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
         <!-- 数据库基本信息配置 -->   <property name="url" value="${jdbc.write.jdbcUrl}" /> <property name="username" value="${jdbc.write.user}" /> <property name="password" value="${jdbc.write.password}" /> <!-- 配置初始化大小、最小、最大 -->
         <!-- 最大并发连接数 -->  <property name="maxActive" value="${jdbc.maxPoolSize}" />
         <!-- 初始化连接数量 -->  <property name="initialSize" value="${jdbc.initialPoolSize}" />
         <!-- 最小空闲连接数 --> <property name="minIdle" value="${jdbc.miniPoolSize}" />
        <!-- 配置获取连接等待超时的时间 ,会降低并发性能-->
        <property name = "maxWait" value ="${jdbc.maxWait}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->  <property name="validationQuery" value="${jdbc.preferredTestQuery}" /> <property name="testWhileIdle" value="true" /> <property name="filters" value="stat" /> </bean> <bean id="readDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.read.jdbcUrl}" /> <property name="username" value="${jdbc.read.user}" /> <property name="password" value="${jdbc.read.password}" /> <property name="maxActive" value="${jdbc.maxPoolSize}" /> <property name="initialSize" value="${jdbc.initialPoolSize}" /> <!-- 配置获取连接等待超时的时间 ,会降低并发性能--> <property name = "maxWait" value ="${jdbc.maxWait}" /> <property name="minIdle" value="${jdbc.miniPoolSize}" /> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${jdbc.preferredTestQuery}" /> <property name="testWhileIdle" value="true" /> <property name="filters" value="stat" /> </bean> <!-- 多数据源配置 --> <bean id="multipleDataSource" class="com.jitri.utils.dataSource.MultipleDataSource"> <property name="defaultTargetDataSource" ref="writeDataSource" /> <property name="targetDataSources"> <map> <entry key="writeDataSource" value-ref="writeDataSource" /> <entry key="readDataSource" value-ref="readDataSource" /> </map> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource" /> <!--PageInterceptor分页拦截器 --> <property name="mapperLocations"> <array> <value>com.jitri.mapper.*.xml</value> </array> </property> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean> <!-- 事务处理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="multipleDataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!--mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并在spring容器中注册 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名,如果扫描多个包,包之间需用半角逗号隔开 --> <property name="basePackage" value="com.jitri.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <import resource="classpath:spring-ehcache.xml" />

  数据库properties配置信息:

##本地环境_allowMultiQueries=true配置用于指定可以执行多条数据库
jdbc.write.jdbcUrl = jdbc:mysql://localhost:3306/jitri?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true
jdbc.write.user = root
jdbc.write.password = 123456

jdbc.read.jdbcUrl = jdbc:mysql://localhost:3306/jitri?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.read.user = root
jdbc.read.password = 123456

jdbc.miniPoolSize = 10
jdbc.maxPoolSize = 30
jdbc.initialPoolSize = 1
#jdbc.acquireIncrement = 1
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis = 60000
jdbc.minEvictableIdleTimeMillis = 300000

jdbc.preferredTestQuery = select * from cc

Guess you like

Origin www.cnblogs.com/jtlgb/p/11459379.html