Loading...

          java .net

          2006-11-10 星期五

          這幾天一直在休息,老師出差了,昨天研究了在tomcat下配置jndi,發現在tomcat5.05.5下配置還有些不一樣,老師是使用得5.0,按照老師那樣配在5.5下就是不行,把下面這段xml放在tomcatserver.xml中的<Host></Host>標簽里面

          <Context path="/book" docBase="d:/bookdemodata" debug="0" reloadable="true"> 

          <Resource name="jdbc/book" auth="Container" type="javax.sql.DataSource"/> 

          <ResourceParams name="jdbc/book"> 

          <parameter> 

          <name>factory</name> 

          <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 

          </parameter>

          <parameter> 

          <name>driverClassName</name> 

          <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> 

          </parameter>

          <parameter> 

          <name>url</name> 

          <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=book</value> 

          </parameter> 

          <parameter>

          <name>username</name> 

          <value>sa</value>

          </parameter> 

          <parameter>

          <name>password</name>

          <value></value> 

          </parameter> 

          <parameter>

          <name>maxActive</name>

          <value>20</value>

          </parameter>

          <parameter> 

          <name>maxIdle</name>

          <value>10</value>

          </parameter>

          <parameter> 

          <name>maxWait</name>

          <value>-1</value>

          </parameter> 

          <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> 

          </ResourceParams>

          </Context>

          如果是在5.0下就沒有問題,測試通過,而在5.5下面這樣寫就有錯誤了,到網上搜了些資料,說它們兩個配置還是有些不一樣的

          tomcat5.5conf\Catalina\localhost目錄下新建一個針對自己項目的xml文件

          <?xml version="1.0" encoding="UTF-8"?>

          <Context path="/addressbook" docBase="/addressbook" debug="0" reloadable="true"> 

          <Resource

          auth="Container"

          name="jdbc/address"

          type="javax.sql.DataSource"

          password=""

          driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"

          maxIdle="10"

          maxWait="100000"

          username="sa"

          url="jdbc:microsoft:sqlserver://localhost:1433;databasename=addressbooksample"

          maxActive="20"/>

          </Context>

          文件名叫addressbook.xml這里的文件名是隨便起的,在tomcat啟動的時候會自動加載這個目錄下的所有xml文件,OK 這樣就調試通過了,而且看起來這樣配置要比在5.0下配置簡單。

          還有一個方法

          就是把sql的配置資源

          <Resource

          auth="Container"

          name="jdbc/address"

          type="javax.sql.DataSource"

          password=""

          driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"

          maxIdle="10"

          maxWait="100000"

          username="sa"

          url="jdbc:microsoft:sqlserver://localhost:1433;databasename=addressbooksample"

          maxActive="20"/>

          放入conf文件夾下的Context.xml中的<Context></Context>標簽內,就可以了

          再有就是在java代碼中取得Connection的時候就不能像原來這樣寫了

          Context initContext = new InitialContext();

          Context envContext = (Context)initContext.lookup("java:comp/env");

          DataSource ds = (DataSource)envContext.lookup("jdbc/addressbook");

          conn = ds.getConnection();

          這樣寫我試過也不行,tomcat會有異常

          這樣寫就沒錯

          Context initContext = new InitialContext();

          DataSource ds = (DataSource)initContext.lookup("java:comp/env/jdbc/address");

          conn = ds.getConnection();

          經過一番研究試驗,終于搞定了,但是其中玄機還不知是為何

          像這個東西"java:comp/env"是啥意思(JNDI查找的Tomcat的默認名字

          Oh.god,我弄錯了

          Context initContext = new InitialContext();

          Context envContext = (Context)initContext.lookup("java:comp/env");

          DataSource ds = (DataSource)envContext.lookup("jdbc/address");

          conn = ds.getConnection();

          和這樣寫

          Context initContext = new InitialContext();

          DataSource ds = (DataSource)initContext.lookup("java:comp/env/jdbc/address");

          conn = ds.getConnection();

          都沒錯,都能出來數據

          posted on 2008-08-26 22:06 閱讀(166) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 出国| 奉新县| 芷江| 文山县| 莲花县| 绥德县| 新民市| 吕梁市| 绥棱县| 南皮县| 黄浦区| 龙南县| 晋江市| 建昌县| 南投市| 喜德县| 商丘市| 顺昌县| 克拉玛依市| 伊金霍洛旗| 辰溪县| 康保县| 禄丰县| 岫岩| 宝清县| 隆安县| 澄江县| 通化县| 常山县| 崇左市| 八宿县| 长子县| 阿荣旗| 正蓝旗| 铜山县| 兰州市| 雅江县| 黎川县| 循化| 鸡西市| 买车|