2

I am trying to create a Java application that will migrate an old schema to a new schema. I've chosen Java for the following reasons:

  • I need to perform data sanitation.
  • Business logic needs to be done to default previously null/bad data.
  • We are using JPA for our server code anyway. Using this in a migration will ensure our data migration has the correct integrity constraints.

My problem is; that when I increase the number of JPA Entity classes it takes a very long time to instantiate the EntityManager. Is there anything I can do to speed this up?

persistence.xml

<?xml version="1.0" encoding="UTF-
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="puMigration" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.myCompany.MyEntity</class>
    . . .
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
  </persistence-unit>
</persistence>

Java Code

Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
properties.put("javax.persistence.jdbc.url", url);
properties.put("javax.persistence.jdbc.user", username);
properties.put("javax.persistence.jdbc.password", password);

// TODO Refactor, this call takes 2 minutes.  Why?
EntityManagerFactory emf = Persistence.createEntityManagerFactory("puMigration", properties);
entityManager = emf.createEntityManager();
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
CodeMonkey
  • 160
  • 9
  • 1
    You will have to look for tools that can analyse what is taking the time. EclipseLink lazily loads persistence units, so you can control when this processing occurs using the eclipselink.deploy-on-startup property, but this will just move the processing to the Factory creation instead of the first EntityManager created. 2 minutes might not be excessive - it all depends on your machine, number of entities, resources and config options. Are you using weaving for instance? – Chris Nov 24 '14 at 17:33
  • you mean EntityManagerFACTORY creation is slow, as opposed to what the question title says – Neil Stockton Nov 24 '14 at 18:22
  • @NeilStockton - You are correct, the EntityManagreFactory is the line that is taking 2 minutes. – CodeMonkey Nov 24 '14 at 18:31

0 Answers0