java - Could not initialize proxy - no Session -


i've got error looks this:

could not initialize proxy - no session

i'm working java, hibernate , spring. error comes when trying generate pdf document, , i'm following next steps generate on fly , store in database.

  1. i sent request app through post method. generates pdf on fly , shows user.

  2. just after request send another, through ajax request. generate same pdf save in db.

the error shows query not executed due "could not initialize proxy - no session" error.

is there doing wrong, calling same methods twice same user session? session closed before both requests have finished?

hope can me understand happening.

your problem hibernate session lives 1 request. opens in start of request , closes @ end. guessed answer: hibernate session closed before both requests finished.

exactly happening? entity objects live during both requests. how? stored in http session (which different thing called session) don't give information framework using, can't give more details, framework using somehow keeps entities in http session. how framework makes easy work same objects more 1 requests.

when processing of second request starts, code trying access entity (usually element of collection) lazily initialized hibernate. entity not attached hibernate session, , hibernate can't initialize hibernate proxy before reading it. should open session , re-attach entity @ beginning of ajax request processing.

edit:

i try give brief explanation of happening behind scene. java web frameworks have 1 or more servlets handle requests. servlet handles each request (httprequest) creating new thread produce response (httpresponse). method processes each request executed inside thread.

at beginning of request processing application should allocate resources needs processing (transaction, hibernate session etc). @ end of processing cycle these resources released (transaction committed, hibernate session closed, jdbc connections released etc). lifecycle of these resources managed framework, or done code.

in order support application state in stateless protocol http, have httpsession object. (or frameworks) put on httpsession information remains relevant between different request cycles of same client.

during processing of first request hibernate reads (lazily) entity database. due lazy initialization parts of object's structure hibernate proxy objects. these objects associated hibernate session created them.

the framework finds entity previous request in httpsession object when try process second request. trying access property child entity lazily initialized , hibernate proxy object. hibernate proxy object imitation of real object ask hibernate session fill information database when tries access 1 of properties. hibernate proxy trying do. session closed @ end of previous request processing, doesn't have hibernate session use in order hydrated (filled real info).

note possible have opened hibernate session @ beginning of second request, isn't aware of entity contains proxy object because entity read different hibernate sesion. should re-attach entity new hibernate session.

there lot of discussion how re-attach detached entity, simplest approach right session.update(entity).

hope helps.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -