如何在JBoss中打开的连接池中正确保持数据库连接 [英] How to properly keep a DB connection from a Connection Pool opened in JBoss

查看:216
本文介绍了如何在JBoss中打开的连接池中正确保持数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JBoss AS 7.1作为服务器,并且我的DataSource配置了池。我对此很新,所以请原谅任何新手的错误...毕竟我在这里学习。

I'm using JBoss AS 7.1 as a server and I have my DataSource configured with pooling. I'm quite new to this so please excuse any rookie mistakes... after all I'm here to learn.

当客户登录时会获得连接到数据库,我需要保持该连接(从池中)打开,直到用户注销或HttpSession到期。这是来自我们的数据库管理员的绝对要求。谁说他需要数据库会话变量。我正在使用 servlet

When a client logs-in it gets a connection to the database and I need to keep that connection(from the pool) open until the user logs-out or the HttpSession expires. This is an absolute requirement coming from our DB Admin. who says that he needs the DB session variables. I am using a servlet for all this.

玩我遇到的两个主要问题:

Playing with the possibilities I have encountered 2 major problems:


  1. 据我所知,JBoss会自动关闭未使用的连接=>我打开的连接返回到游泳池。所以这可能不是正确的道路。

  1. As far as I see JBoss automatically closes unused connections => my opened connection returns to the pool. So this might not be the right path.

如果我尝试存储/调用Connection对象,如 this:

If I try to store/recall the Connection object like this:

private Hashtable<String, Connection> connections = new Hashtable<String, Connection>();

try {
    String strDSName1 = "java:/OracleDSJNDI";
    ctx = new InitialContext();
    ds1 = (javax.sql.DataSource) ctx.lookup(strDSName1);

    System.out.println("Got 1'st ds.");

} catch (Exception e) {
    System.out.println("ERROR getting 1'st DS : " + e);
}

connection = ds1.getConnection();
connections.put(session.getId(), connection);

conn = (Connection) connections.get(sessionID);

会抛出此异常:


java.sql.SQLException:Connection与托管
connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@dee1f37






我的问题是:如何正确保持我的连接打开?

谢谢

推荐答案


如何正确保持连接打开?

How do I properly keep my connection opened?

一定不能这样做,让连接池处理这个。

You must not do that, let the connection pool handle this.

在幕后,连接pool将在 SLEEPING 状态中保留与数据库引擎(MySQL,Oracle,SQL Server ......取决于您如何配置它)的大量数据库连接。当您执行此代码时:

Behind the scenes, the connection pool will keep a bunch of database connections to the database engine (MySQL, Oracle, SQL Server... depends how you configure it) in SLEEPING state. When you execute this code:

//avoiding all the particular exceptions just for code simplicity purposes...
//in real world applications, you must handle each of these exceptions
public Connection getConnection() throws Exception {
    ctx = new InitialContext();
    ds1 = (javax.sql.DataSource) ctx.lookup(strDSName1);
    return ds1.getConnection();
}

您要求连接池检索其中一个可用连接。连接池将为您提供数据库连接(如果可用),并允许您根据需要使用它。然后你在任何你想要/需要的地方使用它并关闭它

You're asking to the connection pool to retrieve one of these connections available. The connection pool will give you a database connection (if available) and let you use it as long as you want. Then you use it wherever you want/need and close it:

public void foo() throws Exception {
    Connection connection = getConnection();
    //do what you want/need...

    //in the end, you close the connection
    //this is A MUST!
    connection.close();
}

执行 connection.close()从连接池检索的连接中,您没有关闭物理数据库连接,但通知连接池此特定数据库连接必须返回 SLEEPING 状态。

When executing connection.close() from a connection retrieved by the connection pool, you're not closing the physical database connection but notifying the connection pool this specific database connection must return to the SLEEPING state.

解释中的一些建议:


  • 必须尝试保持连接活动,这是连接池的工作。

  • 一定不能尝试将连接存储在任何缓存类结构中,即连接池的工作。

  • 必须检索 java.sql.Connection 在最短的范围内,你将需要它。使用后,关闭

  • You must not try to keep the connection alive, that's connection pool's job.
  • You must not try to store the connections in any cache-like structure, that's connection pool's job.
  • You must retrieve a java.sql.Connection in the shortest scope you will need it. Once you have used it, close it.

这篇关于如何在JBoss中打开的连接池中正确保持数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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