首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

在spring配备jdbc-pool连接池

2012-08-28 
在spring配置jdbc-pool连接池看了一篇文章,讲jdbc-pool比c3p0、dbcp效率高,但没有找到它在spring中的配置方

在spring配置jdbc-pool连接池
    看了一篇文章,讲jdbc-pool比c3p0、dbcp效率高,但没有找到它在spring中的配置方式,所以自己尝试了一下,成功了。

<?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:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!--数据源配置-->           <bean id="dataSource" destroy-method="close">        <property name="poolProperties">            <bean value="jdbc:sqlserver://172.168.16.11:1433;databaseName=xgerp"/>                <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>                <property name="username" value="xgerp_r"/>                <property name="password" value="xgerp"/>                <property name="jmxEnabled" value="true"/>                <property name="testWhileIdle" value="false"/>                <property name="testOnBorrow" value="true"/>                <property name="validationInterval" value="30000"/>                <property name="testOnReturn" value="false"/>                <property name="validationQuery" value="select 1"/>                <property name="timeBetweenEvictionRunsMillis" value="30000"/>                <property name="maxActive" value="100"/>                <property name="initialSize" value="10"/>                <property name="maxWait" value="10000"/>                <property name="removeAbandonedTimeout" value="60"/>                <property name="minEvictableIdleTimeMillis" value="30000"/>                <property name="minIdle" value="10"/>                <property name="logAbandoned" value="true"/>                <property name="removeAbandoned" value="true"/>                <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>            </bean>        </property>    </bean>

测试代码如下:
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger;import javax.sql.DataSource;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;/** * 简单测试jdbc-pool在spring中的配置 * @author Administrator */public class JdbcPoolTest {    @Test    public void testPool() {        ApplicationContext context = new FileSystemXmlApplicationContext("file:G:/vss/xuguang/web/WEB-INF/applicationContext.xml");        DataSource ds = (DataSource)context.getBean("dataSource");        Connection con = null;         try {            con = ds.getConnection();            Statement st  = con.createStatement();            ResultSet rs = st.executeQuery("select top 10 personName from comm_person");            while (rs.next()) {                System.out.println("personName: " + rs.getString("personName"));            }            rs.close();            st.close();        } catch (SQLException ex) {            Logger.getLogger(JdbcPoolTest.class.getName()).log(Level.SEVERE, null, ex);        } finally {            if (con != null) try {con.close();} catch (Exception ignore){}        }    }}

热点排行