Tomcat 和 Jetty 下 JNDI 配置 DBCP 連接池
系統(tǒng)
1808 0
Tomcat 的配置如下:(在某個App的context.xml中,或某個虛擬主機(jī)的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中。另外把數(shù)據(jù)庫的驅(qū)動包拷貝到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插件開發(fā)應(yīng)用,在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中的配置,在開發(fā)中spring中的dataSource可以按如下形式寫:
????
<
bean?
id
="dataSource"
?class
="org.springframework.jndi.JndiObjectFactoryBean"
>
????????
<
property?
name
="jndiName"
?value
="java:comp/env/jdbc/pgsqldbcp"
/>
????
</
bean
>
?
?
?
?
Tomcat 和 Jetty 下 JNDI 配置 DBCP 連接池
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元