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 >
< 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 >
<! 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 >
< 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 >
< property name ="jndiName" value ="java:comp/env/jdbc/pgsqldbcp" />
</ bean >