[精華] JNDI(Java 命名和目錄接口)


[精華] JNDI(Java 命名和目錄接口)


http://www.chinaunix.net 作者:ii  發表於:2003-03-31 10:37:13
發表評論】【查看原文】【Java討論區】【關閉

JNDI(Java 命名和目錄接口)
分佈式計算環境通常使用命名和目錄服務來獲取共享的組件和資源。命名和目錄服務將名稱與位置、服務、信息和資源關聯起來。 
命名服務提供名稱—對象的映射。目錄服務提供有關對象的信息,並提供定位這些對象所需的搜索工具。有許多命名和目錄服務實現,並且到它們的接口是不同的。

Java 命名和目錄接口或 JNDI 提供了一個用於訪問不同的命名和目錄服務的公共接口。請參閱 URL java.sun.com/products/jndi/serviceproviders.html 以獲取支持通過 JNDI 接口訪問命名和目錄服務的供應商列表。 

JNDI(Java Naming and Directory Interface)
當你在開發企業beans時,JNDI很重要,因爲對一個EJB的訪問是通過JNDI的命名服務完成的。運用一個命名服務來查找與一個特定名字相關的一個對象。在EJB context中,一個命名服務找到一個企業bean,給定這個bean的名字。因此,瞭解JNDI在開發一個EJB應用程序中是至關重要的。另外,JDBC可以用JNDI來訪問一個關係數據庫。




附:The JNDI Tutorial
http://java.sun.com/products/jndi/tutorial/index.html
(下載)
http://java.sun.com/products/jndi/docs.html#TUTORIAL



JDBC2.0擴展API(1) 

[ 作者: 不詳   添加時間: 2001-8-24 14:11:50 ]
 

