tomcat常用配置

來源:互聯網
上載者:User

tomcat共用線程池的配置

配置很簡單
第一步,開啟共用的線程池

1.<Service name="Catalina">     2.  <!--The connectors can use a shared executor, you can define one or more named thread pools-->     3.     4.    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"      5.    maxThreads="1000" minSpareThreads="50" maxIdleTime="600000"/>     6.  <Service name="Catalina">  7.<!--The connectors can use a shared executor, you can define one or more named thread pools-->  8.<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"  9.maxThreads="1000" minSpareThreads="50" maxIdleTime="600000"/>  

預設前後是注釋<!-- -->掉的,去掉就可以了。其中

name
The name used to reference this pool in other places in server.xml. The name is required and must be unique.

這個是線程池的名字,必須唯一,我們在後面的配置裡要用到這個東西

namePrefix
(String) The name prefix for each thread created by the executor. The thread name for an individual thread will be namePrefix+threadNumber

線程的名字首碼,用來標記線程名字的,這樣每個線程就用這個首碼加上線程編號了,比如
catalina-exec-1
catalina-exec-2

maxThreads
(int) The max number of active threads in this pool, default is 200
允許的最大線程池裡的線程數量,預設是200,大的並發應該設定的高一些,反正只是限制而已,不佔用資源

minSpareThreads
(int) The minimum number of threads always kept alive, default is 25
最小的保持活躍的線程數量,預設是25.這個要根據負載情況自行調整了。太小了就影響反應速度,太大了白白佔用資源。

maxIdleTime
(int) The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

超過最小活躍線程數量的線程,如果空閑時間超過這個設定後,會被關別。預設是1分鐘。

threadPriority
(int) The thread priority for threads in the executor, the default is Thread.NORM_PRIORITY

線程的等級。預設是Thread.NORM_PRIORITY

第二步
在 Connector裡指定使用共用線程池

1.<Connector     2.  port="8009"     3.  protocol="AJP/1.3"     4.  maxThreads="5000"     5.  executor="tomcatThreadPool"     6.    <Connector  7.port="8009"  8.protocol="AJP/1.3"  9.maxThreads="5000"  10.executor="tomcatThreadPool"  

注意,一旦使用了線程池,則其它的線程屬性,比如 maxThreads等將被忽略

我測試了一下,由於每次請求不再需要重新分配線程,系統響應速度還是有很明顯的改善的。

串連池的配置

本方法的原理是,在%CATALINA%\conf\server.xml中設定資料庫的串連屬性,
在應用目錄的/WEB-INF/web.xml中配置一個引用,
然後在應用中的/META-INF/context.xml中將以上兩個配置聯絡起來。
所以真正產生串連的是tomcat系統級,因此資料庫驅動應該放在%CATALINA%\common\lib中

以下是具體步驟:

1.將資料庫驅動程式的JAR檔案放在Tomcat的 common/lib 中。

2.在server.xml中設定資料來源,以MySQL資料庫為例,如下:
在<GlobalNamingResources> </GlobalNamingResources>節點中加入,注意,一旦使用了線程池,則其它的線程屬性,比如 maxThreads等將被忽略

我測試了一下,由於每次請求不再需要重新分配線程,系統響應速度還是有很明顯的改善的。

串連池的配置

本方法的原理是,在%CATALINA%\conf\server.xml中設定資料庫的串連屬性,
在應用目錄的/WEB-INF/web.xml中配置一個引用,
然後在應用中的/META-INF/context.xml中將以上兩個配置聯絡起來。
所以真正產生串連的是tomcat系統級,因此資料庫驅動應該放在%CATALINA%\common\lib中

以下是具體步驟:

1.將資料庫驅動程式的JAR檔案放在Tomcat的 common/lib 中。

2.在server.xml中設定資料來源,以MySQL資料庫為例,如下:
在<GlobalNamingResources> </GlobalNamingResources>節點中加入,

 

1.<Resource  2.  name="jdbc/DBPool"  3.  type="javax.sql.DataSource"  4.  password="root"  5.  driverClassName="com.mysql.jdbc.Driver"  6.  maxIdle="2"  7.  maxWait="5000"  8.  username="root"  9.  url="jdbc:mysql://127.0.0.1:3306/test"  10.  maxActive="4"/>  

 

屬性說明:name,資料來源名稱,通常取”jdbc/XXX”的格式;
  type,”javax.sql.DataSource”;
  password,資料庫使用者密碼;
  driveClassName,資料庫驅動;
  maxIdle,最大空閑數,資料庫連接的最大空閑時間。超過空閑時間,資料庫連
  接將被標記為不可用,然後被釋放。設為0表示無限制。
  MaxActive,串連池的最大資料庫連接數。設為0表示無限制。
maxWait ,最大建立串連等待時間。如果超過此時間將接到異常。設為-1表示
  無限制。

3.在你的web應用程式的web.xml中設定資料來源參考,如下:
  在<web-app></web-app>節點中加入,
 

1.<resource-ref>  2.  <description>MySQL DB Connection Pool</description>  3.  <res-ref-name>jdbc/DBPool</res-ref-name>  4.  <res-type>javax.sql.DataSource</res-type>  5.  <res-auth>Container</res-auth>  6.  <res-sharing-scope>Shareable</res-sharing-scope>  7. </resource-ref>  

子節點說明: description,描述資訊;
  res-ref-name,參考資料源名字,同上一步的屬性name;
  res-type,資源類型,”javax.sql.DataSource”;
  res-auth,”Container”;
  res-sharing-scope,”Shareable”;

4.在web應用程式的/META-INF/context.xml中設定資料來源連結,如下:
  在<Context></Context>中加入:

1.<Context>  2. <ResourceLink  3. name="jdbc/DBPool"    4. type="javax.sql.DataSource"    5. global="jdbc/DBPool"/>  6. </Context>  

屬性說明:name,同第2步和第3步的屬性name值,和子節點res-ref-name值;
  type,同樣取”javax.sql.DataSource”;
  global,同name值。

至此,設定完成,下面是如何使用資料庫連接池。
1.建立一個串連池類,DBPool.java,用來建立串連池,代碼如下:

1.import javax.naming.Context;  2.import javax.naming.InitialContext;  3.import javax.naming.NamingException;  4.import javax.sql.DataSource;  5.  6.public class DBPool {  7.  private static DataSource pool;  8.  static {  9.  Context env = null;  10.  try {  11.  env = (Context) new InitialContext().lookup("java:comp/env");  12.  pool = (DataSource)env.lookup("jdbc/DBPool");  13.  if(pool==null)    14.  System.err.println("'DBPool' is an unknown DataSource");  15.  } catch(NamingException ne) {  16.  ne.printStackTrace();  17.  }  18.  }  19.  public static DataSource getPool() {  20.  return pool;  21.  }  22.}  

2.在要用到資料庫操作的類或jsp頁面中,用DBPool.getPool().getConnection(),
獲得一個Connection對象,就可以進行資料庫操作,最後別忘了對Connection對象調用close()方法,
注意:這裡不會關閉這個Connection,而是將這個Connection放回資料庫連接池。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.