spring - Struts2 and Hibernate - Access ActionContext.getContext().getSession() outside a Servlet -


i try access actioncontext.getcontext() outside struts (struts2) action in 2 different situations , different result, regarding trying:

  1. from currenttenantidentifierresolverimpl (my) implementation of currenttenantidentifierresolver, hibernate interface --> ko (the result null)
  2. from abstractroutingdatasource (my) implementation of abstractdatasource, spring interface --> ok

i have found solution currenttenantidentifierresolverimpl working using threadlocal, :

  1. why working spring , not hibernate?
  2. why can't access actioncontext (threadlocal also) can access threadlocal (that set actioncontext, same place, same data)?

currenttenantidentifierresolverimpl : supposed give tenant used hibernate can give connection database.

import java.util.map; import org.hibernate.context.spi.currenttenantidentifierresolver; import com.opensymphony.xwork2.actioncontext;  public class currenttenantidentifierresolverimpl implements currenttenantidentifierresolver {  @override public string resolvecurrenttenantidentifier() {      if (actioncontext.getcontext() != null) { 

abstractroutingdatasource : same handled spring

import java.util.map; import org.springframework.jdbc.datasource.lookup.abstractroutingdatasource; import com.opensymphony.xwork2.actioncontext;  public class routingdatasource extends abstractroutingdatasource {      @override     protected object determinecurrentlookupkey() {          if (actioncontext.getcontext() != null) { 

when have @ stack in both case, can see that:

  • abstractroutingdatasource has got servlet (homeaction) in stack
  • currenttenantidentifierresolver not have servlet (homeaction) in stack

here stack traces.

hibernate :

daemon thread [http-bio-8081-exec-4] (suspended (breakpoint @ line 24 in currenttenantidentifierresolverimpl))      owns: socketwrapper<e>  (id=184)         currenttenantidentifierresolverimpl.resolvecurrenttenantidentifier() line: 24        sessionfactoryimpl$sessionbuilderimpl.<init>(sessionfactoryimpl) line: 1572      sessionfactoryimpl.withoptions() line: 1019      sessionfactoryimpl.opensession() line: 999       opensessioninviewfilter.opensession(sessionfactory) line: 203        opensessioninviewfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 139         opensessioninviewfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       characterencodingfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 88      characterencodingfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       standardwrappervalve.invoke(request, response) line: 222         standardcontextvalve.invoke(request, response) line: 123         nonloginauthenticator(authenticatorbase).invoke(request, response) line: 502         standardhostvalve.invoke(request, response) line: 171        errorreportvalve.invoke(request, response) line: 100         accesslogvalve.invoke(request, response) line: 953       standardenginevalve.invoke(request, response) line: 118      coyoteadapter.service(request, response) line: 408       http11processor(abstracthttp11processor<s>).process(socketwrapper<s>) line: 1041         http11protocol$http11connectionhandler(abstractprotocol$abstractconnectionhandler<s,p>).process(socketwrapper<s>, socketstatus) line: 603        jioendpoint$socketprocessor.run() line: 312      threadpoolexecutor(threadpoolexecutor).runworker(threadpoolexecutor$worker) line: 1145       threadpoolexecutor$worker.run() line: 615        taskthread(thread).run() line: 722 

spring:

daemon thread [http-bio-8081-exec-2] (suspended (breakpoint @ line 20 in routingdatasource))        owns: socketwrapper<e>  (id=562)         routingdatasource.determinecurrentlookupkey() line: 20       routingdatasource(abstractroutingdatasource).determinetargetdatasource() line: 196       routingdatasource(abstractroutingdatasource).getconnection() line: 164       datasourceconnectionproviderimpl.getconnection() line: 139       abstractsessionimpl$noncontextualjdbcconnectionaccess.obtainconnection() line: 380       logicalconnectionimpl.obtainconnection() line: 228       logicalconnectionimpl.getconnection() line: 171      statementpreparerimpl.connection() line: 63      statementpreparerimpl$5.doprepare() line: 162        statementpreparerimpl$5(statementpreparerimpl$statementpreparationtemplate).preparestatement() line: 186         statementpreparerimpl.preparequerystatement(string, boolean, scrollmode) line: 160       queryloader(loader).preparequerystatement(string, queryparameters, limithandler, boolean, sessionimplementor) line: 1885         queryloader(loader).executequerystatement(string, queryparameters, boolean, list<afterloadaction>, sessionimplementor) line: 1862        queryloader(loader).executequerystatement(queryparameters, boolean, list<afterloadaction>, sessionimplementor) line: 1839        queryloader(loader).doquery(sessionimplementor, queryparameters, boolean, resulttransformer) line: 910       queryloader(loader).doqueryandinitializenonlazycollections(sessionimplementor, queryparameters, boolean, resulttransformer) line: 355        queryloader(loader).dolist(sessionimplementor, queryparameters, resulttransformer) line: 2554        queryloader(loader).listusingquerycache(sessionimplementor, queryparameters, set<serializable>, type[]) line: 2399       queryloader(loader).list(sessionimplementor, queryparameters, set<serializable>, type[]) line: 2362      queryloader.list(sessionimplementor, queryparameters) line: 497      querytranslatorimpl.list(sessionimplementor, queryparameters) line: 387      hqlqueryplan.performlist(queryparameters, sessionimplementor) line: 236      sessionimpl.list(string, queryparameters) line: 1264         queryimpl.list() line: 103       hibernatetemplate$29.doinhibernate(session) line: 875        hibernatetemplate$29.doinhibernate(session) line: 864        hibernatetemplate.doexecute(hibernatecallback<t>, boolean) line: 340         hibernatetemplate.executewithnativesession(hibernatecallback<t>) line: 308       hibernatetemplate.find(string, object...) line: 864      aidegestiondaoimpl(generichibernatedao<t,id>).find(string, object...) line: 92       aidegestiondaoimpl.findbygerantanddate(long, date) line: 54      nativemethodaccessorimpl.invoke0(method, object, object[]) line: not available [native method]       nativemethodaccessorimpl.invoke(object, object[]) line: 39       delegatingmethodaccessorimpl.invoke(object, object[]) line: 25       method.invoke(object, object...) line: 597       aoputils.invokejoinpointusingreflection(object, method, object[]) line: 317      reflectivemethodinvocation.invokejoinpoint() line: 190       reflectivemethodinvocation.proceed() line: 157       methodinvocationproceedingjoinpoint.proceed() line: 85       daoaudit.daoprofiling(proceedingjoinpoint) line: 30      generatedmethodaccessor101.invoke(object, object[]) line: not available      delegatingmethodaccessorimpl.invoke(object, object[]) line: 25       method.invoke(object, object...) line: 597       aspectjaroundadvice(abstractaspectjadvice).invokeadvicemethodwithgivenargs(object[]) line: 621       aspectjaroundadvice(abstractaspectjadvice).invokeadvicemethod(joinpoint, joinpointmatch, object, throwable) line: 610        aspectjaroundadvice.invoke(methodinvocation) line: 68        reflectivemethodinvocation.proceed() line: 168       exposeinvocationinterceptor.invoke(methodinvocation) line: 92        reflectivemethodinvocation.proceed() line: 179       jdkdynamicaopproxy.invoke(object, method, object[]) line: 207        $proxy108.findbygerantanddate(long, date) line: not available        homeaction.buildtabsyn() line: 241       nativemethodaccessorimpl.invoke0(method, object, object[]) line: not available [native method]       nativemethodaccessorimpl.invoke(object, object[]) line: 39       delegatingmethodaccessorimpl.invoke(object, object[]) line: 25       method.invoke(object, object...) line: 597       defaultactioninvocation.invokeaction(object, actionconfig) line: 450         defaultactioninvocation.invokeactiononly() line: 289         defaultactioninvocation.invoke() line: 252       parametersinterceptor.dointercept(actioninvocation) line: 239        parametersinterceptor(methodfilterinterceptor).intercept(actioninvocation) line: 98      defaultactioninvocation.invoke() line: 246       actionmappingparametersinteceptor(parametersinterceptor).dointercept(actioninvocation) line: 239         actionmappingparametersinteceptor(methodfilterinterceptor).intercept(actioninvocation) line: 98      defaultactioninvocation.invoke() line: 246       multiselectinterceptor.intercept(actioninvocation) line: 73      defaultactioninvocation.invoke() line: 246       checkboxinterceptor.intercept(actioninvocation) line: 91         defaultactioninvocation.invoke() line: 246       prepareinterceptor.dointercept(actioninvocation) line: 171       prepareinterceptor(methodfilterinterceptor).intercept(actioninvocation) line: 98         defaultactioninvocation.invoke() line: 246       servletconfiginterceptor.intercept(actioninvocation) line: 164       defaultactioninvocation.invoke() line: 246       exceptionlogginginterceptor(exceptionmappinginterceptor).intercept(actioninvocation) line: 189       defaultactioninvocation.invoke() line: 246       strutsactionproxy.execute() line: 54         dispatcher.serviceaction(httpservletrequest, httpservletresponse, servletcontext, actionmapping) line: 563       executeoperations.executeaction(httpservletrequest, httpservletresponse, actionmapping) line: 77         strutsprepareandexecutefilter.dofilter(servletrequest, servletresponse, filterchain) line: 99        applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       responseoverridefilter.dofilter(servletrequest, servletresponse, filterchain) line: 125      applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       sessionfilter.dofilter(servletrequest, servletresponse, filterchain) line: 52        applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       userlogfilter.dofilter(servletrequest, servletresponse, filterchain) line: 43        applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 330      filtersecurityinterceptor.invoke(filterinvocation) line: 118         filtersecurityinterceptor.dofilter(servletrequest, servletresponse, filterchain) line: 84        filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      exceptiontranslationfilter.dofilter(servletrequest, servletresponse, filterchain) line: 113      filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      sessionmanagementfilter.dofilter(servletrequest, servletresponse, filterchain) line: 103         filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      anonymousauthenticationfilter.dofilter(servletrequest, servletresponse, filterchain) line: 113       filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      securitycontextholderawarerequestfilter.dofilter(servletrequest, servletresponse, filterchain) line: 150         filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      requestcacheawarefilter.dofilter(servletrequest, servletresponse, filterchain) line: 45      filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      usernamepasswordauthenticationfilter(abstractauthenticationprocessingfilter).dofilter(servletrequest, servletresponse, filterchain) line: 199        filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      logoutfilter.dofilter(servletrequest, servletresponse, filterchain) line: 110        filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      webasyncmanagerintegrationfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 50         webasyncmanagerintegrationfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107      filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      concurrentsessionfilter.dofilter(servletrequest, servletresponse, filterchain) line: 125         filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      securitycontextpersistencefilter.dofilter(servletrequest, servletresponse, filterchain) line: 87         filterchainproxy$virtualfilterchain.dofilter(servletrequest, servletresponse) line: 342      filterchainproxy.dofilterinternal(servletrequest, servletresponse, filterchain) line: 192        filterchainproxy.dofilter(servletrequest, servletresponse, filterchain) line: 160        delegatingfilterproxy.invokedelegate(filter, servletrequest, servletresponse, filterchain) line: 344         delegatingfilterproxy.dofilter(servletrequest, servletresponse, filterchain) line: 261       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       opensessioninviewfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 150         opensessioninviewfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       opensessioninviewfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 150         opensessioninviewfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       characterencodingfilter.dofilterinternal(httpservletrequest, httpservletresponse, filterchain) line: 88      characterencodingfilter(onceperrequestfilter).dofilter(servletrequest, servletresponse, filterchain) line: 107       applicationfilterchain.internaldofilter(servletrequest, servletresponse) line: 243       applicationfilterchain.dofilter(servletrequest, servletresponse) line: 210       standardwrappervalve.invoke(request, response) line: 222         standardcontextvalve.invoke(request, response) line: 123         nonloginauthenticator(authenticatorbase).invoke(request, response) line: 502         standardhostvalve.invoke(request, response) line: 171        errorreportvalve.invoke(request, response) line: 100         accesslogvalve.invoke(request, response) line: 953       standardenginevalve.invoke(request, response) line: 118      coyoteadapter.service(request, response) line: 408       http11processor(abstracthttp11processor<s>).process(socketwrapper<s>) line: 1041         http11protocol$http11connectionhandler(abstractprotocol$abstractconnectionhandler<s,p>).process(socketwrapper<s>, socketstatus) line: 603        jioendpoint$socketprocessor.run() line: 312      threadpoolexecutor$worker.runtask(runnable) line: 895        threadpoolexecutor$worker.run() line: 918        taskthread(thread).run() line: 662   

the actioncontext seems initialized filter "strutsprepareandexecutefilter":

dofilter(servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception {          httpservletrequest request = (httpservletrequest) req;         httpservletresponse response = (httpservletresponse) res;          try {             prepare.setencodingandlocale(request, response);             prepare.createactioncontext(request, response); 

and in "hibernate" case filter not executed (cf. stack traces in question) ==> actioncontext not initialized.

so conclusion: yes can access actioncontext outside servlet, if stack "classic". careful if try access framework or transversal code.

here solution hibernate problem: have created own filter access httpsession (we have access here) , put information need in threadlocal.

my filter:

public class tenantidentifierfilter extends onceperrequestfilter {      @override     protected void dofilterinternal(             httpservletrequest request, httpservletresponse response, filterchain filterchain)             throws servletexception, ioexception {          httpsession session = request.getsession();         string tenantcode = (string) session.getattribute(worksession.connected_entity);         usertenantcontext.set(tenantcode);         logger.debug("vpd-multitenancy - tenantcode : " + tenantcode);         filterchain.dofilter(request, response);     }  } 

my threadlocal:

public class usertenantcontext {      public static final threadlocal<string> usertenantcode = new threadlocal<string>();      /**      * set user in context of threadlocal      * @param user      */     public static void set(string tenantcode) {         usertenantcode.set(tenantcode);     }      public static void unset() {         usertenantcode.remove();     }      /**      * @return user in context of threadlocal      */     public static string get() {         return usertenantcode.get();     } } 

my web.xml (be sure put filter before hibernate filter, threadlocal initialized):

    ...     <filter-mapping>             <filter-name>characterencodingfilter</filter-name>             <url-pattern>/*</url-pattern>         </filter-mapping>          <!-- vpd-multitenancy-add-->         <filter>             <filter-name>tenantfilter</filter-name>             <filter-class>                 amundi.sits.mgs.web.services.user.tenantidentifierfilter             </filter-class>         </filter>         <filter-mapping>             <filter-name>tenantfilter</filter-name>             <url-pattern>/*</url-pattern>         </filter-mapping>         <!-- vpd-multitenancy-add/ -->          <filter>             <filter-name>hibernatefilter</filter-name>             <filter-class>                 org.springframework.orm.hibernate4.support.opensessioninviewfilter             </filter-class>         </filter>         <filter-mapping>             <filter-name>hibernatefilter</filter-name>             <url-pattern>/*</url-pattern>         </filter-mapping>   ... 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -