在关闭同一连接上的不同语句之前在连接上执行语句的行为是什么?
What is the behavior of executing a statement on a connection before closing a different statement on the same connection?
我试图理解在处理同一连接上不同语句执行的结果时在连接上执行语句的含义。
举个例子:
public void doSomething(){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement("some sql");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
//...do some stuff with the results
/*
pass the db connection so we don't create
too many connections to the database
*/
doSomethingElse(dbConnection);
}
} catch (SQLException e) {
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
public void doSomethingElse(Connection dbConnection){
PreparedStatement preparedStatement2 = null;
try {
//using the same connection from "doSomething"
preparedStatement2 = dbConnection.prepareStatement("some more sql");
ResultSet rs2 = preparedStatement2.executeQuery();
while (rs2.next()) {
//...do some stuff with the results
}
} catch (SQLException e) {
} finally {
if (preparedStatement2 != null) {
preparedStatement2.close();
}
}
}
我的主要问题是:在使用同一连接关闭语句之前在连接上执行语句的行为是什么?在同一个连接上执行另一个语句是否会破坏任何一个结果?我知道做这样的事情是不行的,但确切的行为是什么?或者行为是不可预测的?
我一直在寻找,但无法在任何地方找到答案或相关示例。任何见解或方向都会很棒。
JDBC 3.0 规范指出:
13.1.1 Creating Statements
... Each Connection object can create multiple Statement objects that may
be used concurrently by the program...
我试图理解在处理同一连接上不同语句执行的结果时在连接上执行语句的含义。
举个例子:
public void doSomething(){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement("some sql");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
//...do some stuff with the results
/*
pass the db connection so we don't create
too many connections to the database
*/
doSomethingElse(dbConnection);
}
} catch (SQLException e) {
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
public void doSomethingElse(Connection dbConnection){
PreparedStatement preparedStatement2 = null;
try {
//using the same connection from "doSomething"
preparedStatement2 = dbConnection.prepareStatement("some more sql");
ResultSet rs2 = preparedStatement2.executeQuery();
while (rs2.next()) {
//...do some stuff with the results
}
} catch (SQLException e) {
} finally {
if (preparedStatement2 != null) {
preparedStatement2.close();
}
}
}
我的主要问题是:在使用同一连接关闭语句之前在连接上执行语句的行为是什么?在同一个连接上执行另一个语句是否会破坏任何一个结果?我知道做这样的事情是不行的,但确切的行为是什么?或者行为是不可预测的?
我一直在寻找,但无法在任何地方找到答案或相关示例。任何见解或方向都会很棒。
JDBC 3.0 规范指出:
13.1.1 Creating Statements
... Each Connection object can create multiple Statement objects that may be used concurrently by the program...