Hello Cyclos team!
We have a scheduled job, whose job it is to sync users (~600) from an external system into cyclos.
Evolution
It is working so far: Creating new users that dont already exist, updating existing ones. But especially creating new users seems to consume a lot of memory: After importing many new users the script throws java.lang.OutOfMemoryError. So I limited it to import at max 100 Users, but update all existing. Interestingly this works, even when after ~6 runs all users are imported, it is no problem to update all 600.
But as soon as I turned that script from manually running ("run script") to an actual scheduled task it again crashed with OutOfMemoryError when I executed the task manually, even with < 100 new users.
I already discovered that userService.importUser() persists but does not write to the database, e.g. following searches for that new username turn up empty but then will fail the next importUser() call with a collision.
So I thought, if I flushed the entityManager after each user, I could a) save memory and b) prevent those stale searches. But calling entityManager.flush() seems to do nothing: The script still crashes, and when it crashes, no imported user is actually in the database. All db actions seem to be encapsulated into one transaction, ignoring the flush.
Questions
So why is entityManager.flush() not actually persisting into the database? What is the correct way to do this, so that later UserQuery() usages actually include those new records?
And how do I reduce memory usage, should I use entityManager.detach() in the loop after each user import/update?
Thanks in advance,
Jakob
How to flush in an import script?
Moderators: alexandre, hugo, rmvanarkel
Re: How to flush in an import script?
Which Cyclos version are you using?
Anyway, the problem is likely that entityManager.flush() just writes loaded entities that had any fields changed to the database, but the entities are still loaded in memory, together with all its dependencies.
The best way to work is to work with a CacheFlusher.
Anyway, the problem is likely that entityManager.flush() just writes loaded entities that had any fields changed to the database, but the entities are still loaded in memory, together with all its dependencies.
The best way to work is to work with a CacheFlusher.
Code: Select all
import org.cyclos.impl.InvocationContext
def cacheFlusher = InvocationContext.newCacheFlusher()
for (user in usersToImport) {
// Import the user
// Flush the cache
cacheFlusher.flush()
}Luis Fernando Planella Gonzalez
Cyclos development team
Cyclos development team
-
jakob.schumann
- Posts: 63
- Joined: Thu Apr 23, 2020 5:37 pm
Re: How to flush in an import script?
Thank you Luis, this works!