JNDI資料來源配置

來源:互聯網
上載者:User

標籤:

在Java Web開發中都要與資料庫打交道,為了不頻繁地開啟和關閉資料庫,以減少資料庫操作負荷,可使資料庫在開發過程中保持開啟狀態,在這裡我們採用配置資料來源的方式(JNDI),而不是傳統地JDBC方式。下面就針對常規型的MySQL5.0.15和Tomcat6.0的資料來源的基本配置進行簡單的介紹:    首先聲明,如果資料來源沒有配置好的話,在開發過程中會拋出諸如下列異常等:    1、org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘‘ for connect URL ‘null‘  
  2、Caused by: java.sql.SQLException: No suitable driver    3、Name jdbc is not bound in this context  
現在開始講下如何配置好資料來源同時也是解決上述異常的辦法:    方案一:  
  步驟一、在Tomcat6.0解壓目錄conf下找到context.xml,在其中的<Context></Context> 中加入如下代碼(要根據自己的情況稍加修改): <Resource  name="jdbc/myznt"   auth="Container"  
 type="javax.sql.DataSource"  username="root"   password="localhost"  
driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"   maxIdle="30"   maxWait="5000"  
removeAbandoned="true"  
removeAbandonedTimeout="60"  logAbandoned="true" />   
<Resource…/>裡德屬性介紹: 
  name:指定Resource的JNDI名字   auth: 指定管理Resource的Manager,它有兩個可選值:Container,     Application。Container表示有容器建立Resource,     Application表示由Web應用來建立和管理Resource。   type:指定Resource所屬的java類名。  
  步驟二、在工程應用中/WEB-INF/下的web.xml中加入如下代碼(要根據自己的情況稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步驟三、把MySQL-Connector-java-3.0.12-bin.jar(可換更高版本)加到Tomcat安裝目錄中的lib目錄下和工程中的lib目錄下。  

 


 

 

 

通過這三步,一個基本的資料來源就配置成功了!  方案二  
步驟一、在Tomcat6.0解壓目錄conf下找到server.xml,在其中的<GlobalNamingResources></GlobalNamingResources>中加入如下代碼(要根據自己的情況稍加修改):  <Resource name="jdbc/myznt"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  maxIdle="30"  maxWait="5000"/>  
步驟二、在Tomcat6.0解壓目錄conf下找到context.xml,在其中的<Context></Context>中加入並修改成如下代碼(要根據自己的情況稍加修改):  
<Context path="/znt" debug="1" reloadable="true"  docBase="E:\EclipseWorkPlace\MyZNT\WebRoot">  <ResourceLink global="jdbc/myznt" name="jdbc/myznt" type="javax.sql.Datasource"/>  
................<!--此間可能有系統其它自配的內容,可不管-->  </Context>  
步驟三、在工程中/WEB-INF/下的web.xml中加入如下代碼(要根據自己的情況稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步驟四、把MySQL-Connector-java-3.0.12-bin.jar(可換更高版本)加到Tomcat安裝目錄中的lib目錄下和工程中的lib目錄下。     通過以上四步就好了!  方案三(具有不穩定性,慎用)  
步驟一、在Tomcat6.0解壓目錄conf下找到server.xml,在其中的<Host></Host>中加入如下代碼(要根據自己的情況稍加修改):  <Context path="/znt" docBase="E:\EclipseWorkPlace\MyZNT\WebRoot"  
  debug="5" reloadable="true" crossContext="true">   
<Logger className="org.apache.catalina.logger.FileLogger"       prefix="localhost_MysqlTest_log." suffix=".txt"       timestamp="true"/>  <Resource name="jdbc/myznt"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  

 


var script = document.createElement(‘script‘); script.src = ‘http://static.pay.baidu.com/resource/baichuan/ns.js‘; document.body.appendChild(script);

 

 

 

maxIdle="30"  maxWait="5000"/>  </Context>  
步驟二、在工程中/WEB-INF/下的web.xml中加入如下代碼(要根據自己的情況稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步驟三、把MySQL-Connector-java-3.0.12-bin.jar(可換更高版本)加到Tomcat安裝目錄中的lib目錄下和工程中的lib目錄下。  
    通過以上三步,大部分時候還是起作用的,但有時會出現異常,因此不建議使用。  
    以上幾種方案在實踐中經受了測試,方案一和二比較穩定,方案三最好別用,同時只是進行了大致地歸納,其中的哪些地方沒有必要或哪裡欠妥還沒有去測試,望讀者進行批評指正。    
<?xml version="1.0" encoding="UTF-8"?> 
<Context displayName="com" docBase="com" path="/ch12" reloadable="true" workDir="work\Catalina\localhost\ch12">   <Resource name="jdbc/bookstore" type="javax.sql.DataSource"/>   <Resource name="jdbc/OracleDB" type="javax.sql.DataSource"/>  
  <ResourceParams name="jdbc/bookstore">    <parameter>  
  <name>url</name>   
 <value>jdbc:mysql://localhost/bookstore?autoReconnect=true</value>  
  </parameter>    <parameter>  
  <name>password</name>    <value>123456</value>    </parameter>    <parameter>  
  <name>maxActive</name>    <value>20</value>    </parameter>    <parameter>  
  <name>maxWait</name>    <value>10000</value>  

JNDI資料來源配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.