EclipseLink连接超时

问题描述:

这应该是一个老问题,但是我进行了很多搜索,没有发现有用的信息。
我的主要问题是,当连接断开时,Java swing应用程序将冻结。必须存在连接超时,以便应用程序通知用户。

This should be an old question, but I searched a lot and found nothing useful. My main problem is that when the connection losts, my Java swing application freezes. There must be a connection timeout so that the application informs the user.

我尝试了,但它们没有帮助。

I tried this and this but they wasn't helpful.

这真的是EclipseLink的弱点还是我无法弄清楚?

Is this really a weakness in EclipseLink or this is something I can't figure out?

编辑后:我如何通过EclipseLink连接:

Edited: Here is how I connect via EclipseLink:

private static javax.persistence.EntityManager em = null;
public static javax.persistence.EntityManager getEntityManager()
{
    if (em == null || !em.isOpen())
    {
        try
        {
            em = Persistence
                  .createEntityManagerFactory("persistenceUnitName", getPersistenceConfig()).createEntityManager();
        } catch (Exception e)
        {
           //log
        }
    }
    return em;
}

这是我的persistence.xml

And this is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="persistenceUnitName" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.entities.MyEntity</class>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>

秋千和 em 在同一线程中, em 无法检索数据,因此秋千正在等待 em 并冻结。 / p>

The swing and em are in the same thread, em cannot retrieve the data, so the swing is waiting for emand it freezes.

您的应用程序挂在哪里?请中断或调试该过程并获取堆栈转储。

Where does your application hang? Please break or debug the process and get the stack dump.

如果查询挂起,则可以设置查询超时,这可能会有所帮助。

If it hangs on a query, you can set a query timeout that may help.

很有可能挂在JDBC驱动程序上,JPA无法执行任何操作。检查JDBC驱动程序中是否有任何设置可以防止挂起。

It is most likely hanging on the JDBC driver, there is nothing JPA can do. Check if there is any setting in your JDBC driver to prevent hanging.