Persistence.createEntityManagerFactory takes ages

柔情痞子 提交于 2019-12-25 01:48:31

问题


I have a DBUnit test which runs OK, but is taking ages (4-5 mins) to create the entity manager factory. I am using JPA with hibernate and SQL serve. Would be of great help if anybody could throw some light on this. My machine seems faster to blame it on Sql server :) Here is my setup code.

@BeforeClass
public static void initEntityManager() throws Exception {
    emf = Persistence.createEntityManagerFactory("primary");
    em = emf.createEntityManager();
    tx = em.getTransaction();

    connection = new DatabaseConnection(((EntityManagerImpl) em).getSession().connection());
    dataset = getDataSet();
}

And here is my Persistence.xml

<persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.prototype.database.Customer</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <!-- Properties for Hibernate -->
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
        <property name="hibernate.connection.username" value="testuser" />
        <property name="hibernate.connection.password" value="testuser" />
        <property name="hibernate.connection.url" value="jdbc:sqlserver://localhost:1433;DatabaseName=testdb"/>
    </properties>
</persistence-unit>

回答1:


Finally managed to bring down the EntityManagerFactory operation time from around 255 seconds to about 3 seconds on an average. Upgraded the hibernate-entitymanager from 3.4.0.GA to 3.6.3.Final and voila! Unit test runs like a unit test now, just under 6 secs. Will try to seeek an answer for this improvement for my knowledge.




回答2:


I would recommend you trying to find where the bottleneck is by:

1-Connecting to the SQLServer using a terminal.

2-Modifying the connection to link to a different database (MySQL, PostgreSQL, H2,...).

If both run smoothly, then the problem is in your configuration, and I can't help you any further, as long as I am not quite experienced with JPA.



来源:https://stackoverflow.com/questions/7966281/persistence-createentitymanagerfactory-takes-ages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!