Spring configures druid database connection pool

druid: Ali produced, Taobao and Alipay dedicated database connection pool, but it is not only a database connection pool, it also contains a ProxyDriver, a series of built-in JDBC component library, a SQL Parser. Supports all JDBC-compliant databases, including Oracle, MySql, Derby, Postgresql, SQL Server, H2, and more.
Druid has been specially optimized for Oracle and MySql, such as Oracle's PS Cache memory usage optimization and MySql's ping detection optimization.
Druid provides complete support for MySQL, Oracle, Postgresql, and SQL-92 SQL. This is a handwritten high-performance SQL Parser that supports the Visitor mode, making it very convenient to analyze the abstract syntax tree of SQL.
Simple SQL statements take less than 10 microseconds, and complex SQL statements take 30 microseconds.

The SQL Parser provided by Druid can intercept SQL at the JDBC layer for corresponding processing, such as sub-database sub-table, auditing, etc. Druid's WallFilter to defend against SQL injection attacks is implemented through Druid's SQL Parser analysis semantics.

jar rack package:

druid-1.0.2.jar

Spring configuration file:

<!-- read properties file -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<!--Multiple resource files can be configured-->
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- datasource-->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
	   <!-- driver name -->
	    <property name="driverClassName" value="${driver}" />
	    <!-- JDBC connection string -->
	    <property name="url" value="${url}" />
	    <!-- database username-->
	    <property name="username" value="${username}" />
	     <!-- Controls whether the password is encrypted -->
       <!--  <property name="connectionProperties" value="config.decrypt=true" /> -->
	    <!-- database user password-->
	    <property name="password" value="${pwd}"/>
	    <!-- Configure filters for monitoring statistics interception -->
	    <property name="filters" value="stat" />
	   <!-- Maximum connection pool value-->
	    <property name="maxActive" value="20" />
	    <!-- Initialize size-->
	    <property name="initialSize" value="1" />
	    <!-- Get the maximum wait time for connection-->
	    <property name="maxWait" value="60000" />
	    <!-- Connection pool minimum idle-->
	    <property name="minIdle" value="1" />
	    <!-- Evicted connection detection interval -->
	    <property name="timeBetweenEvictionRunsMillis" value="3000" />
	    <!-- Minimum eviction time-->
	    <property name="minEvictableIdleTimeMillis" value="300000" />
	    <!-- SQL to check if the connection is valid -->
	    <property name="validationQuery" value="SELECT 1" />
	    <!-- Whether to test when idle connection -->
	    <property name="testWhileIdle" value="true" />
	    <!-- Whether to test when lending connections -->
	    <property name="testOnBorrow" value="false" />
	    <!-- Whether to test when returning the connection -->
	    <property name="testOnReturn" value="false" />
	    <!-- Whether to test when lending connections -->
	    <property name="poolPreparedStatements" value="true" />
	     <!-- Statement cache size-->
	    <property name="maxPoolPreparedStatementPerConnectionSize" value="10" />
     </bean>

Properties resource file: jdbc.properties

username=root
url=jdbc:mysql://localhost:3306/qw?characterEncoding=utf8
driver=com.mysql.jdbc.Driver
pwd=123456789

You can also configure more properties here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324640213&siteId=291194637