设置基于MVC的jsp / servlets应用程序中的连接池? [英] Setup Connection pooling in jsp/servlets application based on MVC?

查看:208
本文介绍了设置基于MVC的jsp / servlets应用程序中的连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Java Web应用程序中使用MySQL和JDBC使用连接池,我找到了一个非常有资源可以学习 Apache Tomcat 6.0(6.0.35) - JNDI Datasource HOW-TO
但是这个例子使用JSTL代码来解释如何检索从游泳池连接。我希望以类似的方式工作,但是从头开始的MVC架构包括Beans,DAO,Servlet和JSP。我从一个非常好的 DAO教程获得了我想要的一切由BalusC撰写,但我在教程的最后部分说到了连接池怎么样?。任何人都可以详细说明这个连接池主题和 close()方法吗?

I want to use Connection pooling in my java web application with MySQL and JDBC, I found a very resource to learn at Apache Tomcat 6.0 (6.0.35) - JNDI Datasource HOW-TO, But this example uses JSTL code to explain how to retrieve connection from pool. I want to work similarly but with a MVC architecture from scratch consisting of Beans, DAOs, Servlets and JSPs. I got everything I want from a very good DAO tutorial by BalusC but I am confused in the last part of tutorial saying How about Connection Pooling?. Could anyone please elaborate on this connection pooling topic and the close() method?

编辑:

实际上我也应该先添加这个东西:

我上面链接的教程是在JDK7之前发布的,它现在有了try-wth-resource代码,自动关闭连接,然后我们如何使用相同的DAO代码维护连接池并关闭池中的连接(或者进行少量更改)如教程中那样?


Actually i should have add this thing earlier also:
As the tutorial I have linked above comes before JDK7, which now has try-wth-resource code that closes the Connection automatically, then how can we maintain a Connection pool and closing a Connection in pool here with the same DAO code (or with few changes) as in the tutorial?

推荐答案

如果您的应用程序需要多个用户使用一个连接可以由连接池持有,因此其中一些用户将重用现有的连接而不是使用新的连接,这将消耗时间。
关于 close()方法:连接池保持活动状态,如果每次访问后都没有关闭连接,连接将堆积起来,如果数量增加连接池被卡住,不再接受其他连接!

if your application requires is to be used by several users one connection can be held by a connection pool so several of those users will reuse the existing connection instead of making new connection which will consume time. about the close()method: the connection pool stays active and if you do not close a connection to it after every access,connections will pile up and if the number increases the connection pool get jammed and no longer accepts other connections!

public class MyDao {

    private InitialContext context;
    private DataSource datasource;

    public MyDao() {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            context = new InitialContext();
            datasource = (DataSource) context.lookup("datasource name");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }
 public MyBean getMyBean() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet res = null;

        String sql = "some query";
        try {
            connection = datasource.getConnection();//pool connection
            statement = connection.prepareStatement(sql);
            res = statement.executeQuery();
            while (res.next()) {
               //return true

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

     finally {
            if (rs!= null) try { rs.close(); } catch (SQLException logOrIgnore) {}//result set if any
            if (stm!= null) try { stm.close(); } catch (SQLException logOrIgnore) {}//clase statement if any
            if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}//close connection
            }
            }


}//close MyDao

这篇关于设置基于MVC的jsp / servlets应用程序中的连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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