posts - 3,  comments - 2,  trackbacks - 0

              一直都JNDI這個名字,可是一直都沒有去研究過,因為工作中一直都沒用到。。。
              這次面試的時候用到了。。。 于是乎,找來例子練練手。。

              看了Tomcat官方的說明,順便看到々上善若水々的文章,理解了整個意思。
              下面將我的例子寫在這里,只希望下次面試的時候,我可以說,來我博客看吧,這些我都會。。
          在server.xml中添加:
           
                   <Context path="/connjndi" docBase="D:\workspace\TestJndi\WebRoot" reloadable="true">

                      
          <Resource name="jdbc/SampleData" auth="Container" type="javax.sql.DataSource"
                         maxActive
          ="100" maxIdle="30" maxWait="10000"
                         username
          ="sampledata" password="password" driverClassName="oracle.jdbc.OracleDriver"
                         url
          ="jdbc:oracle:thin:@//localhost:1521/mondrian"/>
                   
          </Context>

              Tomcat標(biāo)準(zhǔn)數(shù)據(jù)源資源工廠配置項如下:
              * driverClassName - 所使用的JDBC驅(qū)動類全稱。
              * maxActive - 同一時刻可以自數(shù)據(jù)庫連接池中被分配的最大活動實例數(shù)。
              * maxIdle - 同一時刻數(shù)據(jù)庫連接池中處于非活動狀態(tài)的最大連接數(shù)。
              * maxWait - 當(dāng)連接池中沒有可用連接時,連接池在拋出異常前將等待的最大時間,單位毫秒。
              *password - 傳給JDBC驅(qū)動的數(shù)據(jù)庫密碼。
              * url - 傳給JDBC驅(qū)動的連接URL。
              * user - 傳給JDBC驅(qū)動的數(shù)據(jù)庫用戶名。
              * validationQuery - 一個SQL查詢語句,用于在連接被返回給應(yīng)用前的連接池驗證。
              * 如果指定了該屬性,則必為至少返回一行記錄的SQL SELECT語句。

          在web.xml中添加:
              
              <description>Oracle Test App</description>
              
          <resource-ref>
                
          <description>DB Connection</description>
                
          <res-ref-name>jdbc/SampleData</res-ref-name>
                
          <res-type>javax.sql.DataSource</res-type>
                
          <res-auth>Container</res-auth>
              
          </resource-ref>

          寫一個簡單的jsp測試了一下:
           1<%@ page import="java.sql.*,javax.sql.*,javax.naming.*" %>
           2
           3<%
           4Connection conn = null;
           5try
           6  {
           7    Context ctx = new InitialContext(); 
           8    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/SampleData");
           9    conn = ds.getConnection();
          10    System.out.println("connection pool connected !!");   
          11  }
           catch (NamingException e) {
          12    System.out.println(e.getMessage());
          13  }
           catch (SQLException e) {
          14    System.out.println(e.getMessage());
          15    e.printStackTrace();
          16  }
          finally
          17  {
          18    conn.close();
          19  }

          20 %>

              我就是這樣連接成功了。
              
              寫的時候還是看了下々上善若水々 的博客,總感覺他的寫得比較全,而我總感覺寫不出比他好的,又不能寫出和他不同的地方。。就這樣子啦。。


          PS:在查資料的時候,看到在CSDN中有位朋友談到了JNDI的用處:
              JNDI不止用于數(shù)據(jù)源,其可以用于存儲和獲得任何類型的已命名的java對象等等.   
              
              如果使用JDBC有thin和oci兩種連接方式:   
              
              [先說thin]   
              打開數(shù)據(jù)庫連接方式:DriverManager.getConnection     

          1      DriverManager.registerDriver(new   oracle.jdbc.OracleDriver());   
          2    
          3          Connection   myConnection   =   DriverManager.getConnection(   
          4              "jdbc:oracle:thin:@test2000:1521:orac",     //orac為oracle的SID   
          5              "user",   
          6              "password"   
          7          ); 
            
              
              或者用前面給出的那個   
              OracleDataSource   myODS   =   new   OracleDataSource();   //其屬性簡單明了,不難掌握   
              Connection   myConnection   =   myODSgetConnection("user","password");   
              [再說OCI](可以使連接池中有多個緩沖的連接)   
           1    OracleOCIConnectionPool   myOOCP   =   new   OracleOCIConnectionPool();   
           2    
           3        //   set   the   attributes   for   the   physical   database   connections   
           4        myOOCP.setServerName("test2000");   
           5        myOOCP.setDatabaseName("ORCL");   
           6        myOOCP.setDriverType("oci");   
           7        myOOCP.setPortNumber(1521);   
           8        myOOCP.setUser("store_user");   
           9        myOOCP.setPassword("store_password");   
          10    
          11        //   set   the   values   for   the   dynamic   attributes   of   myOOCP   
          12        Properties   myProperties   =   new   Properties();   
          13        myProperties.put(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT,"5");   
          14        myProperties.put(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT,"10");   
          15        myProperties.put(OracleOCIConnectionPool.CONNPOOL_INCREMENT,"2");   
          16        myProperties.put(OracleOCIConnectionPool.CONNPOOL_TIMEOUT,"30");   
          17        myProperties.put(OracleOCIConnectionPool.CONNPOOL_NOWAIT,"true");   
          18        myOOCP.setPoolConfig(myProperties);   
          19    
          20        //   request   a   connection   instance   from   myOOCP   and   store   
          21        //   the   connection   instance   in   myConnection   
          22        OracleOCIConnection   myConnection   =   (OracleOCIConnection)   myOOCP.getConnection(); 
          23

              這個就當(dāng)是個引子,之后對JNDI有個深入的了解和認(rèn)識。。。。
          posted on 2008-03-26 21:27 Mr. Michael.Q 閱讀(3756) 評論(1)  編輯  收藏 所屬分類: JNDI 概念學(xué)習(xí)與應(yīng)用研究

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 昭通市| 大名县| 枞阳县| 五家渠市| 沙雅县| 综艺| 岳普湖县| 平凉市| 乐东| 北碚区| 墨竹工卡县| 三原县| 秦安县| 和硕县| 平邑县| 阳西县| 准格尔旗| 永安市| 绥芬河市| 扎兰屯市| 冀州市| 乳源| 石屏县| 丁青县| 大竹县| 辽源市| 喀喇沁旗| 山丹县| 确山县| 清苑县| 石门县| 杭锦后旗| 重庆市| 青州市| 沙坪坝区| 二连浩特市| 东至县| 阳新县| 新竹县| 三都| 张家口市|