來源:www.csdn.net

   JDBC 2.0 API被劃分爲兩部分:JDBC 2.0核心API和JDBC 2.0標準擴展API。核心API在java.sql裏面。這是原來的版本就實現了的基本的功能。標準擴展API在javax.sql裏面。由JDBC2.0規範新規定的一些接口在這裏面。當然,JDBC2.0也對原來版本的java.sql核心做了一些改動。不過不是很大。原來JDBC1.0的程序可以不加修改的在JDBC2.0上運行。這是Java的一貫的良好的作風。最新的JDBC包可以從sun公司的網站上下載。 
  JDBC2.0的擴展API增加了一些數據訪問和數據源訪問的重大的功能。這中間有一些是主要用來做企業計算的。用JDBC2.0的新的擴展包,JDBC提供了一個從JAVA2平臺的通用的數據訪問的方法。
  首先,我們來看看JDBC標準擴展的API怎樣來和JDBC2.0結合在一起的。JDBC2.0包括兩個包:
  1、 java.sql包,個包裏面是JDBC2.0的核心API。它包括了原來的JDBC API(JDBC 1.0版本),再加上一些新的2.0版本的API。這個包在Java 2 Platform SDK裏面有。
  2、 javax.sql包,這裏面是JDBC2.0的標準擴展API。這個包是一個全新的,在Java 2 Platform SDK, Enterprise Edition裏面單獨提供。
  JDBC2.0的核心API包括了JDBC1.0的API,並在此基礎上增加了一些功能,對某些性能做了增強。使java語言在數據庫計算的前端提供了統一的數據訪問方法,效率也得到了提高。
  JDBC是向後兼容的,JDBC1.0的程序可以不加修改的運行在JDBC2.0上。但是,假如程序中用到了JDBC2.0的新特性,就必須要運行在JDBC2.0版本上。
  概括的來說,JDBC核心API的新特性在兩個方面做了工作。一個是支持一些新的功能,另一個就是支持SQL3的數據類型。
  1、 在支持新功能方面:包括結果集可以向後滾動,批量的更新數據。另外,還提供了UNICODE字符集的字符流操作。
  2、 在支持SQL3的數據類型方面:包括新的SQL3數據類型,增加了對持久性對象的存貯。
  爲了對數據的存取,操作更加方便,JDBC的新特性是應用程序的設計更容易了。例如:數據塊的操作能夠顯著的提高數據庫訪問的性能。新增加的BLOB, CLOB,和數組接口能夠是應用程序操作大塊的數據類型,而不必客戶端在存貯之前進行其它的處理。這樣,就顯著的提高了內存的使用效率。
   下面我們來介紹JDBC2.0的標準擴展API。標準擴展API分爲如下幾個方面:
  1、 DataSource接口:和Java名字目錄服務(JNDI)一起工作的數據源接口。它提供了對數 吹囊恢指玫牧臃椒ā?br>;  2、 Connection pooling(連接池):可以重複使用連接,而不是對每個請求都使用一個新的連接。
  3、 Distrubute transaction(分佈式的事務):在一個事務中涉及到了多個數據庫服務器。
  4、 Rowsets:JavaBean組件包含了結果集,主要用來將數據傳給瘦客戶,或者提供一個可以滾動的結果集。
  下面我們一個一個來介紹:
  一、DataSource接口是一個更好的連接數據源的方法:
  JDBC1.0是原來是用DriverManager類來產生一個對數據源的連接。JDBC2.0用一種替代的方法,使用DataSource的實現,代碼變的更小巧精緻,也更容易控制。
  一個DataSource對象代表了一個真正的數據源。根據DataSource的實現方法,數據源既可以是從關係數據庫,也電子表格,還可以是一個表格形式的文件。當一個DataSource對象註冊到名字服務中,應用程序就可以通過名字服務獲得DataSource對象,並用它來產生一個與DataSource代表的數據源之間的連接。
  關於數據源的信息和如何來定位數據源,例如數據庫服務器的名字,在哪臺機器上,端口號等等,都包含在DataSource對象的屬性裏面去了。這樣,對應用程序的設計來說是更方便了,因爲並不需要硬性的把驅動的名字寫死到程序裏面去。通常驅動名字中都包含了驅動提供商的名字,而在DriverManager類中通常是這麼做的。如果數據源要移植到另一個數據庫驅動中,代碼也很容易做修改。所需要做的修改只是更改DataSource的相關的屬性。而使用DataSource對象的代碼不需要做任何改動。
  由系統管理員或者有相應權限的人來配置DataSource對象。配置DataSource,包括設定DataSource的屬性,然後將它註冊到JNDI名字服務中去。在註冊DataSource對象的的過程中,系統管理員需要把DataSource對象和一個邏輯名字關聯起來。名字可以是任意的,通常取成能代表數據源並且容易記住的名字。在下面的例子中,名字起爲:InventoryDB,按照慣例,邏輯名字通常都在jdbc的子上下文中。這樣,邏輯名字的全名就是:jdbc/ InventoryDB。
  一旦配置好了數據源對象,應用程序設計者就可以用它來產生一個與數據源的連接。下面的代碼片段示例瞭如何用JNDI上下文獲得一個一個數據源對象,然後如何用數據源對象產生一個與數據源的連接。開始的兩行用的是JNDI API,第三行用的纔是JDBC的API:
   Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/InventoryDB");Connection con = ds.getConnection("myPassword", "myUserName");
  在一個基本的DataSource實現中,DataSource.getConnection方法返回的Connection對象和用DriverManager.getConnection方法返回的Connection對象是一樣的。因爲DataSource提供的方便性,我們推薦使用DataSource對象來得到一個Connection對象。我們希望所以的基於JDBC2.0技術的數據庫驅動都包含一個基本的DataSource的實現,這樣就可以在應用程序中很容易的使用它。
  對於普通的應用程序設計者,是否使用DataSource對象只是一個選擇問題。但是,對於那些需要用的連接池或者分佈式的事務的應用程序設計者來說,就必須使用DataSource對象來獲得Connection,原因在下面我們會提到。
   二、Connection pooling(連接池):
  連接池是這麼一種機制,當應用程序關閉一個Connection的時候,這個連接被回收,而不是被destroy,因爲建立一個連接是一個很費資源的操作。如果能把回收的連接重新利用,會減少新創建連接的數目,顯著的提高運行的性能。
  假設應用程序需要建立到一個名字爲EmpolyeeDB的DataSource的連接。使用連接池得到連接的代碼如下:
   Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/EmployeeDB");Connection con = ds.getConnection("myPassword", "myUserName");除了邏輯名字以外,我們發現其代碼和上面舉的例子的代碼是一樣的。邏輯名字不同,就可以連接到不同的數據庫。DataSource對象的getConnection方法返回的Connection是否是一個連接池中的連接完全取決於DataSource對象的實現方法。如果DataSource對象實現與一個支持連接池的中間層的服務器一起工作,DataSource對象就會自動的返回連接池中的連接,這個連接也是可以重複利用的。
  是否使用連接池獲得一個連接,在應用程序的代碼上是看不出不同的。在使用這個Connection連接上也沒有什麼不一樣的地方,唯一的不同是在java的finally語句塊中來關閉一個連接。在finally中關閉連接是一個好的編程習慣。這樣,即使方法拋出異常,Connection也會被關閉並回收到連接池中去。代碼應該如下所示:
  try{…
  }catch(){…
  }finally{ if(con!=null)con.close();}
  三、分佈式事務:
  獲得一個用來支持分佈式事務的連接與獲得連接池中的連接是很相似的。同樣,不同之處在於DataSource的實現上的不同,而不是在應用程序中獲得連接的方式上有什麼不同。假設DataSource的實現可以與支持分佈式事務中間層服務器一起工作,得到連接的代碼還是如下所示:
   Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/EmployeeDB"); Connection con = ds.getConnection("myPassword", "myUserName");由於性能上的原因,如果一個DataSource能夠支持分佈式的事務,它同樣也可以支持連接池管理。
  從應用程序設計者的觀點來看。是否支持分佈式的事務的連接對它來說沒什麼不同,唯一的不同是在事務的邊界上(開始一個事務的地方和結束一個事務的地方),開始一個事務或者結束一個事務都是由事務服務器來控制的。應用程序不應該做任何可能妨礙服務的事情。應用程序不能夠直接調用事務提交commit或者回滾rollback操作,也不能夠使用事務的自動提交模式auto-commit mode(在數據庫操作完成的時候自動的調用commit或者rollback)。 
 
