java - Spring + Hibernate: No Hibernate Session bound to thread -


i trying implement following: clear details db on springsecurity logout handler. main problem after trying user details db error. rest of code , same method work fine in other cases.

public class currentuserlogoutsuccesshandler extends simpleurllogoutsuccesshandler {      /**      *       */     @autowired     private requestsservice requestsservice;      /**      *       */     @autowired     private offersservice offersservice;      /**      *       */     @autowired     private usersservice usersservice;      /**      *       */     @override     public void onlogoutsuccess(httpservletrequest request, httpservletresponse response, authentication authentication)             throws ioexception, servletexception {         if (authentication != null) {             userdetailsextended details = (userdetailsextended) authentication.getprincipal();             user user = usersservice.get(details.getid()); // fails here              requestsservice.unlockallbybackoffice(user);             offersservice.unlockallbybackoffice(user);         }          setdefaulttargeturl("/");         super.onlogoutsuccess(request, response, authentication);     } } 

config:

<bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean">         <property name="datasource" ref="datasource" />         <property name="packagestoscan" value="com.ejl.butler.object.data" />         <property name="hibernateproperties">             <props>                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>                 <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>                 <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>            </props>         </property>     </bean>     <bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactory" />     </bean> 

dao:

public user get(final long id) {         session session = sessionfactoryutils.getsession(sessionfactory, false);          return (user) session.get(user.class, id);     } 

spring security config:

<logout invalidate-session="true" logout-url="/logout" success-handler-ref="logoutsuccesshandler"/> 

exception:

no hibernate session bound thread, , configuration not allow creation of non-transactional 1 here     @ org.springframework.orm.hibernate3.sessionfactoryutils.dogetsession(sessionfactoryutils.java:356)     @ org.springframework.orm.hibernate3.sessionfactoryutils.getsession(sessionfactoryutils.java:202) 

@transactional resolves problem can't understand why? mean fails in handler in other calls method works fine without annotation!

thank in advance!

upd: temporary solution add @transactional whole onlogoutsuccess method.. works)

if have defined transactionmanager in spring context have specify @transactional somewhere in stack. otherwise exception encountered because trying run query outside of transaction.

there workarounds such specifying current_session_context_class in hibernate configuration thread or

<property name="current_session_context_class">org.hibernate.context.threadlocalsessioncontext</property> 

but it's not production safe..

the possible values current_session_context_class jta, thread , managed. further that, jta , thread supported hibernate out of box. thread context used in stand alone hibernate apps or based on light weight frameworks spring , jta used in java ee environments.

also try sessionfactory.getcurrentsession() instead of sessionfactoryutils.getsession().


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -