views:

33

answers:

1

I'm trying to implement the advice found in this great blog post for batch processing in grails with MySQL. The problem that I'm having is that inclusion of periodic calls to session.clear() in my loop causes org.hibernate.LazyInitializationException's to be thrown. There's a quote down in the comments section of the page:

You’re second point about potentially causing LIEs is absolutely true. If you’re doing other things outside of importing with the current thread, you’ll want to make sure to reattach any objects to the session after you’re doing your clearing.

But how do I do that? Can anyone help me specifically understand how to "reattach any objects to the session after I'm done clearing?

I'm also interested in parallelizing the database insertion process so that I can take advantage of having a multi core processor. Can anyone provide advice on how to do that in Grails?

+1  A: 

Grails has a few methods to help with this (they leverage hibernate under the covers).

If you know an object is detached, you can use the attach method to reconnect it.

If you've made changes to the object while it was detatched, you can use merge.

If for whatever reason, you're not sure if an object is attached to the session, you can use the link text method to find out if it is or isn't.

It might also be worth reviewing the Hibernate documentation on Session.

Ted Naleid
@Ted, thanks for the response. Do you have any advice on how I can determine what objects need to be reattached?
vicatcu
@vicatu unfortunately, it can be solution dependent, it depends on what objects you might have detatched during the clear(). The other thing you could try is that rather than doing a brute force clearing of the session, you could evict the specific domain objects you're working with on the session.Another thing that I haven't tried, is the new "withNewSession" method in GORM (http://www.grails.org/doc/latest/ref/Domain%20Classes/withNewSession.html). It wasn't around when I wrote the post originally, but might be a better way to wrap your transaction and clean up after.
Ted Naleid