void problemCode1(DataSource ds)
{
Connection conn = ds.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM MY_TABLE");
//...
conn.close();
}
void problemCode2(DataSource ds)
{
Connection conn = null;
try
{
Connection conn = ds.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM MY_TABLE");
//...
}
finally
{
conn.close();
}
}
void problemCode3(DataSource ds)
{
Connection conn = ds.getConnection();;
try
{
//...
}
finally
{
conn.getAutoCommit(); // can throw an exeption and next line never execute
conn.close();
}
}
|