<Context?path= " /JNDIDemo " ?docBase= " D:\workspace\JNDIDemo\WebRoot " ?debug= " 0 " ?reloadable= " true " ?crossContext= " true " >?
<Logger?className= " org.apache.catalina.logger.FileLogger " ?prefix= " localhost_quality_log. " ?suffix= " .txt " ?timestamp= " true " />?
<Resource?
name= " jdbc/test " ?<!--?JNDI數(shù)據(jù)池名稱(chēng)?-->?
type= " javax.sql.DataSource " ?<!--?數(shù)據(jù)類(lèi)?-->?
password= " karid " ?<!--?密碼?-->?
driverClassName= " oracle.jdbc.driver.OracleDriver " ?<!--?驅(qū)動(dòng)?-->?
maxIdle= " 2 " ?<!--?最少可用lia?-->?
maxWait= " 5000 " ?<!--?最大等待時(shí)間?5秒?-->?
username= " karid " ?<!--?用戶(hù)名?-->?
url= " jdbc:oracle:thin:@127.0.0.1:1521:karid " ?
maxActive= " 4 " ?<!--?最大可用連接?-->?/>?
<ResourceParams?name= " jdbc/test " >?
<parameter>?
<name>removeAbandoned</name>?
<!--?Abandoned?DB?connections?are?removed?and?recycled?-->?
<value> true </value>?
</parameter>?
<parameter>?
<name>removeAbandonedTimeout</name>?
<!--?Use?the?removeAbandonedTimeout?parameter?to? set ?the?number?of?seconds?a?DB?connection?has?been?idle?before?it? is ?considered?abandoned.?-->?
<value> 60 </value>?
</parameter>?
<parameter>?
<name>logAbandoned</name>?
<!--?Log?a?stack?trace?of?the?code?which?abandoned?-->?
<value> false </value>?
</parameter>?
<parameter>?
<name>factory</name>?
<!--DBCP?Basic?Datasource?Factory?-->?
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>?
</parameter>?
</ResourceParams>?
2 、配置web.xml
<description>MySQL?Test?App</description>?
<resource- ref >?
<description>DB?Connection</description>?
<res- ref -name>jdbc/test</res- ref -name>?
<res-type>javax.sql.DataSource</res-type>?
<res-auth>Container</res-auth>?
</resource- ref >?
3 、JNDI使用
public ? class ?DataSourceFactory?
{?
private ? static ?DataSource?ds;?
public ? static ?DataSource?createDataSourde()?
{?
if ?(ds?==? null )?
{?
try ?
{?
Context?initContext?=? new ?InitialContext();?
if ?(initContext?==? null )?
System. out .println( " 無(wú)配置環(huán)境 " );?
Context?envContext?=?(Context)?initContext.lookup( " java:/compenv " );?
ds?=?(DataSource)?envContext.lookup( " jdbc/test " );? // 根據(jù)名稱(chēng)取得數(shù)據(jù)源?
}?
catch ?(NamingException?e)?
{?
e.printStackTrace();?
}?
}?
return ?ds;?
}?
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
