javascript
spring连接jdbc_在Spring JDBC中添加C3PO连接池
spring連接jdbc
連接池是一種操作,其中系統會預先初始化將來要使用的連接。 這樣做是因為在使用時創建連接是一項昂貴的操作。 在這篇文章中,我們將學習如何在Spring JDBC中創建C3P0連接池(某人沒有使用Hibernate)。
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=trueBaseDao類
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
spring連接jdbc
總結
以上是生活随笔為你收集整理的spring连接jdbc_在Spring JDBC中添加C3PO连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ziplinux解压命令(zip lin
- 下一篇: DDoS流量(ddos流量牵引)