2015-08-14 175 views
3

我有以下配置我standalone.xmlJboss的AS7連接池將無法重新連接

<subsystem xmlns="urn:jboss:domain:datasources:1.1"> 
    <datasources> 
     <datasource jta="true" jndi-name="java:/jdbc/myds" pool-name="CADS" enabled="true" use-java-context="true" use-ccm="true"> 
      <connection-url>jdbc:postgresql://db.host/name</connection-url> 
      <driver>postgresql</driver> 
      <new-connection-sql>select 1</new-connection-sql> 
      <pool> 
       <min-pool-size>20</min-pool-size> 
       <max-pool-size>100</max-pool-size> 
       <flush-strategy>IdleConnections</flush-strategy> 
      </pool> 
      <security> 
       <user-name>user</user-name> 
       <password>pwd</password> 
      </security> 
      <validation> 
       <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/> 
       <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/> 
      </validation> 
      <timeout> 
       <blocking-timeout-millis>30000</blocking-timeout-millis> 
       <idle-timeout-minutes>1</idle-timeout-minutes> 
      </timeout> 
      <statement> 
       <track-statements>true</track-statements> 
      </statement> 
     </datasource> 
     <drivers> 
      <driver name="postgresql" module="org.postgresql"> 
       <xa-datasource-class>org.postgresql.Driver</xa-datasource-class> 
      </driver> 
     </drivers> 
    </datasources> 
</subsystem> 

如果由於某種原因,數據庫停止響應爲第二時,JBoss無法重新連接,我要重新啓動應用服務器。

但是,如果我改變datasourcexa-datasource使用org.postgresql.xa.PGXADataSource驅動程序(保持的配置,因爲它是在本例中),它的工作原理

事情是:我無法理解這一點。糾正我,如果我錯了,但xa-datasources應該被用來在多個數據庫同步提交,這不是這裏的情況。我實際上配置了多個數據庫,但我不需要同步它們之間的事務。

「默認」datasource在連接池大小方面似乎也有問題。有時,應用程序的負載無關緊要,它會打開超過100個連接(即使限制爲100),並在幾秒鐘後關閉它們。這很難重現 - 因爲它看起來是隨機的,所以,我無法確定切換到xa-datasource也解決了這個問題。

現在:

  • 爲什麼切換到xa-datasource作品?
  • 這樣做的含義是什麼?
  • 爲什麼連接池會像這樣瘋狂?

只是爲了澄清,我的試驗包括:

  1. 啓動的Postgres和應用服務器;
  2. 對應用程序做一些請求;
  3. 停止數據庫;
  4. 對應用程序執行一些請求 - 並查看它們無法工作,因爲它無法打開任何連接;
  5. 重新啓動數據庫;
  6. 做一些應用程序的請求

在最後一步,xa-datasource可以用Postgres的重新連接並一切正常。 datasource不能,並永遠失敗,加載並不重要 - 我必須重新啓動應用程序服務器。

回答

3

配置jboss時要記住的一件事情是,有時最好的文檔是在單個組件的項目中。在數據源設置的情況下,我總是告訴人們要看看IronJacamar文檔: http://www.ironjacamar.org/doc/userguide/1.0/en-US/html_single/

你想要做什麼,這些設置應該工作:

<validation> 
    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/> 
    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/> 

    <!-- I don't know what this does but someone on my DevOps 
    team said to set it this way. :) --> 
    <validate-on-match>false</validate-on-match> 

    <!-- validate the connection using a background 
    thread rather than right before you try to use the connection --> 
    <background-validation>true</background-validation> 

    <!-- sets the frequency the background thread will check each connection. 
    The lower this setting, the quicker it will find a bad connection 
    but it will be more chatty sending the validations to the server --> 
    <background-validation-millis>60000</background-validation-millis> 

    <!-- fast fail will mark all the connections invalid as soon as 
    it finds a bad one. This will make it clear the pool quicker 
    if all connections are reset at once such as a restart. Fast 
    fail would be trouble though if you had a setup where the database 
    sometimes selectively kills a single connection, such as killing long 
    running queries. --> 
    <use-fast-fail>true</use-fast-fail> 

</validation> 
+0

這似乎解決了問題......非常感謝! – caarlos0

+0

這裏的「魔術」似乎是use-fast-fail = true,我想知道爲什麼我需要所有其他人......你能解釋一下你的配置的基本原理嗎? – caarlos0

+1

我添加了關於不同參數的評論。 – teacurran

1

我想你錯過:<check-valid-connection-sql>select 1</check-valid-connection-sql><validation>部分

PS

PostgreSQLValidConnectionChecker.isValidConnection向空查詢到的Postgres stmt.execute("");我覺得Postgres的司機剛igno重新吧。 XA連接很可能會發送一些用於支持XA事務的系統SQL語句並獲取SQLException。