web-dev-qa-db-ja.com

org.hibernate.exception.GenericJDBCExceptionの発生:長時間実行されているプロセスの接続を開くことができませんでした

私のアプリケーションでは、ジョブに20スレッドを使用します。各スレッドはリモートWebサービスを呼び出し、Or​​acleDBを更新します。 Jbossの再起動後、ログで次のように検出された場合を除いて、ジョブはデータベースの更新に失敗します。

2017-11-08 23:36:20,706 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 21) javax.resource.ResourceException: IJ000460: Error checking for a transaction
2017-11-08 23:36:20,706 ERROR [org.jboss.as.ejb3] (EJB default - 16) javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Could not open connection
2017-11-08 23:36:20,707 ERROR [org.jboss.as.ejb3] (EJB default - 20) javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Could not open connection
2017-11-08 23:36:20,707 ERROR [org.jboss.as.ejb3] (EJB default - 19) javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Could not open connection
2017-11-08 23:36:20,710 ERROR [org.jboss.as.ejb3] (EJB default - 21) javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Could not open connection
2017-11-08 23:36:20,711 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 16) JBAS014134: EJB Invocation failed on component PersonServiceDaoImpl for method public abstract long com.kaviletta.re.caltek.db.dao.PersonServiceDao.getNotUpdatedAddressCountBySubmissionId(int): javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.Java:162) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.Java:252) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.Java:341) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.Java:238) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.Java:41) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.Java:64) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.Java:59) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.Java:50) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.Java:55) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.Java:45) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.Java:61) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.Java:165) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.Java:182) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.Java:61) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.Java:72) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at com.kaviletta.re.caltek.db.dao.PersonServiceDao$$$view6.getNotUpdatedAddressCountBySubmissionId(Unknown Source) [classes:]
    at com.kaviletta.re.caltek.rest.service.impl.AsynchronousServiceImpl.asyncAddressDetails(AsynchronousServiceImpl.Java:61) [classes:]
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_55]
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57) [rt.jar:1.7.0_55]
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) [rt.jar:1.7.0_55]
    at Java.lang.reflect.Method.invoke(Method.Java:606) [rt.jar:1.7.0_55]
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.Java:72) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.Java:53) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.Java:58) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.Java:53) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.Java:58) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.Java:43) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.Java:47) [jboss-as-jpa-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.Java:21) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.Java:61) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.Java:53) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.Java:51) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.Java:272) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.Java:339) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.Java:238) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.Java:41) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.Java:64) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.Java:59) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.Java:50) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.Java:55) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.Java:45) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.Java:61) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.Java:165) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.Java:182) [jboss-as-ee-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.AsyncFutureInterceptorFactory$1$1.runInvocation(AsyncFutureInterceptorFactory.Java:89) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at org.jboss.as.ejb3.component.interceptors.AsyncInvocationTask.run(AsyncInvocationTask.Java:73) [jboss-as-ejb3-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145) [rt.jar:1.7.0_55]
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615) [rt.jar:1.7.0_55]
    at Java.lang.Thread.run(Thread.Java:745) [rt.jar:1.7.0_55]
    at org.jboss.threads.JBossThread.run(JBossThread.Java:122)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.Java:1387) [hibernate-entitymanager-4.2.7.SP5-redhat-1.jar:4.2.7.SP5-redhat-1]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.Java:1310) [hibernate-entitymanager-4.2.7.SP5-redhat-1.jar:4.2.7.SP5-redhat-1]
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.Java:316) [hibernate-entitymanager-4.2.7.SP5-redhat-1.jar:4.2.7.SP5-redhat-1]
    at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.getSingleResult(QueryNonTxInvocationDetacher.Java:69) [jboss-as-jpa-7.3.4.Final-redhat-1.jar:7.3.4.Final-redhat-1]

データベース操作のためにスレッドによって使用されるコード:

    @PersistenceContext
EntityManager entityManager;
protected EntityManager getEntityManager() {
    return entityManager;
}

