配置java连接池 tomcat方式、Spring方式配置的比较

一、首先配置连接池
1、tomcat下配置连接池
  *>在tomcat下的context.xml配置连接池代码   
<Resource
      name="jdbc/sqlserver"                  //连接池名称
      type="javax.sql.DataSource" 
      password="syl6413"
         driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
      maxIdle="5"
      maxWait="5000"
      username="sa"
      url="jdbc:sqlserver://192.168.0.9:1433; DatabaseName=disasterSystem"
      maxActive="10"/>
    *>tomcat连接池中配置web.xml代码
<!-- DataSource -->
<resource-ref>
        <description>SQLServer2005 Datasource disasterSystem</description>
         <res-ref-name>jdbc/sqlserver</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>
    *>在Spring配置tomcat连接池的JNDI代码
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName" value="java:comp/env/jdbc/sqlserver">
      </property>
</bean>
    自此tomcat连接池已配置完毕,若不用Spring管理连接池,也可以不配Spring,这样即可以在程序中使用tomcat连接池了.若使用Spring来统一管理连接池,也就是统一SessionFactory,配置方式如下:   
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.SQLServerDialect
                </prop>
                <prop key="hibernate.connection.autocommit">true</prop>
                <!-- 显示sql语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 解决提交乱码问题 -->
                <prop key="connection.useUnicode">true</prop>
                <prop key="connection.characterEncoding">utf-8</prop>
                <!-- 格式化sql语句 -->
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.order_updates">true</prop>
                <!-- 事务自动管理 -->
                <prop key="hibernate.connection.release_mode">
                    after_transaction
                </prop>
            </props>
        </property>

        <property name="mappingResources">
            <list></list>
        </property>
    </bean>
2、tomcat下配置连接池
    Spring不通过用tomcat中配置的JNDI来连接,但是它也是通过用commons.dbcp.jar连接池来管理的,也可以通过其他的开源连接池工具如:C3P0等。配置又分为两种,具体代码如下:
    *>一种是直接配置applicationContext.xml   
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
        </property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:SJY">
        </property>
        <property name="username" value="**"></property>
        <property name="password" value="**"></property>
</bean>
    *>另一种是通过perportise配置文件:datasource.properties再通过applicationContext.xml得到
        datasource.properties代码:       
datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.url=jdbc:sqlserver://192.168.0.9:1433;DatabaseName=disasterSystem
datasource.username=sa
datasource.password=syl6413
datasource.maxActive=10
datasource.maxIdle=5
datasource.maxWait=5000
datasource.defaultAutoCommit=true
        applicationContext.xml代码:            
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
        <value>classpath:/datasource.properties</value>
        </property>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
        <value>${datasource.driverClassName}</value>
        </property>
        <property name="url">
         <value>${datasource.url}</value>
            </property>
            <property name="username">
                 <value>${datasource.username}</value>
            </property>
            <property name="password">
                 <value>${datasource.password}</value>
            </property>
            <property name="maxActive">
                 <value>${datasource.maxActive}</value>
            </property>
            <property name="maxIdle">
                 <value>${datasource.maxIdle}</value>
            </property>
            <property name="maxWait">
        <value>${datasource.maxWait}</value>
        </property>
            <property name="defaultAutoCommit">
               <value>${datasource.defaultAutoCommit}</value>
            </property>
    </bean>

二、测试配置连接池
1、测试tomcat连接池
    *>不用Spring管理的tomcat连接池测试代码(只能在jsp里面测试):           
Context initContext = new InitialContext();
    Context envContext =(Context)initContext.lookup("java:comp/env");
    DataSource ds = (DataSource) envContext.lookup("jdbc/sqlserver");
    Connection con = ds.getConnection();
    if (con != null) {
            System.out.println("已经得到连接");
    } else {
        System.out.println("没有得到连接");
    }
    *>用Spring管理tomcat连接池的测试代码:   
//ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");//两种得到bean工厂的代码,任选其一
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource ds = (DataSource) ctx.getBean("dataSource");
    Connection con = ds.getConnection();
    if(con != null){
        System.out.println("连接成功");
    }else{
        System.out.println("连接不成功");
    }
2.测试Spring连接池
    其实测试Spring连接池的代码和上面Spring管理tomcat连接池测试代码一样:   
//ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");//两种得到bean工厂的代码,任选其一
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource ds = (DataSource) ctx.getBean("dataSource");
    Connection con = ds.getConnection();
    if(con != null){
        System.out.println("连接成功");
    }else{
        System.out.println("连接不成功");
    }

猜你喜欢

转载自licongming163.iteye.com/blog/1707050