Persistence.createEntityManager() painfully slow with Hibernate
I'm having huge performance issues with my Eclipse RCP application, apparently caused by Hibernate. To be more specific, the application takes extremely long to start up (up to 2 minutes) - profiling and debugging showed that creating a Hibernate/JPA EntityManagerFactory (which happens at startup) takes extremely long.
I've played around a lot with the settings, such as using a file database rather than in-memor开发者_如何学编程y and not using hbm2ddl, but without success. Has anyone ever experienced problems like these?
I'm using JDK 1.6.20, Hibernate 3.5 and HSQLDB 1.8 (in-memory configuration).
Below is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="my_db" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- about 20 classes; skipped for brevity -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:my_db"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
Depending on how many entities you have, it may take some time to create all the tables, indexes and alikes. I'd change to another DB (or non in-memory db), replace the create-drop to "create", run it once, remove the property entirely (so that it doesn't try to create the already existing tables) and run it again. This last step is closer to the actual time spent by Hibernate than your current configuration.
Such a long delay could also be a sign of a network timeout. According to this answer, hibernate should validate the persistence.xml against an embedded copy of the xml schema. Maybe it is for some reason trying to fetch the xsd from the internet. Can you get a thread dump or a more detailed profile where the entity manager creation is stuck?
精彩评论