java - How to access web layer(Controllers) from the DAO Layer? -
i had cancel running queries. in dao set entity.session
in servletcontext
when start query , remove when query finished. check in controller
if session present in servletcontext
, if present cancel query calling session.cancelquery()
session object in servletcontext
.
this working fine in dev environment, in pre-prod testing entire code not run inside tomcat container. web part runs in tomcat whereas data layer runs java application. hence not find servletcontext
in dao class , gave me classnotfound
exception
so decoupled web layer in dao. set hibernate.session
in controller when calls dao calculate()
. created problem, session exists if there no calculations going on , in actual there post or precalculations. , mechanism cancel query doesn't work.
so need way access controller
dao
set session. have used static method in controller , set session think again not practice.
dao initial code:
public calculate(){ session session = mentitymanager.unwrap(session.class); //pre possing if(session != null) { mcontext.setattribute(""+view.getid(), session); } list<object[]> results = query.getresultlist(); mcontext.removeattribute(""+view.getid()); //post processing }
decoupled dao code: method getsession() called controller before calculate method called in controller. , when user requests cancel ui cancel
method called in controller.
public session getsession() { session session = mentitymanager.unwrap(session.class); return session; } public calculate(){ //pre possing list<object[]> results = query.getresultlist(); //post processing }
controller:
@requestmapping public webserviceresponse cancel(httpservletrequest request) { if(mcontext.getattribute(id) != null) ((session)mcontext.getattribute(id)).cancelquery(); }
it seems have convoluted control flow because not separating detection of problem handling problem: detect problem in data layer need handle in presentation layer.
consider using exceptions. have data layer throw exception when detects problem. presentation layer can handle problems in data layer catching exceptions.
Comments
Post a Comment