Hello.
i'm relative new to JPA/Hibernate and therefore have following problem:
I leech files, convert em to csv, and load them into a database.
For each leeched file i create a status-object, which holds a list of log-objects. After each step in the process, i create a log-object with some infos and add it to the log-object list within the status-object (each file gets its own status-object).
so on each new log-object within the list, the status-object should be persisted in the database.
so if my app crashes/ a power blackout occurs / a meteor hits the server / etc. ;) there should be the last step logged in the database.
i act as follows:
i create an EntityManager:
Code:
EntityManagerFactory emf;
EntityManager em;
public void init(String unitName)
{
emf = Persistence.createEntityManagerFactory(unitName);
em = emf.createEntityManager();
}
Leech a file and after its local, create a status-object WITHOUT logs and persist it.
Code:
public void persist(Object o)
{
em.persist(o);
em.flush();
}
After this is done, i start processing the file and after each step i'm creating a log-object and add it to the logs-list within the status-object and call em.flush()
but the flush() saves the changes non-permanent, if my app crashes after the flush() no log would be in the database.
I have to call em.commit() after EACH change in the statusobject (e.g. adding a log) to make it persistent in the DB.
But commit() closes the transaction and i have to reconnect to the DB, search the status-object, add a new log and commit() again, and this for each change/log entry, IMHO thats way to expensive.
I have the feeling that there has to be another solution, i looked up the session-thing, but hibernate just describes making all changes and then commit in the end.
This makes sense in most cases, rollback clears all up etc. But i explicitly want EVERY change directly written into the database.
I would be glad if someone could point me into the right direction :), thanks for it in advance, and thanks for reading to this point :)
sincerely yours,
chillum