在一個連接參與了分佈式事務的時候,下面的代碼是你不能做的(con表示支持分佈式事務的連接Connection)。
  con.commit();或者con.rollback();或者con.setAutoCommit(true);對於通常的Connection來說,缺省的是auto-commit模式。而對於支持分佈式事務的Connection來說,缺省不是auto-commit模式。注意,即使Connection是支持事務的,它也可以用於沒有事務的情況。關於事務邊界的限制只是是對分佈式事務的情況下才成立的。
  配置支持連接池的DataSource的時候,涉及到配置ConnectionPoolDataSource對象,這個對象是三層體系結構中的中間層來管理連接池的。同樣的,在配置支持分佈式事務的時候,需要配置XADataSource,XADataSource是中間層用來管理分佈式事物的對象。ConnectionPoolDataSource和XADataSource是由驅動提供商提供的,對應用程序的設計者來說是透明的。和基本的DataSource一樣,系統管理員來配置ConnectionPoolDataSource和XADataSource對象。
  四、結果集:
  結果集對象是一行行數據的容器。根據其目的,可以通過多種方法實現。RowSet及其相關的接口與JDBC2.0的標準擴展API有點不同,他們並不是驅動的一部分,RowSet是在驅動的上層實現的,可以由其它的任何人來實現他們。
  任何類型的rowset都實現了RowSet接口,RowSet接口擴展了ResultSet接口。這樣RowSet對象就有了ResultSet對象所有的功能。能夠通過getXXX方法得到數據庫中的某列值,通過updateXXX方法可以修改某列值,可以移動光標,是當前行變爲另一行。
  當然,我們更感興趣的是RowSet接口提供的新的功能。作爲一個JavaBean組件,RowSet對象可以增加或者刪除一個listener(監聽者),可以get或者set其屬性值,這些屬性中,有一個是字符串,表示一個對數據庫Query請求,RowSet接口定義了設定參數的方法,也提供了執行這個請求的方法。這意味着RowSet對象能夠執行查詢請求,可以根據它產生的結果集進行計算。同樣,RowSet也可以根據任何表格數據源進行計算,所以,它不侷限於關係數據庫。
  從數據源得到數據之後,RowSet對象可以和數據源斷開連接,rowset也可以被序列化。這樣,RowSet就可以通過網絡傳遞給瘦客戶端。
  RowSet可以被重新連接到數據源,這樣,做的修改就可以存回到數據源中去。如果產生了一個listener,當RowSet的當前行移動,或者數據被修改的時候,監聽者就會收到通知。例如,圖形用戶界面組件可以註冊成爲監聽者,當RowSet更改的時候,圖形用戶界面接到通知,就可以修改界面,來符合它所表示的RowSet。
  根據不同的需要,RowSet接口可以通過多種方法來實現。Java software已經寫了一個CachedRowSet實現,從http://developer.java.sun.com/developer/earlyAccess/crs/index.html中可以得到這個實現。
  與CachedRowSet類不樣的是,JDBCRowSet類總是保持一個和數據源的連接。這樣,在ResultSet外圍簡單到加了一層,是基於JDBC技術的驅動看起來象是一個簡單的JavaBean組件一樣。

  總結:JDBC2.0標準擴展API通過見DataSource註冊到JNDI名字服務上,將JDBC技術擴展爲一個全新的概念。使應用程序的代碼更加精巧,易於控制。新的API支持了連接池,支持分佈式的事務。最後,還使java應用程序可以在網絡上傳播結果集,是不可以滾動的ResultSet變成了可以滾動的RowSet。



 ii 回覆於:2003-03-11 08:17:53

