[英]Java JDBC Connection Pooling (How to check)?
First of all, I'm new to Java/JSP/Eclipse
. 首先,我是Java/JSP/Eclipse
新手。 However, I've many many years of .Net experience. 但是,我有很多年的.Net经验。 I have just learned in Java how to make a connection to MS SQL Server using the javax.sql.DataSource
(so that I don't need to key in the userid/password). 我刚刚在Java中学习了如何使用javax.sql.DataSource
与MS SQL Server建立连接(因此无需键入用户ID /密码)。
Right now, I've created a class to handle DB query, and in that class, I have a function that I can just call to return a RowSet
. 现在,我创建了一个用于处理数据库查询的类,并且在该类中,我可以调用一个函数以返回RowSet
。
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import javax.sql.DataSource;
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl;
public abstract class ClsDBAccessBASE {
public boolean prbAutoCommit = true;
public String prsDataSourceName = "";
public ClsDBAccessBASE( String pvsDataSourceName ) {
prsDataSourceName = pvsDataSourceName;
}
public JdbcRowSet fnorsSQLText( String pvsSQLText ) {
JdbcRowSet voRS = null;
try {
Context voContext = new InitialContext();
DataSource voDS = (DataSource)voContext.lookup(prsDataSourceName);
Connection voConn = (Connection)voDS.getConnection();
voRS = new JdbcRowSetImpl((voConn.createStatement()).executeQuery(pvsSQLText));
} catch (Exception e) {e.printStackTrace();}
return voRS;
}
}
}
My objective is to ensure that I am using Connection Pooling. 我的目标是确保使用连接池。 I understand that the JdbcRowSet
implements that automatically? 我知道JdbcRowSet
是自动实现的吗? However, the problem (I think) is that I am casting the (voConn.createStatement()).executeQuery(pvsSQLText)
from a ResultSet
to a JdbcRowSet
. 但是,问题(我认为)是我正在将(voConn.createStatement()).executeQuery(pvsSQLText)
从ResultSet
转换为JdbcRowSet
。 Would this mean that the underlying connection used is still ResultSet
? 这是否意味着所使用的基础连接仍然是ResultSet
? and not RowSet
? 而不是RowSet
? Is there a way for me to check that I am indeed using Connection Pooling? 有没有一种方法可以检查我是否确实在使用连接池?
I'm sorry, if my question sounds silly. 很抱歉,如果我的问题听起来很傻。 I am very new to Java development & Eclipse. 我是Java开发和Eclipse的新手。 Please bear with me and I will appreciate any guidance you can provide me. 请多多包涵,我们将为您提供任何指导,我们将不胜感激。
I am using: sqljdbc4.jar
from Microsoft
(which seems to give me error whenever I call the JdbcRowSet.execute()
function and/or .next() function. Something about NullReference. But that's another problem. 我正在使用:来自Microsoft
sqljdbc4.jar
(每当我调用JdbcRowSet.execute()
函数和/或.next()函数时,似乎都会给我错误。有关NullReference的信息。但这是另一个问题。
获取JDBC connection object
System.out.println(conn.getClass().getName());
Yes, if you use container DataSource, it do connection pooling for you. 是的,如果您使用容器DataSource,它将为您做连接池。
Connection pooling not depend of RowSet implementation, and rowset should be closed after use. 连接池不依赖RowSet实现,使用后应关闭行集。 I suggest to read it immediatly into array of plain object and close. 我建议立即将其读入普通对象数组并关闭。 And don't use JdbcRowSetImpl
, RowSet
will be enough. 并且不要使用JdbcRowSetImpl
, RowSet
就足够了。
EDIT: i forget to answer your question: Check how many connection you have in pool, than in loop call getConnection and execute select, and do not close ResultSet
. 编辑:我忘记回答您的问题:检查池中的连接数,而不是循环调用getConnection并执行select,并且不关闭 ResultSet
。 You get error when you reach count open connection == configured max open connection in connection pool. 当您达到计数打开连接==在连接池中配置的最大打开连接时,您会收到错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.