连接池是一种操作,其中系统会预先初始化将来要使用的连接。 这样做是因为在使用时创建连接是一项昂贵的操作。 在本文中,我们将学习如何在Spring JDBC中创建C3P0连接池(某人未使用休眠)。
Pom.xml
<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version>
</dependency>
Spring上下文文件(applicaitonContext-persistance.xml)
现在,我们需要为spring准备一个JDBC上下文文件。 我们需要使用所有凭据为数据库定义一个数据源。
<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- Employee DB data source. --><bean id="employeeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.employee_db_url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxPoolSize" value="${jdbc.maxPoolSize}" /><property name="minPoolSize" value="${jdbc.minPoolSize}" /><property name="maxStatements" value="${jdbc.maxStatements}" /><property name="testConnectionOnCheckout" value="${jdbc.testConnection}" /></bean><context:component-scan base-package="com.javapitshop.dao"></context:component-scan>
</beans>
在上面的示例中,我们为Employee DB创建了一个C3P0数据源,其中包含所有凭据和适当的参数。 上下文文件中未提及所有凭据和设置。 我一直在使用专用的属性文件。 现在,可以在任何DAO类中自动将此bean作为DataSource对象连接。
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.employee_db_url=jdbc:mysql://localhost:3306/employee
jdbc.username=root
jdbc.password=root
jdbc.maxPoolSize=50
jdbc.minPoolSize=10
jdbc.maxStatements=100
jdbc.testConnection=true
BaseDao类
DAO基础类将定义我们需要在所有子类中使用的任何抽象方法或任何常用功能。 我们可以根据需要将其抽象化或根据需要添加任何内容。 另请注意,我已经重载了其构造函数以实现Logging。 现在,每个子类都需要提供其类定义。
package com.icsmobile.faadplatform.dao;import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;/*** Its the parent Dao class of the all the daos defined in the System.** @author JavaPitShop*/
public class BaseDao extends SimpleJdbcDaoSupport {//common logger for all the classes in the dao layerprotected Logger logger;/*** * @param daoClass*/public BaseDao(Class<?> daoClass) {logger = Logger.getLogger(daoClass);}}
EmployeeJdbcDao.Java
EmployeeJdbcDao正在扩展BaseDao,并在其构造函数中自动装配了我们在上下文Bean中定义的“ employeeDataSource”。
@Repository
public class EmployeeJdbcDAO extends BaseDao {/*** Instantiates a new employee jdbc dao.** @param userDataSource the employee data source*/@Autowiredpublic ApplicationJdbcDAO(DataSource employeeDataSource) {super(ApplicationJdbcDAO.class);this.setDataSource(userDataSource);}public EmployeeBO getEmployeeById(final int employeeId) {logger.debug("getEmployeeById(" + employeeId + ")");EmployeeBO employeeBO = null;StringBuilder queryString = new StringBuilder();queryString.append(" SELECT ").append( "*" ) .append(" FROM employee ").append(" WHERE employee_id = ? ");Object[] parameterList = { employeeId };logger.debug(queryString.toString());// execute querySqlRowSet dataRow = getJdbcTemplate().queryForRowSet(queryString.toString(), parameterList);if (dataRow.next()) {// create application objectemployeeBO = getEmployeeBusinessObjectFromRowSet(dataRow);}return employeeBO;}
}
翻译自: https://www.javacodegeeks.com/2014/05/adding-c3po-connection-pooling-in-spring-jdbc.html