spring - how to keep jpa session in the thread -


now use jpa in web application. , have class this:

class {     ...      @onetomany     private list<b> list;     ... } 

when in http request, can use a.getlist() successful. in schedule thread, throws exception:

org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: a.list, not initialize proxy - no session 

can keep session in schedule thread http request thread?

actually, when spring handle http request, start transaction interceptor org.springframework.orm.hibernate3.support.opensessioninviewinterceptor or filter org.springframework.orm.hibernate3.support.opensessioninviewfilter. if want keep session, can start , commit transaction our self.

here code:

public class backendthread {      @autowired     private platformtransactionmanager platformtransactionmanager;      @override     public void onmessage(message message, byte[] pattern) {     new transactiontemplate(platformtransactionmanager).execute(new transactioncallback<void>() {         @override         public void dointransaction(transactionstatus transactionstatus) {             ...             code here             ...             return null;         }     });     }  } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -