Tomcat環境下配置oracle數據源的方法

Tomcat環境下配置oracle數據源的方法

JB2K5下自帶的tomcat jakarta-tomcat-4.1.30爲例

 完整版本(PDF文件)請到http://hbird.vicp.net/t-1598.html處下載

 完整版本(PDF文件)請到http://hbird.vicp.net/t-1598.html處下載

 

 

Tomcat45)提供了一個與Java Enterprise Edition應用服務相兼容的JNDI--InitialContext實現實例。它的初始數據設置在$CATALINA_HOME/conf/server.xml文件裏。並可能在網頁應用環境描述(/WEB-INF/web.xml)裏被下列元素引用:

 <resource-ref>--資源參數,一般是數據庫驅動程序、JavaMail Session、自定義類工廠等。

 

 

$CATALINA_HOME/conf/server.xml裏設置數據庫連接池:

 

 

下面是tomcat配置樣例的代碼,必須放在<Host></Host>之間。

        <!-- Tomcat Examples Context -->

        <Context path="/examples" docBase="examples.war" debug="0"

                 reloadable="true" crossContext="true">

          <Logger className="org.apache.catalina.logger.FileLogger"

                     prefix="localhost_examples_log." suffix=".txt"

              timestamp="true"/>

          <Ejb   name="ejb/EmplRecord" type="Entity"

                 home="com.wombat.empl.EmployeeRecordHome"

               remote="com.wombat.empl.EmployeeRecord"/>

 

 

          <!-- If you wanted the examples app to be able to edit the

               user database, you would uncomment the following entry.

               Of course, you would want to enable security on the

               application as well, so this is not done by default!

               The database object could be accessed like this:

 

 

               Context initCtx = new InitialContext();

               Context envCtx = (Context) initCtx.lookup("java:comp/env");

               UserDatabase database =

                    (UserDatabase) envCtx.lookup("userDatabase");

          -->

<!--

          <ResourceLink name="userDatabase" global="UserDatabase"

                        type="org.apache.catalina.UserDatabase"/>

-->

 

 

 

 

          <!-- PersistentManager: Uncomment the section below to test Persistent

               Sessions.

 

 

               saveOnRestart: If true, all active sessions will be saved

                 to the Store when Catalina is shutdown, regardless of

                 other settings. All Sessions found in the Store will be

                 loaded on startup. Sessions past their expiration are

                 ignored in both cases.

               maxActiveSessions: If 0 or greater, having too many active

                 sessions will result in some being swapped out. minIdleSwap

                 limits this. -1 or 0 means unlimited sessions are allowed.

                 If it is not possible to swap sessions new sessions will

                 be rejected.

                 This avoids thrashing when the site is highly active.

               minIdleSwap: Sessions must be idle for at least this long

                 (in seconds) before they will be swapped out due to

                 activity.

                 0 means sessions will almost always be swapped out after

                 use - this will be noticeably slow for your users.

               maxIdleSwap: Sessions will be swapped out if idle for this

                 long (in seconds). If minIdleSwap is higher, then it will

                 override this. This isn't exact: it is checked periodically.

                 -1 means sessions won't be swapped out for this reason,

                 although they may be swapped out for maxActiveSessions.

                 If set to >= 0, guarantees that all sessions found in the

                 Store will be loaded on startup.

               maxIdleBackup: Sessions will be backed up (saved to the Store,

                 but left in active memory) if idle for this long (in seconds),

                 and all sessions found in the Store will be loaded on startup.

                 If set to -1 sessions will not be backed up, 0 means they

                 should be backed up shortly after being used.

 

 

               To clear sessions from the Store, set maxActiveSessions, maxIdleSwap,

               and minIdleBackup all to -1, saveOnRestart to false, then restart

               Catalina.

          -->

          <!--

          <Manager className="org.apache.catalina.session.PersistentManager"

              debug="0"

              saveOnRestart="true"

              maxActiveSessions="-1"

              minIdleSwap="-1"

              maxIdleSwap="-1"

              maxIdleBackup="-1">

                <Store className="org.apache.catalina.session.FileStore"/>

          </Manager>

          -->

          <Environment name="maxExemptions" type="java.lang.Integer"

                      value="15"/>

          <Parameter name="context.param.name" value="context.param.value"

                     override="false"/>

          <Resource name="jdbc/EmployeeAppDb" auth="SERVLET"

                    type="javax.sql.DataSource"/>

          <ResourceParams name="jdbc/EmployeeAppDb">

            <parameter><name>username</name><value>sa</value></parameter>

            <parameter><name>password</name><value></value></parameter>

            <parameter><name>driverClassName</name>

              <value>org.hsql.jdbcDriver</value></parameter>

            <parameter><name>url</name>

              <value>jdbc:HypersonicSQL:database</value></parameter>

          </ResourceParams>

          <Resource name="mail/Session" auth="Container"

                    type="javax.mail.Session"/>

          <ResourceParams name="mail/Session">

            <parameter>

              <name>mail.smtp.host</name>

              <value>localhost</value>

            </parameter>

          </ResourceParams>

          <ResourceLink name="linkToGlobalResource"

                    global="simpleValue"

                    type="java.lang.Integer"/>

        </Context>

