When studying the author's e-commerce architecture, I found that the author used Ali's database connection pool to query our project, but did not use any database connection pool.
package com.puhui.flowplatform.entry.config; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @Configuration @MapperScan(basePackages = "com.puhui.flowplatform.common.dao.nirvanaread", sqlSessionFactoryRef = "nirvanareadSqlSessionFactory", sqlSessionTemplateRef = "nirvanareadSqlSessionTemplate") public class DataSourceNirvanaReadConfig { @Bean(name = "nirvanareadDataSource") @ConfigurationProperties(prefix = "spring.datasource.nirvanaread") public DataSource nirvanareadDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "nirvanareadSqlSessionFactory") public SqlSessionFactory nirvanareadSqlSessionFactory(@Qualifier("nirvanareadDataSource") DataSource dataSource) throws Exception { // NOSONAR SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mappers/nirvanaread/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean(name = "nirvanareadTransactionManager") public DataSourceTransactionManager nirvanareadTransactionManager( @Qualifier("nirvanareadDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "nirvanareadSqlSessionTemplate") public SqlSessionTemplate nirvanareadSqlSessionTemplate( @Qualifier("nirvanareadSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
Author's database connection pool configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- Configure database connection pool --> <!-- Load configuration file --> <context:property-placeholder location="classpath:properties/db.properties" /> <!-- Database connection pool --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- SqlSessionFactory --> <!-- Let spring manage sqlsessionfactory using mybatis and spring integration package --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- Database connection pool --> <property name="dataSource" ref="dataSource" /> <!-- Load the global configuration file of mybatis--> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> <!-- Package scanner for Mapper mapping files --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.taotao.mapper" /> </bean> </beans>
Most of the previous contacts are database connection pools such as c3p0. Today I will learn about druid. Let’s compare it first. Why use druid?
Main function comparison
Druid | BoneCP | DBCP | C3P0 | Proxool | JBoss | Tomcat-Jdbc | |
---|---|---|---|---|---|---|---|
LRU | Yes | no | Yes | no | Yes | Yes | ? |
PSCache | Yes | Yes | Yes | Yes | no | no | Yes |
PSCache-Oracle-Optimized | Yes | no | no | no | no | no | no |
ExceptionSorter | Yes | no | no | no | no | Yes | no |
Update maintenance | Yes | no | no | no | no | ? | Yes |
PSCache
PSCache is a key indicator of database connection pooling. In Oracle, for SQL like SELECT NAME FROM USER WHERE ID = ?, the performance of enabling PSCache and not enabling PSCache may be an order of magnitude different. Proxool is a database connection pool that does not support PSCache. If you use a database that supports cursors such as Oracle, SQL Server, DB2, and Sybase, then you don't need to consider Proxool at all.
PSCache-Oracle-Optimized
For Oracle 10 series Drivers, if PSCache is enabled, it will take up a lot of memory. Special processing must be done, and optimization methods such as enabling the internal EnterImplicitCache can reduce the memory usage. This feature is only available in DruidDataSource. If you are using Oracle Jdbc, you should not hesitate to adopt DruidDataSource.
ExceptionSorter
ExceptionSorter is a very important fault-tolerant feature. If a connection generates an unrecoverable error, it must be removed from the connection pool immediately, otherwise a large number of errors will be generated continuously. This feature is currently only implemented by JBossDataSource and Druid. Druid's implementation is referenced from JBossDataSource, supplemented by long-term production feedback.
database | Support status |
mysql | support, mass use |
oracle | support, mass use |
sqlserver | support |
postgres | support |
db2 | support |
h2 | support |
derby | support |
sqlite | support |
sybase | support |
Druid is a database for monitoring. In addition to the advantages mentioned above, Druid provides powerful monitoring functions:
In addition, druid supports powerful data aggregation and can develop OLAP platforms.