Search |
||
Patch Leaky Connections with AOPPosted by crazybob on March 5, 2004 at 1:23 PM PST
In a recent thread on TheServerSide.com, readers voiced concern over JDBC drivers and connection pools that fail to close Statements and ResultSets when you call Erring on the side of caution, many developers write JDBC code like the following example or rely on frameworks to create and close their ResultSets for them:
Connection c = ...;
Statement s = null;
ResultSet rs = null;
try {
// some JDBC code...
}
finally {
if (rs != null) {
try {
rs.close();
}
catch (Exception e) {}
}
if (s != null) {
try {
s.close();
}
catch (Exception e) {}
}
try {
c.close();
}
catch (Exception e) {}
}
Rather than repeating the same code over and over again or limiting yourself to one ResultSet at a time, I suggest a proxy-based design: wrap the Connection, track the child resources and close them when the user calls
The resulting proof of concept reuses code in ways its OO counterpart could only dream of. In my design, Connections contain Statements which in turn contain ResultSets. I turn the Connections and Statements into containers and add their respective children using generic container advice. Additional advice applied to the
Connection c = LeakyConnection.wrap(...);
try {
// some JDBC code...
}
finally {
try {
// this automatically cleans up resources.
c.close();
}
catch (Exception e) {}
}
»
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|