spring configuration dataSource: dbcp database connection pool

    Use org.apache.commons.dbcp.BasicDataSource to configure spring database connections, using database connection pooling technology.

    The project section references the maven configuration of the jar

    <!-- dbcp -->
    <dependency>
    	<groupId>commons-pool</groupId>
    	<artifactId>commons-pool</artifactId>
    	<version>1.6</version>
	</dependency>
    <dependency>
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>1.4</version>
	</dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>4.0.0.RELEASE</version>
	</dependency>
    <dependency>
   		<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.38</version>
	</dependency>

1. Create a new simple spring project to test and configure the dbcp connection pool. Its directory structure is as follows:

Project directory description:

dbcpTestfileBeans.xml: Spring application context.

jdbc.properties: dbcp database connection pool configuration file.

UserDao.java: DAO layer bean.

DbcpMain: Test class.

Since it is just built for testing by myself, the file name naming is very irregular, and there is no directory to speak of.

The database tables and table data for testing are as follows:

2. The jar package that needs to be referenced.

    <!-- dbcp -->
    <dependency>
    	<groupId>commons-pool</groupId>
    	<artifactId>commons-pool</artifactId>
    	<version>1.6</version>
	</dependency>
    <dependency>
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>1.4</version>
	</dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>4.0.0.RELEASE</version>
	</dependency>
    <dependency>
   		<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.38</version>
	</dependency>

3. Contents of the project file.

jdbc.properties:

#加载驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
#数据库
jdbc.url=jdbc:mysql://172.21.4.45:3307/cdspdb?useUnicode=true&amp;characterEncoding=utf-8
#数据库用户名
jdbc.username=cdsp
#数据库密码
jdbc.password=cdsp
#初始化连接数
jdbc.initialSize=3
#最大连接数,非正数为不限制
jdbc.maxActive=1000
#最大空闲连接数,负数为不限制
jdbc.maxIdle=5
#最小空闲连接数,0为不保留空闲连接
jdbc.minIdle=1
#获取连接最大等待时间 ,单位毫秒
jdbc.maxWait=1000
#数据库连接验证语句
jdbc.validationQuery=select 1
#空闲连接检验回收器设置
jdbc.testWhileIdle=true
#空闲连接回收器线程运行周期,单位为毫秒,值大小以数据库为准,非正数则不运行空闲连接回收器线程 
jdbc.timeBetweenEvictionRunsMillis=300000

dbcpTestfileBeans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- jdbc配置文件 -->
	<context:property-placeholder location="com/springtest/dbcp/jdbc.properties" />
	<!-- 配置dbcp数据源连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
		p:driverClassName="${jdbc.driverClassName}"
		p:url="${jdbc.url}"
		p:username="${jdbc.username}"
		p:password="${jdbc.password}"
		p:initialSize="${jdbc.initialSize}"
		p:maxActive="${jdbc.maxActive}"
		p:maxIdle="${jdbc.maxIdle}"
		p:minIdle="${jdbc.minIdle}"
		p:maxWait="${jdbc.maxWait}"
		p:validationQuery="${jdbc.validationQuery}"
		p:testWhileIdle="${jdbc.testWhileIdle}"
		p:timeBetweenEvictionRunsMillis="${jdbc.timeBetweenEvictionRunsMillis}"/>
		
	<!-- 引入spingjdbc -->
	<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置da层bean -->
	<bean id="UserDao" class="com.springtest.dbcp.UserDao">
		<property name="jdbcTemplate" ref="JdbcTemplate"></property>
	</bean>
</beans>

UserDao.java:

package com.springtest.dbcp;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {
	
	JdbcTemplate jdbcTemplate;
	
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
        this.jdbcTemplate = jdbcTemplate;  
    } 
	public List<Map<String,Object>> queryByName(String name)
	{
		String sql = "select * from thnb_user where user_name='" + name + "' ";
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
}

DbcpMain:

package com.springtest.dbcp;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DbcpMain {

	public static void main(String[] args) {

//		String contextXmlPath = "com/springtest/dbcp/DbcpTestbeans.xml";
		//spring应用上下文
		String contextXmlPath2 = "com/springtest/dbcp/dbcpTestfileBeans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(contextXmlPath2);
		UserDao userDao = (UserDao) ctx.getBean("UserDao");
		List<Map<String,Object>> list = userDao.queryByName("thb");
		System.out.println(list);
	}

}

4. Test verification

    Execute DbcpMain and print as follows:

In addition to printing out the query results, the above lines are warnings when the connection pool establishes an initial link. Configuration is fine.

5. Configure the dbcp database connection pool in the project.

    If configured in the project, you can directly put jdbc.properties in the project directory, refer to the spring application context, and then configure the required parameters. Configure the dataSource in the test project dbcpTestfileBeans.xml to the springcontext of the project.

Guess you like

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