原理:在DataSource中事先建立多個資料庫連接,儲存在資料庫連接池中。當程式訪問資料庫時,只用從串連池中取空閑狀態的資料庫連接即可,訪問結束,銷毀資源,資料庫連接重新回到串連池,呵呵,這與每次去直接存取資料庫相比,會節省大量時間和資源。恩,感覺不錯~
JNDI( Java Naming and Directory Interface ),是Java平台 的一個標準擴充,提供了一組介面、類和關於命名空間的概念。如同其它很多Java技術一樣,JDNI是provider-based的技術,暴露了一個 API和一個服務供應介面(SPI)。這意味著任何基於名字的技術都能通過JNDI而提供服務,只要JNDI支援這項技術。JNDI目前所支援的技術包括 LDAP、CORBA Common Object Service(COS)名字服務、RMI、NDS、DNS、Windows註冊表等等。很多J2EE技術,包括EJB都依靠JNDI來組織和定位實體。
哦~JNDI的概念好長~呵呵,朋友們可以把它理解為一種將對象和名字捆綁的技術,對象工廠負責生產出對象,這些對象都和唯一的名字綁在一起,外部資源可以通過名字獲得某對象的引用。
在javax.naming的包包中提供Context介面,提供了兩個很好用的方法:
<1> void bind( String name , Object object )
將名稱綁定到對象。所有中間上下文和目標上下文(由該名稱最終原子組件以外的其他所有組件指定)都必須已經存在。PS:名字不可為空~
<2>Object lookup( String name )
檢索指定的對象。如果 name為空白,則返回此內容相關的一個新執行個體(該執行個體表示與此上下文相同的命名內容,但其環境可以獨立地進行修改,而且可以並發訪問)。
外部資源訪問對象工廠中的工程圖:
例:
=================將以下程式碼片段添加到server.xml中的<Host>中============
-
<!-- configure DataSource. Add the following code into server.xml -->
<Context path="/bookstore" docBase="bookstore" debug="0"
reloadable="true" >
<!-- 資料來源名稱 -->
<Resource name="jdbc/BookDB"
auth="Container"
type="javax.sql.DataSource" />
<ResourceParams name="jdbc/BookDB">
<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.
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>dbuser</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter>
<!-- Class name for mm.mysql JDBC driver -->
-
<!-- 指定JDBC驅動 -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.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.
-->
-
<!-- 指定串連資料庫的URL -->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/BookDB?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
=================將以下程式碼片段添加到web.xml中=============
-
<!-- add the following code into <webapp> in web.xml -->
<resource-ref>
<description>DB Connection</description>
-
//對所引用的資源的說明
<res-ref-name>jdbc/BookDB</res-ref-name>
-
//指定所引用資源的JNDI名字,與<Resource>元素中的name屬性相對應
<res-type>javax.sql.DataSource</res-type>
-
//指定所引用資源的類名字,與<Resource>元素中的type屬性相對應
<res-auth>Container</res-auth>
-
//指定管理所引用的資源的Manager,與<Resource>元素中的auth屬性對應
</resource-ref>