简体   繁体   English

在Struts2中维护多个JDBC连接

[英]Maintaining multiple JDBC connections in Struts2

The number of connections to DB exceeds the permissible limit. 与DB的连接数超过了允许的限制。

This is what I tried so far. 这是我到目前为止尝试过的。

When the user successfully logs in I add one connection object to the session: 用户成功登录后,我将一个连接对象添加到会话中:

Connection conn = DatabaseConnectionManager.getConnection();
sessionMap.put("Connection", conn);

then, whenever I need a DB connection, I fetch it from the session: 然后,每当需要数据库连接时,我都会从会话中获取它:

Map<String, Object> sessionMap = (Map<String, Object>) ActionContext.getContext().get("session");
Connection conn = (Connection) sessionMap.get("Connection");

In the getConnection() method I print the number of times the method is called. getConnection()方法中,我打印该方法被调用的次数。 So although I fetch the Connection object from the session why does the number of connections exceed the permissible limit which is 50 ? 因此,尽管我从会话中获取Connection对象,但为什么连接数超过了允许的限制50?

JNDI Code: JNDI代码:

Connection conn = null;         
try {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/MySqdb");
    conn = ds.getConnection();

}  
catch (NamingException ex) {
    Logger.getLogger(DatabaseConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException sqle) {
    sqle.printStackTrace();
}
System.out.println("connection: "+connection++);
return conn;

I have used JNDI. 我已经使用了JNDI。 The number of connections is within the permissible limit. 连接数在允许的范围内。 I'm not sure if this is the right way, please advise. 我不确定这是否正确,请告知。

One connection for each user is not a good solution at all. 每个用户建立一个连接根本不是一个好的解决方案。 As mentioned you must use some connection poll and if you want something simple use https://commons.apache.org/proper/commons-dbcp/ . 如前所述,您必须使用一些连接轮询,如果要简单一些,请使用https://commons.apache.org/proper/commons-dbcp/ As mentioned there: 如此处所述:

Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds. 为了执行可能需要毫秒的数据库事务,为每个用户创建一个新连接可能很耗时(通常需要数秒的时钟时间)。 Opening a connection per user can be unfeasible in a publicly-hosted Internet application where the number of simultaneous users can be very large. 在公共托管的Internet应用程序中,每个用户打开连接可能是不可行的,在该应用程序中,同时存在的用户数量非常大。 Accordingly, developers often wish to share a "pool" of open connections between all of the application's current users. 因此,开发人员通常希望在所有应用程序当前用户之间共享开放连接的“池”。 The number of users actually performing a request at any given time is usually a very small percentage of the total number of active users, and during request processing is the only time that a database connection is required. 在任何给定时间实际执行请求的用户数量通常仅占活动用户总数的很小百分比,并且在请求处理期间,是唯一需要数据库连接的时间。 The application itself logs into the DBMS, and handles any user account issues internally. 应用程序本身登录到DBMS,并在内部处理任何用户帐户问题。

You can find samples at http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/ 您可以在以下网址找到示例: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM