使用postgresql

时间:2017-11-22 15:57:50

标签: java postgresql connection pool wildfly-swarm

我正在使用postgres,wildfly-swarm,hibernate,我的交易是bean管理的。

my project-defaults.yml datasources:data-sources:part

 Adadawdadsa:
   driver-name: postgresql
   connection-url: dadadasdadas
   user-name: aawdwaasda
   password: waddsawdas
   jta: true
   use-java-context: true
   pool-name: awdasdadwaadads
   min-pool-size: 10
   initial-pool-size: 15
   max-pool-size: 40
   pool-prefill: false
   validate-on-match: true
   use-fast-fail: false
   connection-checker-class-name: org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker
   check-valid-connection-sql: "SELECT 1;"
   exception-sorter-class-name: org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter
   connection-properties: CharacterEncoding/ UTF-8 UseUnicode/true

模拟应用程序和postgres之间的连接丢失(关闭数据库,iptables丢包等,然后再做一些请求,然后再次启动数据库或清除iptables丢弃规则),数据库启动但是Hibernate / swarm无法找到一个连接... 有人能告诉我我做错了什么吗?

编辑:添加堆栈跟踪

2017-11-23 15:16:03.180 ERROR [PgSinkTask.paymentResultToJPA.0] PgSinkTask.paymentResultToJPA.0:重试提交时出现异常:: javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法获取JDBC连接 2017-11-23 15:16:03.180 ERROR [PgSinkTask.paymentResultToJPA.0] PgSinkTask.paymentResultToJPA.0:重试提交异常: javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法获取JDBC连接     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1171)     at com.scheidtbachmann.fcs.idbt.datamart.sink.JPASinkTask.setConnectorStatus(JPASinkTask.java:297)     在com.scheidtbachmann.fcs.idbt.datamart.sink.JPASinkTask.errorHandler(JPASinkTask.java:423)     在com.scheidtbachmann.fcs.idbt.datamart.sink.JPASinkTask.flush(JPASinkTask.java:328)     在com.scheidtbachmann.fcs.idbt.datamart.sink.JPASinkTask.getRecord(JPASinkTask.java:182)     在com.scheidtbachmann.fcs.idbt.datamart.sink.JPASinkTask.run(JPASinkTask.java:144)     在java.lang.Thread.run(Thread.java:748)     在org.glassfish.enterprise.concurrent.ManagedThreadFactoryImpl $ ManagedThread.run(ManagedThreadFactoryImpl.java:250) 引起:org.hibernate.exception.GenericJDBCException:无法获取JDBC连接     在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)     在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)     在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)     at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:90)     at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:112)     at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47)     在org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ 5.doPrepare(StatementPreparerImpl.java:146)     at org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)     在org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)     在org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1929)     在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)     在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876)     在org.hibernate.loader.Loader.doQuery(Loader.java:919)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:306)     在org.hibernate.loader.Loader.loadEntity(Loader.java:2204)     在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60)     在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:50)     在org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3967)     在org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)     在org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)     在org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)     在org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)     在org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)     在org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)     在org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)     在org.hibernate.internal.SessionImpl.access $ 2600(SessionImpl.java:164)     at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.load(SessionImpl.java:2696)     在org.hibernate.internal.SessionImpl.get(SessionImpl.java:980)     at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:290)     在org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)     at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)     在org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:840)     在org.hibernate.internal.SessionImpl.merge(SessionImpl.java:822)     在org.hibernate.internal.SessionImpl.merge(SessionImpl.java:827)     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)     ...省略了7个常见帧 引起:java.sql.SQLException:javax.resource.ResourceException:IJ000460:检查事务时出错     在org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)     at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:66)     at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)     at org.hibernate.internal.AbstractSessionImpl $ NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386)     at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:87)     ...省略了39个常用帧 引起:javax.resource.ResourceException:IJ000460:检查事务时出错     at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:424)     在org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:747)     在org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:138)     ...省略了43个常用帧 引起:javax.resource.ResourceException:IJ000459:事务处于非活动状态:tx = TransactionImple< ac,BasicAction:0:ffffc0a83842:6e69d97c:5a16d4f3:18状态:ActionStatus.ABORTED>     at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:408)     ...省略了45个常用帧

编辑:添加了persistence.xml

<persistence-unit transaction-type="JTA" name="dadsadsadsa">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/dadsadsadsaas</jta-data-source>
    <properties>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.id.new_generator_mappings" value="false"/>

        <property name="db.migration.action" value="validate-migrate"/>
        <property name="db.migration.sql.prefix" value="adadsadsadsa-"/>
    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:0)

  

Hibernate的内部连接池算法是基本的,并且是为了开发和测试目的而提供的。使用第三方池可获得最佳性能和稳定性。

有关详细信息,请查看Reference regarding connection pooling。我发现它很有用。

-

Hibernate中的C3P0连接池(配置)

  1. 获取hibernate-c3p0.jar
    要将c3p0与Hibernate集成,需要hibernate-c3p0.jar,从JBoss存储库获取它。 的pom.xml

                 JBoss存储库         http://repository.jboss.org/nexus/content/groups/public/     

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    
    <!-- Hibernate c3p0 connection pool -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    

  2. 配置c3p0属性 要配置c3p0,请将c3p0配置详细信息放在“persistence.xml”中,如下所示:

    ...
    <properties>
      <!-- Configuring JDBC properties -->
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_example" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="my_root_password" />
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    
      <!-- Hibernate properties -->
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="validate" />
    
      <!-- Configuring Connection Pool -->
      <property name="hibernate.c3p0.min_size" value="5" />
      <property name="hibernate.c3p0.max_size" value="20" />
      <property name="hibernate.c3p0.timeout" value="500" />
      <property name="hibernate.c3p0.max_statements" value="50" />
      <property name="hibernate.c3p0.idle_test_period" value="2000" />
    </properties>
    

  3. 也许c3p0 - JDBC3 Connection and Statement Pooling参考也会有所帮助。