配置數據源(DataSource)

DataSource對象是由Tomcat提供的,因此不能在程序中採用創建一個實例的方式來生成DataSource對象。

所以數據源的配置涉及修改server.xml(Tomcat)和web.xml文件,在server.xml中加入定義數據源的元素<Resource>,在web.xml中加入<resource-ref>元素,聲明該web應用所引用的數據源。

 

1.在server.xml中加入<Resource>元素

<Resource>元素用來定義JNDI Resource.在Tomcat中,DataSource是JNDI Resource的一種。以下代碼爲bookstore應用定義了一個名爲jdbc/BookDB的數據源。

 

<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 mysql max_connections large enough to handle all of your dbconnections.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>1000</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 mysql JDBC Driver -->
              <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 make sure that the mysql JDBC Driver will automatically reconnect if mysql closed the connection. Mysql by default closed idle connections after 8 hours -->
              <parameter>
                  <name>url</name>
                  <value>jdbc:mysql://localhost:3306/BookDB?autoReconnect=true</value>
              </parameter>
             
          </ResourceParams>
      </Context>

 

以上代碼定義了<Resource>和<ResourceParams>元素。

 

2.在web.xml中加入<resource-ref>元素

如果web應用訪問了由servlet容器管理的某個JNDI Resource的引用。表示資源引用的元素爲<resource-ref>,以下是聲明引用jdbc/BookDB數據源的代碼:

 

<web-app>
       <resource-ref>
           <description>DB Connection</description>
           <res-ref-name>jdbc/BookDB</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
       </resource-ref>
   </web-app>

 

3.通過數據源訪問數據庫

 

//建立數據庫連接

Context ctx = new InitialContext();

 

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

 

Connection con = ds.getConnection();

 

其他的操作就和用JDBC訪問數據庫一樣了.

 

 

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