Persistence.createEntityManagerFactory takes ages

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.

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

  • What is the default password for SQL Server 2012 if I didn't put a password in the setup?
  • Cannot access WSS 3.0 sites after movind Content and Config DBs
  • SQL Server FUNCTION overflow int vs bigint
  • Clustered index vs. Non-clustered index including ALL columns (SQL Server)
  • Eliminate Null Output row
  • SQL Server Reporting Services export to another server
  • <persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
            <!-- Properties for Hibernate -->
            <property name="" 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"/>

  • what does null entities are not supported by org.hibernate.event.def.EventCache mean?
  • 2 Solutions collect form web for “Persistence.createEntityManagerFactory takes ages”

    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.

    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.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.