Tomcat中配置和使用JNDI 

JNDI是J2EE中一個很重要的標準,通常我們是在EJB編程中用到, 
Tomcat4.0中提供了在JSP和Servelt中直接使用JNDI的方法,下面談一下在Tomcat4.0中配置和使用JNDI的方法 
(以通過JNDI連接數據庫爲例) 
假設使用的數據庫是mysql,實驗例子在TOMCAT_HOME/webapps/DBTest目錄中 

A.將mysql的JDBC連接庫mm.mysql-2.0.9-bin.jar放入TOMCAT_HOME/common/lib中 

B.配置TOMCAT_HOME/conf/serer.xml文件在<Service>;段中加入一個Context: 
<Context path="/DBTest" docBase="DBTest" 
debug="5" reloadable="true" crossContext="true">; 
</Context>; 
這是DBTest的根路徑,這是爲了在DBTest中使用做準備. 

C.在上面加入的<Context>;段加入 
<Resource name="jdbc/TestDB" 
auth="Container" 
type="javax.sql.DataSource"/>; 

<ResourceParams name="jdbc/TestDB">; 
<parameter>; 
<name>;factory</name>; 
<value>;org.apache.commons.dbcp.BasicDataSourceFactory</value>; 
</parameter>; 

<!-- Maximum number of dB connections in pool. Make sure you 
configure your mysqld max_connections large enough to handle 
all of your db connections. Set to 0 for no limit. 
-->; 
<parameter>; 
<name>;maxActive</name>; 
<value>;100</value>; 
</parameter>; 

<!-- Maximum number of idle dB connections to retain in pool. 
Set to 0 for no limit. 
-->; 
<parameter>; 
<name>;maxIdle</name>; 
<value>;30</value>; 
</parameter>; 

<!-- Maximum time to wait for a dB connection to become available 
in ms, in this example 10 seconds. An Exception is thrown if 
this timeout is exceeded. Set to -1 to wait indefinitely. 
-->; 
<parameter>; 
<name>;maxWait</name>; 
<value>;10000</value>; 
</parameter>; 