protected void setEntityManager(EntityManager manager) {
    this.entityManager = manager;
}
 public long getNotUpdatedAddressCountBySubmissionId(int submissionId) {
            Query q = getEntityManager().createQuery(
                    "select count(address.addressId.submissionId) from AddressStruc address where processState!=1  and address.addressId.submissionId = :submissionId");
            q.setParameter("submissionId", submissionId);
            return (long) q.getSingleResult();

        }

        public void updateStudentdAddress(List<AddressStruc> studnetsAddressDetails) {
            try {
                for (AddressStruc studentdAddress : studnetsAddressDetails) {
                    AddressStruc addressDetails = entityManager.find(AddressStruc.class, new AddressId(
                            studentAddress.getAddressId().getSubmissionId(), studentAddress.getAddressId().getRowId()));
                    if (addressDetails != null) {
                        entityManager.merge(updateStudentDetails(addressDetails, studentAddress));
                        entityManager.flush();
                    }

                }
            } catch (Exception e) {
                log.error("Exception occured while updating Address(method-->updateStudentAddress))-->" + e);
            }
        }

UpdateStudentAddressメソッドを呼び出すAsynメソッド

public AsyncResult<StudentStatusModel> asyncAddressDetails(int startRow, int noOfRecord, int submissionId) {
    // method for fetching record based on the start row provided
    Collection<AddressStruc> addressList = studentEjb.getAddressBySubmissionId(startRow, noOfRecord, submissionId);
    StudentStatusModel status = new StudentStatusModel();
    status.setStartRow(startRow);
    status.setRetry(false);
    status.setMessage(null);
    // invoke the webservice and generate student for the given address
    if (StudentUtil.webServiceFailed <= 5) {
        OutputModel outputModel = invokeWebservice(addressList, status);

        if ((outputModel != null) && (outputModel.getStudentModels().size() > 0)) {
        // update the address details based on the submissionId+rowId
        studentEjb.updateStudentAddress(new ArrayList<>(convertModelToEntity(outputModel).values()));
    }
}
return new AsyncResult<StudentStatusModel>(status);
}

Db変換にコンテナ管理エンティティマネージャを使用しています。

各スレッドはupdateStudentAddressメソッドを呼び出し、100レコードを更新します。

このジョブは長時間実行されるため、しばらくするとjbossはOracleとそれ以上接続できなくなります。

この問題を修正する方法はありますか?

8

この問題は、内部の例外処理が原因である可能性があると思います:asyncAddressDetails

RuntimeExceptionまたはApplicationExceptionが発生した後、jboss7でこのエラーが発生し、この例外がキャッチされたときにトランザクションのロールバックが発生し、アプリケーションコードは後で現在のトランザクションコンテキストが終了する前にEntityManagerでさらに操作を実行しました。

例:

void methodInNewTransaction() {
  try {
    dao.inRequiredTransaction();
  } catch (RuntimeException ex) {
    ; // ignore it
  }
  dao.secondCallInRequiredTransaction();
}

secondCallInRequiredTransactionは、RuntimeExceptionが無視されたときにエラーを引き起こす可能性があります。

これを修正する方法、私は次の可能性を見ます:

  • 例外の発生を防ぐ
  • @ ApplicationException-Annotationを使用して、発生する例外のトランザクション効果を制御します
  • 例外を再スローして、外部のtransactioncontextが使用されないようにします
2
aschoerk

org.hibernate.exception.GenericJDBCException:接続を開くことができませんでした。コードを見せてもらえますか?同期スレッドの問題である可能性があります。または悪い実装。

dBに対して書き込もうとする場合は、オープントランザクションが必要です。例

employee = em.find(Employee.class, 1);

  em.getTransaction().begin();
  employee.setNickname("Joe the Plumber");
  entityManager.merge(employee);
  em.getTransaction().commit();
1
Jorge L. Morla

別の回答が指摘しているように、Hibernateでは永続性の例外をキャッチして無視することはできないため、ドキュメントでは明示的に禁止されています。しかし、あなたの問題は毎晩の再起動に関連していると思います。完全なログがないと確実に言うのは難しいですが、JBossが起動すると、一部の部分が他の部分よりも先に開始します。すべてが初期化されてからエラーが発生する前に、インバウンド要求を取得するのが一般的です。ある夜の再起動を防ぐことができれば、それでもエラーが発生するかどうかを確認できます。

修正(再起動しない場合を除く)は、コンポーネントが正しい順序で開始されるようにするか、再起動が完了するまで着信呼び出しを防止することです(ロードバランサーを通過する場合は簡単です)。

開始順序を制御するには、application.xmlを編集し、initialize-in-orderがtrueであることを確認します。次に、モジュールを目的の開始順序でリストします。 Web /ジョブ層は最後に開始する必要があります。

非常に汚い修正は、エラーをキャッチし、数秒間スリープしてから再試行することにより、コードでそれを処理することです。私はそれをした人を知っていますが、私はそれをお勧めしません。

1
ewramner