【问题标题】:Jboss AS7 connection pool won't reconnectJboss AS7 连接池不会重新连接
【发布时间】:2015-11-07 19:08:33
【问题描述】:

我的standalone.xml 中有以下配置:

<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 无法重新连接,我必须重新启动应用服务器。

但是,如果我使用org.postgresql.xa.PGXADataSource 驱动程序将datasource 更改为xa-datasource(保持示例中的配置),它可以工作

问题是:我无法理解这一点。如果我错了,请纠正我,但xa-datasources 应该用于在多个数据库中同步提交,而这里不是这种情况。我实际上配置了多个数据库,但我不需要在它们之间同步事务。

“默认”datasource 似乎在调整连接池大小方面也存在问题。有时,与应用程序的负载无关,它会打开超过 100 个连接(即使限制为 100)并在几秒钟后关闭它们。这很难重现 - 因为它似乎是随机的,所以我不能确定切换到 xa-datasource 也可以解决这个问题。

现在:

  • 为什么切换到 xa-datasource 有效?
  • 这样做有什么影响?
  • 为什么连接池会这么疯狂?

澄清一下,我的测试包括:

  1. 启动 postgres 和应用服务器;
  2. 向应用程序发出一些请求;
  3. 停止数据库;
  4. 向应用程序发出一些请求 - 并查看它们没有工作,因为它无法打开任何连接;
  5. 再次启动数据库;
  6. 向应用程序发出一些请求

在最后一步,xa-datasource 可以与 postgres 重新连接,一切正常。 datasource 不能,并且永远失败,加载无关紧要 - 我必须重新启动应用服务器。

【问题讨论】:

    标签: java postgresql jboss7.x


    【解决方案1】:

    配置 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>
    

    【讨论】:

    • 这里的“魔力”似乎是 use-fast-fail=true,我想知道为什么我需要所有其他的...你能解释一下你的配置的基本原理吗?
    • 我添加了关于不同参数的cmets。
    • 谢谢!我测试了一些组合,不需要使用use-fast-fail,关闭连接只需要几秒钟,但会起作用。谢谢!
    • 当 background-validation 设置为 true 时,不应该同时使用 use-fast-fail
    • @midTIerDeveloper 为什么不呢?
    【解决方案2】:

    我想你错过了:&lt;validation&gt; 部分中的&lt;check-valid-connection-sql&gt;select 1&lt;/check-valid-connection-sql&gt;

    PS

    PostgreSQLValidConnectionChecker.isValidConnection 向 postgres stmt.execute(""); 发送空查询我认为 postgres 驱动程序只是忽略它。 XA 连接很可能会发送一些支持 XA 事务的系统 SQL 语句并获得 SQLException。

    【讨论】:

      猜你喜欢
      • 2010-11-11
      • 1970-01-01
      • 2011-12-14
      • 2012-03-03
      • 2011-12-25
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      相关资源
      最近更新 更多