<!-- MySQL dB username and password for dB connections -->; 
<parameter>; 
<name>;username</name>; 
<value>;test</value>; 
</parameter>; 
<parameter>; 
<name>;password</name>; 
<value>;test</value>; 
</parameter>; 

<!-- Class name for mm.mysql JDBC driver -->; 
<parameter>; 
<name>;driverClassName</name>; 
<value>;org.gjt.mm.mysql.Driver</value>; 
</parameter>; 

<!-- The JDBC connection url for connecting to your MySQL dB.-->; 
<parameter>; 
<name>;url</name>; 
<value>;jdbc:mysql://localhost:3306/test</value>; 
</parameter>; 
</ResourceParams>; 

這裏每一個小段都有英文註解,是Tomcat提供的,我們可以將按照Sample加入,主要修改的是driverClassName, 
url,和用戶帳號;需要強調的是"jdbc/TestDB"就是JDNI要查找的Name. 

D. 在JSPh或servlet中使用JNDI查找服務 
下面是在JSP文件中關於JNDI使用的代碼(文件名記爲UserHandleDB.jsp) 
需要注意的是JNDI NAME要在前面加上"java:comp/env/" 

<%@ page language="java"%>; 
<%@ page import="java.util.*" %>; 
<%@ page import="java.sql.*" %>; 
<%@ page import="javax.sql.*" %>; 
<%@ page import="javax.naming.*" %>; 

<% 
String jndi_name="java:comp/env/jdbc/TestDB"; 
String select_user_sql="select userid,name,birthday, email from emp"; 
String colnames[][]={{"User ID","Name","Birth day","EMail"}, 
{"userid","name","birthday","email"}}; 
Vector userSet=new Vector(); 
Vector columnSet=new Vector(); 

