SQL 尝试在 finally 子句中关闭连接时出现异常
SQL exception occurs when trying to close the connection in finally clause
我在里面有一些 OAF 代码,我正在启动 sql 连接,但是当我试图关闭它时,它抛出 sql 异常。这是代码。
try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("handle exception here:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
conn.close(); // throws exception here
}
尝试用 try and catch 子句括起结束代码
try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("Exception in Finding Project Type:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
try{
conn.close(); // throws exception here
}
catch(SQLException e)
{
//your code here
}
}
任何 JDBC 操作的基本方案都非常简单:
- 取得联系。
- 创建声明。
执行语句得到ResultSet。
处理结果集。
关闭一切:ResultSet,
语句和连接。
错误的解决方案
public void handleJDBC() {
try {
DataSource dataSource = getDataSource();
Connection connection = dataSource.getConnection();
operation(connection)
connection.close();
} catch (SQLException e) {
//do something here
}
}
良好实践
finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
if (exception != null) {
exception = new DoubleException(exception, e);
} else {
exception = e;
}
}
}
}
if (exception != null) {
throw exception;
}
我在里面有一些 OAF 代码,我正在启动 sql 连接,但是当我试图关闭它时,它抛出 sql 异常。这是代码。
try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("handle exception here:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
conn.close(); // throws exception here
}
尝试用 try and catch 子句括起结束代码
try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("Exception in Finding Project Type:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
try{
conn.close(); // throws exception here
}
catch(SQLException e)
{
//your code here
}
}
任何 JDBC 操作的基本方案都非常简单:
- 取得联系。
- 创建声明。
执行语句得到ResultSet。
处理结果集。
关闭一切:ResultSet, 语句和连接。
错误的解决方案
public void handleJDBC() {
try {
DataSource dataSource = getDataSource();
Connection connection = dataSource.getConnection();
operation(connection)
connection.close();
} catch (SQLException e) {
//do something here
}
}
良好实践
finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
if (exception != null) {
exception = new DoubleException(exception, e);
} else {
exception = e;
}
}
}
}
if (exception != null) {
throw exception;
}