使用Java EE进行连接池的最佳结构 [英] Best structure for connection pooling with Java EE

查看:135
本文介绍了使用Java EE进行连接池的最佳结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出连接池的最佳结构,以便我可以从任何servlet访问连接池并建立与我的数据库的连接。我一直在关注设置和配置数据源和连接池的一些教程,他们都在扩展HttpServlet的类中初始化和访问它们。所以它看起来像这样:

I am trying to figure out the best structure for connection pools so that I can access the connection pool from any servlet and establish a connection to my database. I have been following some tutorials in setting up and configuring the datasource and connection pool and they all have them initialized and accessed in classes that extend HttpServlet. So it looks something like this:

public class DatabaseConnector extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private DataSource dataSource;
    private Connection connection;
    private Statement statement;

    public void init() throws ServletException {
        try {
            // Get DataSource
            Context initContext  = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            dataSource = (DataSource)envContext.lookup("jdbc/test");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DatabaseConnector() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultSet resultSet = null;
        try {
            // Get Connection and Statement
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            String query = "SELECT * FROM STUDENT";
            resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + resultSet.getString(2) + resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (resultSet != null) {resultSet.close();}
                if (statement != null) {statement.close();}
                if (connection != null) {connection.close();}   
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

我觉得这个连接池只适用于这个servlet,它只会在发送get servlet URL的get请求时分发连接。如果我想要另一个需要访问数据库的servlet,该怎么办?我只想删除这个servlet中的doGet和doPost方法,并保留init以便在运行时初始化连接池,然后对这个servlet进行单例引用,这可以在其他servlet中使用。但是,这对我来说似乎不是正确的方法。构建可以从所有servlet和侦听器访问的连接池的最佳方法是什么?

It looks like to me that this connection pool is only for this servlet which would only distribute connections when a get request is sent the this servlets URL. What if I want to have another servlet that needs to access the database. I was thinking of just removing the doGet and doPost methods in this servlet and leave the init so that the connection pool would be initialized at runtime, and then have a singleton reference to this servlet which could be used in other servlets. However, this doesn't seem like the right way to me. What is the best way to structure a connection pool that can be accessed from all servlets and listeners?

感谢您的帮助!

推荐答案

完全错了。

访问连接的正确方法是使用 JNDI连接池

The correct way to access a connection is to use a JNDI connection pool.

Servlet是HTTP侦听器。它们不应与数据库有任何关系。

Servlets are HTTP listeners. They shouldn't have anything to do with databases.

正确分层的Java EE解决方案将数据源限制在服务层。它将检查进出的连接,了解工作单元和事务,并与数据访问对象进行交互。

A properly layered Java EE solution will restrict data sources to the service tier. It will check connections in and out, know about units of work and transactions, and interact with data access objects.

Servlets应该处理服务,而不是数据源。

Servlets should deal with services, not data sources.

这篇关于使用Java EE进行连接池的最佳结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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