zoukankan      html  css  js  c++  java
  • Tomcat 和 Jetty 下 JNDI 配置 DBCP 连接池

    Tomcat 的配置如下:(在某个App的context.xml中,或某个虚拟主机的ROOT.xml中)

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="">
         
    <Resource name="jdbc/pgsqldbcp" 
                   auth
    ="Container" 
                   type
    ="javax.sql.DataSource" 
                   factory
    ="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
                   driverClassName
    ="org.postgresql.Driver"
                   url
    ="jdbc:postgresql://localhost/xxxxdb"
                   username
    ="postgres" 
                   password
    ="xxxx" 
                   maxActive
    ="20" 
         
    />
    </Context>


    这里使用Tomcat带的tomcat-dbcp.jar包,它包含了 commons-collections.jar, commons-pool.jar, commons-dbcp.jar,如果不使用tomcat-dbcp.jar 则需要将上述3个包拷贝到 Tomcat的lib中。另外把数据库的驱动包拷贝到Tomcat的lib中,就可以了。

    Jetty 下的配置如下:(在jetty-env.xml文件中)

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
         "http://jetty.mortbay.org/configure.dtd"
    >
    <Configure class="org.mortbay.jetty.webapp.WebAppContext">
        
    <New id="pgsqldbcp" class="org.mortbay.jetty.plus.naming.Resource">
            
    <Arg>jdbc/pgsqldbcp</Arg>
            
    <Arg>
                
    <New class="org.apache.commons.dbcp.BasicDataSource">
                    
    <Set name="driverClassName">org.postgresql.Driver</Set>
                    
    <Set name="url">jdbc:postgresql://localhost/xxxxdb</Set>
                    
    <Set name="username">postgres</Set>
                    
    <Set name="password">xxxx</Set>
                    
    <Set name="maxActive">10</Set>
                
    </New>
            
    </Arg>
        
    </New>
    </Configure>


    如果使用maven-jetty-plugin插件开发应用,在pom.xml如下配置:

    <plugin>
        
    <groupId>org.mortbay.jetty</groupId>
        
    <artifactId>maven-jetty-plugin</artifactId>
        
    <version>6.1.7</version>
        
    <configuration>
            
    <webDefaultXml>src/test/resources/webdefault.xml</webDefaultXml>
            
    <jettyEnvXml>src/test/resources/jetty-env.xml</jettyEnvXml>
            
    <contextPath>/exjour</contextPath>
            
    <scanIntervalSeconds>10</scanIntervalSeconds>
            
    <connectors>
                
    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                    
    <port>8000</port>
                    
    <maxIdleTime>60000</maxIdleTime>
                
    </connector>
            
    </connectors>
        
    </configuration>
    </plugin>


    以上,就完成了Tomcat和Jetty下DBCP在JNDI中的配置,在开发中spring中的dataSource可以按如下形式写:

        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            
    <property name="jndiName" value="java:comp/env/jdbc/pgsqldbcp"/>
        
    </bean>
  • 相关阅读:
    Part 7 Joins in sql server
    Part 9 Union and union all in sql server
    Part 4 using entity framework
    Part 3 ViewData and ViewBag in mvc
    Part 2 How are the URL's mapped to Controller Action Methods?
    Part 1 some difference from asp.net to asp.net mvc4
    Part 18 Indexes in sql server
    c/c++保存日志程序模板
    技术只是工具,你不能用它来代替生活
    网络篇:linux下select、poll、epoll之间的区别总结
  • 原文地址:https://www.cnblogs.com/kylindai/p/1342862.html
Copyright © 2011-2022 走看看