[英]Setup Connection pooling in jsp/servlets application based on MVC?
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. 我想在Java Web应用程序中使用带有MySQL和JDBC的连接池,我在Apache Tomcat 6.0(6.0.35)上找到了一个非常值得学习的资源-JNDI Datasource HOW-TO ,但是本示例使用JSTL代码来说明如何检索从池连接。 I want to work similarly but with a MVC architecture from scratch consisting of Beans, DAOs, Servlets and JSPs. 我想进行类似的工作,但要从头开始使用MVC架构,该架构由Bean,DAO,Servlet和JSP组成。 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? 我从BalusC的一个非常好的DAO教程中获得了我想要的一切,但是在教程的最后部分说连接池怎么样呢令我感到困惑。 . 。 Could anyone please elaborate on this connection pooling topic and the close()
method? 谁能详细说明这个连接池主题和close()
方法吗?
EDIT: 编辑:
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? 正如我上面链接的教程在JDK7之前出现的那样,JDK7现在具有try-wth-resource资源代码,该代码自动关闭Connection
,那么我们如何维护连接池并在此处使用相同的DAO代码(或很少的代码)在池中关闭Connection 更改),如教程中所示?
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. 如果应用程序需要是由多个用户使用一个connection
可以通过保持connection pool
所以几个这些用户将重新使用现有的connection
,而不是制造新的connection
,这将消耗时间。 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! 关于close()
方法:连接池保持活动状态,如果您在每次访问后不关闭与其的连接,连接将堆积起来,如果数量增加,连接池将被阻塞并且不再接受其他连接!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.