Java web程序员偶尔会遇见此类问题:
could not initialize proxy - the owning Session was closed
1.
This error means that you're trying to access a lazily-loaded property or collection, but the hibernate session is closed. Lazy loading in Hibernate means that the object will not be populated (via a database query) until the property/collection is accessed in code. Hibernate accomplishes this by creating a dynamic proxy object that will hit the database only when you first use the object. In order for this to work, your object must be attached to an open Hibernate session throughout it's lifecycle. Check to see how you're managing your session. You may need to reattach objects to a new session if the session is being closed in some other place.
2.
attach
Purpose
Attaches a detached domain instance to the Hibernate session bound to the current thread
Examples
def b = Book.get(1) b.title = "Blah" b.save(flush:true)b.discard()
… if(!b.isAttached()) { b.attach() }
Description
Grails uses Hibernate which manages each persistent object in a persistence session. A new session is created per request and is closed at the end of the request. If an object is read from the session and placed into a web scope such as the HttpSession it is seen as detached, since the persistence session has been closed. You can use the attach() method to re-attach an existing persistent instance to the persistence session of the current request.