servlet上下文的多个动态数据源 [英] Multiple dynamic data sources for a servlet context

查看:98
本文介绍了servlet上下文的多个动态数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个java servlet Web应用程序,它管理来自多个数据库(在结构上都相同)的信息,每个数据库对应一个不同的业务。用户选择存储在会话中的当前业务,并且应用程序可以显示或修改该当前业务。

I'm developing a java servlet web application that manages information from multiple databases (all structurally the same) each corresponding to a different "business". The user selects "the current business" which is stored in the session and the application can display or modify that "current business".

我想以动态的方式使用tomcat资源来使用jndi访问这些业务。通过这种方式,我可以在servlet中使用jstl sql标记或上下文查找。我无法在web.xml文件中定义每个资源,因为它们存储在SQL表中。最终结果是能够编写具有以下行的简单jsp:

I would like to use tomcat Resources in a dynamic way to have access to these businesses using jndi. In this way I can use the jstl sql tags or context lookups in servlets. I can not define each Resource in the web.xml file because they are stored in a SQL table. The end result is to be able to write simple jsp that has lines like these:

<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

<sql:query var = "users" dataSource="sources/${sessionScope.currentBusiness}">
  select id, firstName, lastName FROM user
</sql:query>

或可以包含这些行的servlet

or servlets that can have lines like these

String request.getSession().getAttribute("currentBusiness");

Context initial = new InitialContext();
Context context = (Context) initial.lookup("java:comp/env");
DataSource source = (DataSource) context.lookup("sources/" + currentBusiness);

我可以为当前业务获取正确的数据源。

where I can get the correct datasource for the "current business".

我已经尝试编写自己的来自javax.naming.spi.ObjectFactory的ObjectFactories但没有成功。有关如何轻松实现此目的的任何指示?

I have experimented with writing my own ObjectFactories derived from javax.naming.spi.ObjectFactory without success. Any pointers on how to easily do this?

推荐答案

我最终解决了以下解决方案,该解决方案包含一个SessionListener和一个工作的Servlet如下。 SessionListener具有以下形式:

I finally settled for the following solution consisting on a SessionListener and a Servlet that work as follows. The SessionListener has the following form:

public class SessionListener implements HttpSessionListener {

  public void sessionCreated(HttpSessionEvent event) {

    HttpSession session = event.getSession();

    // get list of possible data sources available to this session
    List<DataSource> sources = new ArrayList<DataSource>();
    ... code to get the available sources

    // get the current data source
    DataSource source = null;
    ... code to get the current source                               
    source = sources.get(0); // for example

    // setup the session attributes
    session.setAttribute("availableSources", sources);
    session.setAttribute("currentSource", source); 

  }

}

每当用户登录并创建会话,可用的DataSource列表和当前的列表将放入会话中。这是在会话级别完成的,因为DataSources依赖于用户登录。现在可以从应用程序中访问它们。要更改当前的DataSource,我使用以下简化版本创建了一个Servlet:

Whenever a user logs in and a session is created, the list of available DataSources, and the current one, are placed into the session. This is done at the Session level because DataSources depend on the user login in. It is now possible to have access at them from within the application. To change the current DataSource I created a Servlet with this simplified version:

public abstract class BoxletServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {

    HttpSession session = request.getSession(true);
    String s = request.getParameter("source");

    // based on 's' choose from the available DataSource
    List<DataSource> sources = (List<DataSource>) session.getParameter("availableSources");
    Source source = chooseFrom(sources, s);                                                       
    session.setParameter("currentSource", source);          

    // forward to a page saying that the DataSource changed

  }

}

通过此实现,现在可以创建以下jsps:

With this implementation it is now possible to create the following jsps:

<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

<sql:query var = "users" dataSource="${sessionScope.currentSource}">
  select id, firstName, lastName FROM user
</sql:query>

希望它可以帮助其他人。

Hope it helps someone else.

这篇关于servlet上下文的多个动态数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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