如何从DAO层访问Web层(控制器)? [英] How to access web layer(Controllers) from the DAO Layer?

查看:127
本文介绍了如何从DAO层访问Web层(控制器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须取消正在运行的查询。在我的DAO中,当我开始查询时,在 ServletContext 中设置 Entity.session ,并在查询结果完了。然后检查 Controller 是否会话存在于 ServletContext 中,如果它存在,则取消查询从 ServletContext 中的会话对象调用 session.cancelQuery()



这在我的开发环境中工作正常,但是在我的预先测试中,整个代码不会在tomcat容器内运行。 Web部件在Tomcat中运行,而数据层运行Java应用程序。因此我无法在DAO类中找到 ServletContext ,它给了我一个 ClassNotFound 异常



因此,我将DAO中的Web层分离了。现在我在调用DAO calculate()时在控制器中设置了 hibernate.session 。但是这产生了一个问题,即使没有进行计算,现在会话仍然存在,并且实际上存在一些事后或预先计算。我的机制取消查询不起作用。



所以我需要的是访问 Controller DAO >来设置会话。我可以在Controller中使用静态方法,然后从中设置会话,但我认为这不是一个好习惯。

DAO初始代码:

  public calculate(){
Session session = mEntityManager.unwrap(Session.class);
// pre presing
if(session!= null)
{
mContext.setAttribute(+ view.getId(),session);
}
List< Object []> results = query.getResultList();
mContext.removeAttribute(+ view.getId());
//后处理
}

解耦的DAO代码:
在控制器中调用calculate方法之前,将从控制器调用getSession()方法。然后,当用户向UI请求取消时,在控制器中调用 cancel 方法。


  public Session getSession()
{
Session session = mEntityManager.unwrap(Session.class);
返回会话;
}

public calculate(){
// pre possing
List< Object []> results = query.getResultList();
//后处理
}

控制器:

  @RequestMapping 
public WebServiceResponse cancel(HttpServletRequest request)
{
if(mContext.getAttribute(id)!= null)
((Session)mContext.getAttribute(id))。cancelQuery();


解决方案

流程,因为你没有正确分离问题的处理与处理问题:你在数据层中检测到问题,但需要在表示层处理。



考虑一下使用例外。当数据层检测到问题时会引发异常。表示层可以通过捕获异常来处理数据层中的问题。


I had to cancel the running queries. In my DAO I set the Entity.session in the ServletContext when I start the query and remove it when the query is finished. Then check in the Controller if the session is present in the ServletContext, if it is present then I cancel the query by calling the session.cancelQuery() from the session object in the ServletContext.

This was working fine in my dev environment, but in my pre-prod testing the entire code does not run inside the tomcat container. The web part runs in tomcat whereas the data layer runs a java application. hence I could not find the ServletContext in the DAO class and it gave me a ClassNotFound Exception

So I decoupled the web layer in the DAO. Now I set the hibernate.session in the controller itself when it calls the DAO calculate(). But this created a problem, now the session exists even if there are no calculations going on and in actual there are some post or precalculations. And my mechanism to cancel the query doesn't work.

So what I need is a way to access the Controller from the DAO to set the session. I could have used a static method in the Controller and then set the session from it but I think this is again not a good 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: The method getSession() is called from the controller before the calculate method is called in the controller. And then when the user requests a cancel from the UI the cancel method is called in the 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 you have convoluted control flow because you are not properly separating detection of a problem from handling the problem: you detect a problem in the data layer but need to handle it in the presentation layer.

Consider using exceptions. Have the data layer throw an exception when it detects a problem. The presentation layer can handle problems in the data layer by catching exceptions.

这篇关于如何从DAO层访问Web层(控制器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