Tomcat连接池配置及spring引用方式

来源:互联网 发布:java比较三个数的大小 编辑:程序博客网 时间:2024/04/30 11:28
  • Tomcat连接池的配置方式很多,在网上搜索各种版本不一,这里介绍一下我的个人配置经验,希望对你有用。

    我这里以tomcat6 和 tomcat 7 为例(低版本可能有些许差别),使用oracle10g数据库(其他库类似),在spring中引用。

    1 准备工作
    配置之前,先将oracle10g的数据库驱动包ojdbc14.jar放到tomcat目录下的lib文件夹中

    2 在Tomcat中配置连接池
    Tomcat连接池配置的方式很多,这里介绍两种。

    2.1在context.xml中配置连接池
    在tomcat目录下的conf文件夹中,修改context.xml文件,在context标签之间添加Resource标签如下

     

     

     
    <Context> 
        <!-- Default set of monitored resources --> 
    <WatchedResource>WEB-INF/web.xml</WatchedResource>   
        <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> 
        <Resource name="jdbc/oracleTest" 
                        auth="Container" 
                        type="javax.sql.DataSource" 
                        driverClassName="oracle.jdbc.driver.OracleDriver" 
                        url="jdbc:oracle:thin:@ip:1521:oral" 
                        username="zhangsan" 
                        password="sdfsdf" 
                        maxActive="100" 
                        maxIdle="30" 
                        maxWait="10000"/> 
     </Context> 

    <Context>
        <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
     <Resource name="jdbc/oracleTest"
         auth="Container"
         type="javax.sql.DataSource"
         driverClassName="oracle.jdbc.driver.OracleDriver"
         url="jdbc:oracle:thin:@ip:1521:oral"
         username="zhangsan"
         password="sdfsdf"
         maxActive="100"
         maxIdle="30"
         maxWait="10000"/>
     </Context>
     

    2.2在server.xml中配置连接池
    如果你在conf文件夹中的server.xml文件里像如下这样配置了全局context:


     
    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" 
        xmlValidation="false" xmlNamespaceAware="false"> 
        <Context docBase="E:\projects\test\WebRoot"   
    path="" debug="0" crossContext="true" > 
        </Context>              
    </Host> 

    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"
     xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="E:\projects\test\WebRoot"
    path="" debug="0" crossContext="true" >
     </Context>   
    </Host>

    也可以将2.1中的Resource标签添加到这里的context下,变成下边这样

     
    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" 
        xmlValidation="false" xmlNamespaceAware="false"> 
        <Context docBase="E:\projects\test\WebRoot"   
    path="" debug="0" crossContext="true" > 
            <Resource name="jdbc/oracleTest " 
                auth="Container" 
                type="javax.sql.DataSource" 
                    driverClassName="oracle.jdbc.driver.OracleDriver" 
                    url="jdbc:oracle:thin:@ip:1521:oral" 
                    username="zhangsan" 
                    password="sdfsdf" 
                maxActive="100" 
                maxIdle="30" 
                maxWait="10000" 
            /> 
        </Context>              
    </Host> 

    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"
     xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="E:\projects\test\WebRoot"
    path="" debug="0" crossContext="true" >
      <Resource name="jdbc/oracleTest "
       auth="Container"
       type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@ip:1521:oral"
        username="zhangsan"
        password="sdfsdf"
       maxActive="100"
       maxIdle="30"
       maxWait="10000"
      />
     </Context>   
    </Host>
     

    3 Spring中引用数据源
    引用方式也很简单,只需要将spring配置文件中关于数据源配置的标签替换成下边这样既可


    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">   
            <property name="jndiName">   
                <value>java:comp/env/xxx/xxxxxx</value>              
            </property>   
        </bean> 

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                <value>java:comp/env/xxx/xxxxxx</value>           
            </property>
        </bean>
     

    其中/xxx/xxxxx 部分与我们在上边定义的Resource名字对应即可

0 0
原创粉丝点击