出现 org.hibernate.exception.GenericJDBCException 错误:无法为长时间运行的进程打开连接。

8

在我的应用程序中,我使用20个线程来完成一个任务。每个线程调用远程Web服务并更新Oracle数据库。在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方法

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);
}

我正在使用容器管理的实体管理器进行数据库事务。

每个线程调用updateStudentAddress方法并更新100条记录。

由于这项工作需要长时间运行,一段时间后JBoss无法继续与Oracle建立连接。

有没有办法解决这个问题?


乍一看,我怀疑你的线程如何将连接释放回DataSource的连接池。Oracle有允许的连接数限制(默认为1000,据我所记)。因此,如果连接没有释放,则下一个线程从池中选择一个新连接,当达到最大值时-它会失败。您能调查一下获取该异常需要多长时间吗?有多少成功的调用通过了,以及您的Oracle的限制是什么。此外,请求完成后,您的线程如何处理?至少这是我首先要遵循的方式... - Vadim
这个异常是在午夜jboss重启后发生的,所以当作业正在运行时,jboss可能会重新启动,这会停止当前正在运行的线程,重启后将使用新线程恢复。 - Joby Wilson Mathews
你好,有关于这个问题的任何更新吗?是什么原因导致的呢?请问你是如何解决它的? - Bằng
3个回答

1

org.hibernate.exception.GenericJDBCException: 无法打开连接。

你能展示一下代码吗?可能是同步线程问题或实现不好导致的。

在写入数据库时,你需要开启事务。以下是一个例子:

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

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

好的,谢谢。您正在尝试对数据库进行事务处理,但您忘记打开事务。这可能是问题所在。 - Jorge L. Morla
我正在使用容器管理的实体管理器,因此我不需要开始一个事务。 - Joby Wilson Mathews

1

我认为问题可能是由asyncAddressDetails内部的异常处理引起的。

在jboss7中,当RuntimeException或ApplicationException导致事务回滚时,如果在当前事务上下文结束之前捕获了此异常并且应用程序代码稍后在EntityManager上执行了进一步操作,则会出现此错误。

例如:

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

当忽略RuntimeException时,secondCallInRequiredTransaction可能会导致错误。

如何解决这个问题,我看到以下几种可能性:

  • 防止异常发生
  • 使用@ApplicationException注释来控制发生的异常的事务效果
  • 重新抛出异常,以便不再使用外部事务上下文

你好,我遇到了同样的问题。我同意你的观点,因为在我的日志中也显示了 Caused by: org.jboss.util.NestedSQLException: Transaction is not activeCaused by: javax.resource.ResourceException: Transaction is not active,你是如何追踪导致问题的根异常的呢?你认为数据库中的 ORA-00060: deadlock detected while waiting for resource 是否可能导致问题?谢谢。 - Bằng
当出现ORA-00060错误时,当前事务至少会回滚所选的事务。因此,随后执行需要事务的JPA操作或Hibernate操作将导致您在此处看到的情况。 - aschoerk
请问您有没有调试ORA-00060错误的经验可以分享? - Bằng
数据库管理系统本身通常会记录导致死锁的语句。查看这些语句,您应该能够找出在您的应用程序中是罪魁祸首的代码。请向您的数据库管理员索取死锁相关信息。 - aschoerk

1
正如另一个答案所指出的那样,你可能永远无法用 Hibernate 捕获并忽略持久化异常,因为它在文档中明确禁止。然而,我认为你的问题与夜间重启有关。没有完整的日志很难确定,但当 JBoss 启动时,某些部分会比其他部分先启动。在一切初始化之前就收到入站请求是很常见的,然后就会出现错误。如果你可以阻止重启一晚,就可以查看是否还会出现错误。
解决方法(除了不重启)是确保组件按正确顺序启动,或者在重启完成之前防止传入呼叫(如果它们通过负载平衡器)。
要控制启动顺序,请编辑 application.xml 并确保 initialize-in-order 为 true。然后按所需的启动顺序列出模块。Web/作业层应该最后启动。
一个非常粗糙的解决方案是在代码中处理它,通过捕获错误,等待几秒钟,然后重试。我知道一些人这样做了,尽管我不建议这样做。

如果我阻止重启,错误就无法再现。 - Joby Wilson Mathews

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接