问题
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.
Is this really a weakness in EclipseLink or this is something I can't figure out?
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;
}
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>
The swing and em
are in the same thread, em
cannot retrieve the data, so the swing is waiting for em
and it freezes.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/11718381/eclipselink-connection-timeout