技术问题,求解答!


从性能及资源消耗方面考虑, 评论下面这段代码哪里可以做出改善?

public class Job {  private DataSource dataSource;  public void work() {  Connection conn1 = null;  Connection conn2 = null;  try {  conn1 = dataSource.getConnection();  conn2 = dataSource.getConnection();  // do something  } catch (SQLException e) {  e.printStackTrace();  } finally {  if (conn1 != null) {  try {  conn1.close();  } catch (SQLException e) {}  }  if (conn2 != null) {  try {  conn2.close();  } catch (SQLException e) {}  }  }  }  // getter/setter ...   }

public class Job {

private DataSource dataSource;

public void work() {

Connection conn1 = null;
Connection conn2 = null;

try {
conn1 = dataSource.getConnection();
conn2 = dataSource.getConnection();

// do something

} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn1 != null) {
try {
conn1.close();
} catch (SQLException e) {}
}
if (conn2 != null) {
try {
conn2.close();
} catch (SQLException e) {}
}
}
}

// getter/setter ...

}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务