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 >

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值