简体   繁体   中英

Hibernate and InterruptedException

I have a vaadin application which uses hibernate and uploaded in glassfish, and we're getting this error:

AdmissionWeb:2018-07-03 12:01:06 [WARN ][http-listener-2(15)]com.mchange.v2.resourcepool.BasicResourcePool: com.mchange.v2.resourcepool.BasicResourcePool@1c40470 -- an attempt to checkout a resource was interrupted, and the pool is still live: some other thread must have either interrupted the Thread attempting checkout!
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1414)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
    at org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:84)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1432)
    at sun.reflect.GeneratedMethodAccessor2260.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    at com.sun.proxy.$Proxy648.beginTransaction(Unknown Source)
    at no.solarsoft.venus2.admissionweb2.DatabaseUtil.getSession(DatabaseUtil.java:52)
    at no.solarsoft.venus2.admissionweb2.model.DataManager.getAdmission(DataManager.java:845)
    at no.solarsoft.venus2.admissionweb2.AdmissionWebUI.init(AdmissionWebUI.java:178)
    at com.vaadin.ui.UI.doInit(UI.java:675)
    at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:214)
    at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:253)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)]]

I don't know why this happens. Everything seemed to be normal.

Here's the hibernate.cfg.xml :

<hibernate-configuration>
    <session-factory>    
        <property name="hibernate.current_session_context_class">thread</property>        
        <property name="hibernate.c3p0.idle_test_period"> 120</property> <!-- In seconds -->
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>         
        <property name="hibernate.c3p0.timeout">3600</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="c3p0.acquire_increment">1</property>

We have a recursive method handlePayment() which calls itself every 10 seconds until we post a callback, the payment fails, or it times out (the timeout is set to 60 minutes.

private PxPaymentResponseData handlePayment(String paymentId, long timeoutTimestamp, boolean shouldPostCallback, String admissionId) throws Exception {
    List<String> nodesToExpand = Arrays.asList("urls", "transactions", "payeeInfo", "prices");
    PayExAccountV2 pxAccount = PayExUtil.findPayExAccountForAdmission(admissionId);
    PxPaymentResponse pxPayment = PxPaymentService.getPayment(paymentId, nodesToExpand, pxAccount.getToken());
    Optional<PxTransactionData> lastTx = PxPaymentUtil
            .findLastTransaction(pxPayment.getPaymentData().getTransactions().getTransactionList());

    if (lastTx.isPresent()) {
        PxTransactionData txData = lastTx.get();
        String state = txData.getState();

        if (PxTransaction.State.COMPLETED.value().equalsIgnoreCase(state)) {
            if (PxTransaction.Type.CAPTURE.value().equalsIgnoreCase(txData.getType())) {
                return pxPayment.getPaymentData();
            } else if (PxTransaction.Type.AUTHORIZATION.value().equalsIgnoreCase(txData.getType())) {
                if (shouldPostCallback) {
                    CompletableFuture.runAsync(() -> issuePayExCallback(pxPayment.getPaymentData(), txData, admissionId));
                }
                wait(pxPayment.getPaymentData().getId(), PAYEX_REQUEST_DELAY, timeoutTimestamp);
                return handlePayment(paymentId, timeoutTimestamp, false, admissionId);
            }
        } else if (PxTransaction.State.INITIALIZED.value().equalsIgnoreCase(state)) {
            wait(pxPayment.getPaymentData().getId(), PAYEX_REQUEST_DELAY, timeoutTimestamp);
            return handlePayment(paymentId, timeoutTimestamp, false, admissionId);
        } else if (PxTransaction.State.FAILED.value().equalsIgnoreCase(state)) {
            if (PxTransaction.Type.AUTHORIZATION.value().equalsIgnoreCase(txData.getType())
                    || PxTransaction.Type.CAPTURE.value().equalsIgnoreCase(txData.getType())) {
                handleFailedPayment(pxPayment.getPaymentData(), txData);
            }
        }
    } else {
        log.warn("Could not find any transactions for payment: " + paymentId);
    }
    throw new Exception("Payment not handled!");
}

the wait() method calls Thread.sleep().

private void wait(String paymentId, final int seconds, long timeoutTimestamp)
        throws Exception {
    if (isTimeoutElapsed(timeoutTimestamp)) {
        throw new PaymentTimeoutException("Payment processing session has timed out for payment " + paymentId);
    }

    try {
        Thread.sleep(seconds * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

I don't know if the error has something to do with the wait() method. I hope someone can help me. We have increased the max pool size in glassfish to 200, but it didn't help.

Thanks!

Your Glassfish configuration is set to interrupt Threads quicker than your defined sleep time.

See this one for more details: How to avoid Thread Interrupted Exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM