[英]JDBC RowSets using singleton Connection
I'm using javax.sql.rowset.JdbcRowSet
and com.sun.rowset.JdbcRowSetImpl
to manipulate data. 我正在使用
javax.sql.rowset.JdbcRowSet
和com.sun.rowset.JdbcRowSetImpl
来处理数据。 Everything works fine, but I'm getting a warning that I might get a resource leak. 一切正常,但是我收到警告,我可能会发生资源泄漏。
Also, I'm using singleton Connection in JdbcRowSet constructor which is always open, but when I use JdbcRowSet close()
I can't use it in next method. 另外,我在始终打开的JdbcRowSet构造函数中使用单例连接,但是当我使用JdbcRowSet
close()
时,不能在下一个方法中使用它。
Here's the code. 这是代码。
public static Connection conn = DBConnection.getInstance()
.getConnection();
(not the exact work, only a sample code)
private static void function1() {
try {
JdbcRowSet myrs = new JdbcRowSetImpl(conn);
myrs.setCommand("SELECT * FROM `table1`");
myrs.execute();
//result iteration
myrs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void function2() {
same as function1 (for example, not really important here)
}
public static void start(){
function1();
function2();
}
When it gets to execute myrs
in function2()
I get an error: 当它在
function2()
执行myrs
,出现错误:
at com.sun.rowset.JdbcRowSetImpl.execute(Unknown Source)
Anyone knows how to solve it? 有人知道如何解决吗?
Here's the JdbcRowSetImpl implementation of close 这是关闭的JdbcRowSetImpl实现
public void close() throws SQLException {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
Since the JdbcRowSetImpl.close() will close the connection, the best way to fit with your current architecture might be to create a JdbcRowSet member or instance singleton that is closed when you expect the connection to be classed. 由于JdbcRowSetImpl.close()将关闭连接,因此适合当前体系结构的最佳方法可能是创建一个JdbcRowSet成员或实例单例,在您希望对连接进行分类时将其关闭。 Your function1 and function2 would look like this
您的function1和function2看起来像这样
public static Connection conn = DBConnection.getInstance()
.getConnection();
//Implementation of DBRowSet left as an exercise for the ambitious.
public static JdbcRowSet myrs = DBRowSet.getInstance();
private static void function1() {
try {
myrs.setCommand("SELECT * FROM `table1`");
myrs.execute();
//result iteration
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void function2() {
try {
myrs.setCommand("SELECT * FROM `table2`");
myrs.execute();
//result iteration
} catch (SQLException e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.