Persistence.createEntityManagerFactory takes ages

2019-08-27 11:13发布

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>

2条回答
Juvenile、少年°
2楼-- · 2019-08-27 11:17

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.

查看更多
▲ chillily
3楼-- · 2019-08-27 11:37

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.

查看更多
登录 后发表回答