1

I'm using JPA EntityManager to insert items into a MySQL database, I have code that reads files, collects data, and then adds that data to the appropriate file. What I'm writing is a simplified version of what I'm trying to do, and I appreciate 100% that both makes it harder to understand what I'm actually doing and could conceal the source of the error, so thank you for bearing with me. The object p I use is generated earlier, and is a LinkedHashMap of form

LinkedHashMap<String MyObject>

Where "MyObject" is a class that has several attributes like "name", "weight", "color", and the String is a UUID meant to serve as the primary key.

private final EntityManagerFactory emf = Persistence.createEntityManagerFactory(myUI.PERSISTENCE_UNIT);
private final EntityManager em = emf.createEntityManager();

for (String key : p.keySet()) {
    databaseWriter.writeToDatabase(em, key, p.get(key).getName(),
    p.get(key).getColor(), p.get(key).getWeight();
}

And the relevent code from "databaseWriter.writeToDatabase" is

public void writeToDatabase (EntityManager em, String id, String name,String Color, String weight) {
    em.GetTransaction().begin();
    MyObjectTable mt = new MyObjectTable();
    mt.setId(id);
    mt.setColor(color);
    mt.setWeight(weight);
    mt.setName(name);
    em.persist(mt);
    em.getTransaction().commit();
}

I've confirmed I'm generating the objects in the correct form and passing the information to the function correctly, but I'm having the weirdest behavior. Say there are 15 items in "p", It will only add a few of them, and its not consistent about which few it adds, and its not even consistent about how many "a few" is. I'm hoping its because I'm doing something stupid with EntityManager that is apparent here.

If you need more details than this, I'd be happy to provide them, I just can't think what would cause only some objects to be added (and those objects that are added have the correct information), but only a particular few, not the same few on subsequent attempts, and no errors popping up at all.

Edit: The jpa provider I am using is EclipseLink and I have eddited the factory with something I missed

Edit: here is an example an eclipse server log of a piece of data being entered that does not show up in the databse, can anyone glean anything from this?

[EL Finest]: query: 2016-09-14 12:18:23.867--UnitOfWork(1915196068)--Thread(Thread[http-nio-8084-exec-252,5,main])--Execute query InsertObjectQuery(com.myfiles.stuff_the.PartTable[   idPart=99a26bca-d818-4959-a328-75c1f05a1e38 ])
[EL Fine]: sql: 2016-09-14 12:18:23.867--ClientSession(1291663611)--Connection(1232332820)--Thread(Thread[http-nio-8084-exec-252,5,main])--INSERT INTO PartTable (idPart, dateCreated, description, hash, isBasic, lastModified, name, riskGroup, authorId, formatId, nucseqId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [99a26bca-d818-4959-a328-75c1f05a1e38, 2016-09-14 12:18:23.82, , null, 1, 2016-09-14 12:18:23.82, AXL2 mRNA, null, 1, null, d6f807f0-ac91-4421-8ff2-4940706fd5bb]

I have looked through the whole log file, and can't find any errors, there are the correct number of persist() and commit() statements, not really sure what's going on.

mstorkson
  • 1,130
  • 1
  • 10
  • 26
  • 1
    thats what a JPA provider log is for – Neil Stockton Sep 14 '16 at 15:24
  • I'm new to using JPA, what is a provider log and how do I view it? – mstorkson Sep 14 '16 at 15:29
  • You chose some JPA implementation (Hibernate, EclipseLink, DataNucleus, OpenJPA ?) to be your JPA provider. Their software creates a log. Look in the documentation for the software that you selected. – Neil Stockton Sep 14 '16 at 15:32
  • Please specifiy the [persistence provider](http://stackoverflow.com/questions/27420513/what-is-jpa-provider). We can't help you without knowing the provider you are using. Another thing, you are creating a persistence factory without pass a persistence context name. Do you have a persistence.xml file? – cdaiga Sep 14 '16 at 15:34
  • Added, sorry I have to be parsimonious with this, I know it makes me more difficult to help, but I can't really paste the whole thing here for several reasons. And yes there is a persistence.xml file – mstorkson Sep 14 '16 at 15:48
  • You don't need to post the whole thing. You should be looking for your persists coming in, and SQL being issued. You can add logging in your code that will appear in the same log so you can see when things are fired off to see whether some aren't – Neil Stockton Sep 14 '16 at 17:14
  • I have the logs being generated now, but its a giant wall of text. I can find problem entries and see them being worked on, but I don't know what to look for to say "hey this failed" vs "hey this worked". – mstorkson Sep 14 '16 at 17:18
  • Are you sure there actually are *n* entries in your map? How often is `writeToDatabase` actually called? (Also, consider iterating over `Map.entrySet()` instead of the key set.) – JimmyB Sep 14 '16 at 20:37
  • i've done tests where I added an increment counter to the loop, and it has been called as many times as there are items I expect. – mstorkson Sep 14 '16 at 20:49

1 Answers1

0

It turns out the problem was not having the following line

em.clear();

after I used commit

mstorkson
  • 1,130
  • 1
  • 10
  • 26
  • you would have to check session flush as well in the JPA. FlLushing strategy is also very important. – Acewin Sep 21 '16 at 23:04