下面是一些參數的說明:

 

 

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

 

 

其中:

1) path  指定路徑,這裏設定的是$CATALINA_HOME/webapps下的test目錄;

2) docBase 文件根目錄。

3) reloader  當網頁被更新時是否重新編譯。

4) maxActive 連接池的最大數據庫連接數。設爲0表示無限制。

5) maxIdle  數據庫連接的最大空閒時間。超過此空閒時間,數據庫連接將被標記爲不可用,然後被釋放。設爲0表示無限制。

6) maxWait 最大建立連接等待時間。如果超過此時間將接到異常。設爲-1表示無限制。

7) removeAbandoned 回收被遺棄的(一般是忘了釋放的)數據庫連接到連接池中。

8) removeAbandonedTimeout 數據庫連接過多長時間不用將被視爲被遺棄而收回連接池中。

9) logAbandoned 將被遺棄的數據庫連接的回收記入日誌。

10) driverClassName JDBC驅動程序。

11) url   數據庫連接字符串

 

 

通過對例子的分析不難看出,實際上需要修改的地方不多。主要的元素包括驅動名稱driverClassName、連接字符串url以及連接的用戶和密碼,同時指定應用程序包路徑即可。

 

 

生成文檔如下:

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

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

<ResourceParams name="jdbc/toni">

 <parameter>

  <name>maxActive</name>

  <!-- Maximum number of DB connections in pool.Set to 0 for no limit. -->

  <value>10</value>

 </parameter>

 <parameter>

  <name>maxIdle</name>

  <!-- Maximum number of idle DB connections to retain in pool.Set to 0 for no limit. -->

  <value>5</value>

 </parameter>

 <parameter>

  <name>maxWait</name>

  <!-- Maximum time to wait for a DB connection to become available in ms.An exception is thrown if this timeout is exceeded.Set to -1 to wait indefinitely. -->

  <value>10000</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>username</name>

  <!-- Database User Name -->

  <value>scott</value>

 </parameter>

 <parameter>

  <name>password</name>

  <!-- User Password -->

  <value>tiger</value>

 </parameter>

 <parameter>

  <name>driverClassName</name>

  <!-- Database Driver Class Name -->

  <value>oracle.jdbc.driver.OracleDriver</value>

 </parameter>

 <parameter>

  <name>url</name>

  <!-- Database Address -->

  <value>jdbc:oracle:thin:@127.0.0.1:1521:toni</value>

 </parameter>

</ResourceParams>

</Context>

 

 

與之相配合的應用程序的配置文檔在$CATALINA_HOME/webapps/test/WEB-INF/web.xml裏設置被引用的資源:

 

 

下面是配置代碼,必須放在<web-app></web-app>裏。

 

 

<!-- Database Config start -->

 

 

<resource-ref>

 

 

<description>connectDB test</description>

 

 

<res-ref-name>jdbc/toni</res-ref-name>

 

 

<res-type>javax.sql.DataSource</res-type>

 

 

<res-auth>Container</res-auth>

 

 

</resource-ref>

 

 

<!-- Database Config end -->

 

 

下面是一下參數的必要說明:

 

 

1) description  對被引用的資源的描述。

 

 

2) res-ref-name  資源名稱。見上面的<ResourceParams name="jdbc/toni">

 

 

3) res-type  資源類型。見上面的<Resource name="jdbc/toni" auth="Container" type="javax.sql.DataSource"/>

 

 

在程序中使用的方法與其他的應用大體相同。

                  Context ctx=null;

                  Connection cnn=null;

                      ctx=new InitialContext();

                      if(ctx==null)

                       throw new Exception("沒有匹配的環境");

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

                      if(ds==null)

                       throw new Exception("沒有匹配數據庫");                      

                      cnn=ds.getConnection();

                       return cnn;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章