Hi,
i am currently checking some code of mine wiht findbugs and in the snipplet
(attached at the end)
i receive an findbugs message on the following line :
ps = con.prepareStatement("select * from table");
The message is :
ODR Exception :
Method may fail to close database resource on exception
The method creates a database resource (such as a database connection or row
set), does not assign it to any fields, pass it to other methods, or return it,
and does not appear to
close the object on all exception paths out of the method. Failure to close
database resources on all paths out of a method may result in poor performance,
and could cause the
application to have problems communicating with the database.
I cannot see, why i get this message and why i only get it for the
PreparedStatement.
Can anyone give me an hint, if this is really a bug in source or if findbugs
cannot handle the code correctly.
Kind Regards
Mathias
public void showDatabaseContent() {
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
con = DataSource.getConnection(); // a JBoss Datasource
ps = con.prepareStatement("select * from table");
rs = ps.executeQuery();
while (rs.next()) {
// evaluation of ResultSet;
}
rs.close();
ps.close();
con.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
|
|