Problems using groovy domain classes within Java - 'could not initialize proxy - the owning Session was closed'

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems using groovy domain classes within Java - 'could not initialize proxy - the owning Session was closed'

Jonny Cavell
I have a one-to-many relationship between a Brand and BrandDomain.

This code works fine within a Groovy controller:
...
def bd = BrandDomain.findByDomainName(host)
brand = bd.brand
def brandId = brand.id...

However, I want to have the Brand object available as a request attribute, so I decided to create a (Java) Filter which calls the above code (which is in a Groovy class, so it's called via a Java interface). So far so good, and the first 2 lines of code above work. However, when it comes to actually getting a value from the (lazily initialised) Brand object, I get this hibernate error: 'could not initialize proxy - the owning Session was closed'. The stack trace for this is:

....
[ERROR] 09:38:19 could not initialize proxy - the owning Session was closed at org.hibernate.LazyInitializationException.<init>(LazyInitializationException.java:19)
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
        at Brand$$EnhancerByCGLIB$$58910356.getProperty(<generated>)
        at org.codehaus.groovy.runtime.Invoker.getProperty(Invoker.java:213)
        at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:209)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:500)
        at UserBrandService.getBrandFromDomain(UserBrandService.groovy:63)
        at UserBrandService.this$2$getBrandFromDomain(UserBrandService.groovy)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
....

I can't see why I'd be able to get a BrandDomain object (and e.g. its name, id etc.), but on the next line not be able to get a Brand object from this.

Help!!

 
Reply | Threaded
Open this post in threaded view
|

Re: Problems using groovy domain classes within Java - 'could not initialize proxy - the owning Session was closed'

Jonny Cavell
An update on this:

I have found another place where this happens that doesn't involve Java. If I have a taglib which creates a BrandDomain object, then gets a Brand object from this BrandDomain object:

....
def bd = BrandDomain.findByDomainName(host)
brand = bd.brand
...

This works OK if I use the taglib that does the above in normal GSP pages, but if I use the same taglib in a GSP page within the layout area (i.e. a sitemesh layout file), I get the same error I reported before - 'could not initialize proxy - the owning Session was closed'.

I've attached the Brand and BrandDomain hibernate cfg files.

BrandDomain.hbm.xmlBrand.hbm.xml
Reply | Threaded
Open this post in threaded view
|

Re: Problems using groovy domain classes within Java - 'could not initialize proxy - the owning Session was closed'

graemer
Ok sounds like a bug, i think this is because the hibernate filter is
completing before the sitemesh filter. We're going to re-work this
soon anyway so hopefully it will be fixed for 0.6

Cheers

On 6/20/07, Jonny Cavell <[hidden email]> wrote:

>
> An update on this:
>
> I have found another place where this happens that doesn't involve Java. If
> I have a taglib which creates a BrandDomain object, then gets a Brand object
> from this BrandDomain object:
>
> ....
> def bd = BrandDomain.findByDomainName(host)
> brand = bd.brand
> ...
>
> This works OK if I use the taglib that does the above in normal GSP pages,
> but if I use the same taglib in a page in a layout GSP, I get the same error
> I reported before - 'could not initialize proxy - the owning Session was
> closed'.
>
> I've attached the Brand and BrandDomain hibernate cfg files.
>
> http://www.nabble.com/file/p11211848/BrandDomain.hbm.xml BrandDomain.hbm.xml
> http://www.nabble.com/file/p11211848/Brand.hbm.xml Brand.hbm.xml
> --
> View this message in context: http://www.nabble.com/Problems-using-groovy-domain-classes-within-Java---%27could-not-initialize-proxy---the-owning-Session-was-closed%27-tf3951322.html#a11211848
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problems using groovy domain classes within Java - 'could not initialize proxy - the owning Session was closed'

Jonny Cavell
WRT my problem with the hibernate session being shut when using Groovy domain objects in the Java Filter, do you consider this to be a Grails bug as well, or should I look to see if I can fix it? I guess it needs to have access to the SessionFactory via e.g. a Spring HibernateInterceptor, but can I reference the SessionFactory in resources.xml? Maybe through autowiring?

Thanks.

graemer wrote
Ok sounds like a bug, i think this is because the hibernate filter is
completing before the sitemesh filter. We're going to re-work this
soon anyway so hopefully it will be fixed for 0.6

Cheers

On 6/20/07, Jonny Cavell <jonny.cavell@fish4.co.uk> wrote:
>
> An update on this:
>
> I have found another place where this happens that doesn't involve Java. If
> I have a taglib which creates a BrandDomain object, then gets a Brand object
> from this BrandDomain object:
>
> ....
> def bd = BrandDomain.findByDomainName(host)
> brand = bd.brand
> ...
>
> This works OK if I use the taglib that does the above in normal GSP pages,
> but if I use the same taglib in a page in a layout GSP, I get the same error
> I reported before - 'could not initialize proxy - the owning Session was
> closed'.
>
> I've attached the Brand and BrandDomain hibernate cfg files.
>
> http://www.nabble.com/file/p11211848/BrandDomain.hbm.xml BrandDomain.hbm.xml
> http://www.nabble.com/file/p11211848/Brand.hbm.xml Brand.hbm.xml
> --
> View this message in context: http://www.nabble.com/Problems-using-groovy-domain-classes-within-Java---%27could-not-initialize-proxy---the-owning-Session-was-closed%27-tf3951322.html#a11211848
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email