for(int i=0;i<colnames[0].length;i++){ 
columnSet.add(colnames[0]); 

userSet.add(columnSet); 

Context ctx = new InitialContext(); 
if(ctx == null ) 
throw new Exception("No Context"); 

DataSource ds = (DataSource)ctx.lookup(jndi_name); 

Connection conn = ds.getConnection(); 

try { 
PreparedStatement psPreparedStatement=conn.prepareStatement(select_user_sql); 
ResultSet resultSet = psPreparedStatement.executeQuery(); 
while(resultSet.next()){ 
columnSet=new Vector(); 
for(int i=0;i<colnames[1].length;i++){ 
columnSet.add(resultSet.getString(colnames[1])); 

userSet.add(columnSet); 

}catch(SQLException e) { 
e.printStackTrace(); 
}finally { 
conn.close(); 
%>; 


E. 引用UserHandleDB.jsp(記爲ViewTable.jsp) 
<html>; 
<head>; 
<title>;Test Database </title>; 
<body >; 
<%@ include file="UserHandleDB.jsp" %>; 
<table border="1" >; 
<% 
for(int i=0;i<userSet.size();i++){ 
Vector colSet=(Vector)userSet.get(i); 
out.print("<tr>;"); 
for(int j=0;j<colSet.size();j++){ 
String col=(String)colSet.get(j); 
out.print("<td>;"+col+"</td>;"); 

out.print("</tr>;"); 

%>; 
</table>; 
</body>; 
</html>; 

F. 在web.xml中加入 
<resource-ref>; 
<description>;DB Connection</description>; 
<res-ref-name>;jdbc/TestDB</res-ref-name>; 
<res-type>;javax.sql.DataSource</res-type>; 
<res-auth>;Container</res-auth>; 
</resource-ref>; 
這裏的jdbc/TestDb要和C中Resource段的name匹配 

G. 觀察結果 
首先確定數據庫已經啓動,接着啓動Tomcat,如果Tomcat啓動異常,可能的原因是數據庫的JDBC庫沒有加載 
最後打開瀏覽器,訪問 http://localhost:8080/DBTest/ViewTable.jsp就可以看到結果


 ii 回覆於:2003-03-11 08:45:24

Common Problems 
Here are some common problems encountered with a web application which uses a database and tips for how to solve them.

Intermittent dB Connection Failures 
Tomcat runs within a JVM. The JVM periodically performs garbage collection (GC) to remove java objects which are no longer being used. When the JVM performs GC execution of code within Tomcat freezes. If the maximum time configured for establishment of a dB connection is less than the amount of time garbage collection took you can get a db conneciton failure. 

To collect data on how long garbage collection is taking add the -verbose:gc argument to your CATALINA_OPTS environment variable when starting Tomcat. When verbose gc is enabled your $CATALINA_BASE/logs/catalina.out log file will include data for every garbage collection including how long it took.

When your JVM is tuned correctly 99% of the time a GC will take less than one second. The remainder will only take a few seconds. Rarely, if ever should a GC take more than 10 seconds.

Make sure that the db connection timeout is set to 10-15 seconds. For the DBCP you set this using the parameter maxWait.
 
Random Connection Closed Exceptions 
These can occur when one request gets a db connection from the connection pool and closes it twice. When using a connection pool, closing the connection just returns it to the pool for reuse by another request, it doesn't close the connection. And Tomcat uses multiple threads to handle concurrent requests. Here is an example of the sequence of events which could cause this error in Tomcat: 

  Request 1 running in Thread 1 gets a db connection.

  Request 1 closes the db connection.

  The JVM switches the running thread to Thread 2

  Request 2 running in Thread 2 gets a db connection
  (the same db connection just closed by Request 1).

  The JVM switches the running thread back to Thread 1

  Request 1 closes the db connection a second time in a finally block.

  The JVM switches the running thread back to Thread 2

  Request 2 Thread 2 tries to use the db connection but fails
  because Request 1 closed it.

Here is an example of properly written code to use a db connection obtained from a connection pool: 
  Connection conn = null;
  Statement stmt = null;  // Or PreparedStatement if needed
  ResultSet rs = null;
  try {
    conn = ... get connection from connection pool ...
    stmt = conn.createStatement("select ...");
    rs = stmt.executeQuery();
    ... iterate through the result set ...
    rs.close();
    rs = null;
    stmt.close();
    stmt = null;
    conn.close(); // Return to connection pool
    conn = null;  // Make sure we don't close it twice
  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
    // Always make sure result sets and statements are closed,
    // and the connection is returned to the pool
    if (rs != null) {
      try { rs.close(); } catch (SQLException e) { ; }
      rs = null;
    }
    if (stmt != null) {
      try { stmt.close(); } catch (SQLException e) { ; }
      stmt = null;
    }
    if (conn != null) {
      try { conn.close(); } catch (SQLException e) { ; }
      conn = null;
    }
  }


 ii 回覆於:2003-03-11 08:46:01

0. Introduction
Versions of MySQL and the mm.mysql JDBC driver when have been reported to work: 

MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha 
mm.mysql 2.0.14 (JDBC Driver) 
Please let us know if you have tested the new MySQL mm.mysql 3.0 driver. 

1. MySQL configuration
Ensure that you follow these instructions as variations can cause problems. 

Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password. 

   
 mysql>; GRANT ALL PRIVILEGES ON *.* TO javauser@localhost 
    ->;   IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql>; create database javatest;
mysql>; use javatest;
mysql>; create table testdata (
    ->;   id int not null auto_increment primary key,
    ->;   foo varchar(25), 
    ->;   bar int);

  
   

Note: the above user should be removed once testing is complete! 

Next insert some test data into the testdata table. 

   
 mysql>; insert into testdata values(null, 'hello', 12345);
Query OK, 1 row affected (0.00 sec)

mysql>; select * from testdata;
+----+-------+-------+
| ID | FOO   | BAR   |
+----+-------+-------+
|  1 | hello | 12345 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql>;

  
   


2. server.xml configuration
Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to $CATALINA_HOME/conf/server.xml.

Add this in between the </Context>; tag of the examples context and the </Host>; tag closing the localhost definition.

   
 <Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">;

  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_DBTest_log." suffix=".txt"
             timestamp="true"/>;

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

  <ResourceParams name="jdbc/TestDB">;
    <parameter>;
      <name>;factory</name>;
      <value>;org.apache.commons.dbcp.BasicDataSourceFactory</value>;
    </parameter>;

    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->;
    <parameter>;
      <name>;maxActive</name>;
      <value>;100</value>;
    </parameter>;

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->;
    <parameter>;
      <name>;maxIdle</name>;
      <value>;30</value>;
    </parameter>;

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->;
    <parameter>;
      <name>;maxWait</name>;
      <value>;10000</value>;
    </parameter>;

    <!-- MySQL dB username and password for dB connections  -->;
    <parameter>;
     <name>;username</name>;
     <value>;javauser</value>;
    </parameter>;
    <parameter>;
     <name>;password</name>;
     <value>;javadude</value>;
    </parameter>;

    <!-- Class name for mm.mysql JDBC driver -->;
    <parameter>;
       <name>;driverClassName</name>;
       <value>;org.gjt.mm.mysql.Driver</value>;
    </parameter>;

    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->;
    <parameter>;
      <name>;url</name>;
      <value>;jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>;
    </parameter>;
  </ResourceParams>;
</Context>;

  
   


3. web.xml configuration
Now create a WEB-INF/web.xml for this test application. 

   
 <?xml version="1.0" encoding="ISO-8859-1"?>;
    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">;
<web-app>;
  <description>;MySQL Test App</description>;
  <resource-ref>;
      <description>;DB Connection</description>;
      <res-ref-name>;jdbc/TestDB</res-ref-name>;
      <res-type>;javax.sql.DataSource</res-type>;
      <res-auth>;Container</res-auth>;
  </resource-ref>;
</web-app>;

  
   


4. Test code
Now create a simple test.jsp for use later. 

   
 <html>;
  <head>;
    <title>;DB Test</title>;
  </head>;
  <body>;

  <%
    foo.DBTest tst = new foo.DBTest();
    tst.init();
  %>;

  <h2>;Results</h2>;
    Foo <%= tst.getFoo() %>;<br/>;
    Bar <%= tst.getBar() %>;

  </body>;
</html>;

  
   


And create a Java class to actually use your new Datasource and connection pool. Note: this code isn't anywhere near production ready - it's only supposed to be used as a simple test :-) 

   
 package foo;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class DBTest {

  String foo = "Not Connected";
  int bar = -1;
    
  public void init() {
    try{
      Context ctx = new InitialContext();
      if(ctx == null ) 
          throw new Exception("Boom - No Context");

      DataSource ds = 
            (DataSource)ctx.lookup(
               "java:comp/env/jdbc/TestDB");

      if (ds != null) {
        Connection conn = ds.getConnection();
              
        if(conn != null)  {
            foo = "Got Connection "+conn.toString();
            Statement stmt = conn.createStatement();
            ResultSet rst = 
                stmt.executeQuery(
                  "select id, foo, bar from testdata");
            if(rst.next()) {
               foo=rst.getString(2);
               bar=rst.getInt(3);
            }
            conn.close();
        }
      }
    }catch(Exception e) {
      e.printStackTrace();
    }
 }

 public String getFoo() { return foo; }
 public int getBar() { return bar;}
}

  
   


Finally deploy your web app into $CATALINA_HOME/webapps either as a warfile called DBTest.war or into a sub-directory called DBTest

Once deployed, point a browser at http://localhost:8080/DBTest/test.jsp to view the fruits of your hard work.


 ii 回覆於:2003-03-11 09:19:29

datasource:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html

Resources:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-resources-howto.html

Realm
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/realm-howto.html

安全管理
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/security-manager-howto.html


 roybao 回覆於:2003-03-31 10:30:55

謝謝!


 rickymoth 回覆於:2003-03-31 10:37:13

受益非淺,非常感謝



原文鏈接:http://bbs.chinaunix.net/viewthread.php?tid=30421
轉載請註明作者名及原文出處

發佈了28 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章