You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Unreleased_DB_Resource.java 1.8 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package Unreleased_DB_Resource;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.logging.Logger;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class Unreleased_DB_Resource {
  10. static final Logger log = Logger.getLogger("local-logger");
  11. public void bad(HttpServletRequest request, HttpServletResponse response,Connection conn)
  12. {
  13. PreparedStatement statement = null;
  14. ResultSet rs = null;
  15. try {
  16. statement = conn.prepareStatement("select name from users where name = 'lily'"); // bad 数据库资源未释放
  17. rs = statement.executeQuery();
  18. rs.close();
  19. } catch (SQLException e1) {
  20. log.info("SQLException");
  21. }
  22. }
  23. public void good(HttpServletRequest request, HttpServletResponse response,Connection conn)
  24. {
  25. PreparedStatement statement = null;
  26. ResultSet rs = null;
  27. try {
  28. statement = conn.prepareStatement("select name from users where name = 'lily'"); // good 数据库资源未释放
  29. rs = statement.executeQuery();
  30. } catch (SQLException e1) {
  31. log.info("SQLException");
  32. }finally {
  33. try {
  34. if( rs != null )
  35. {
  36. rs.close();
  37. }
  38. } catch (SQLException se) {
  39. log.info("Error closing conn");
  40. }
  41. try {
  42. if( statement != null )
  43. {
  44. statement.close();
  45. }
  46. } catch (SQLException se) {
  47. log.info("Error closing conn");
  48. }
  49. try {
  50. if( conn != null )
  51. {
  52. conn.close();
  53. }
  54. } catch (SQLException se) {
  55. log.info("Error closing conn");
  56. }
  57. }
  58. }
  59. }

No Description

Contributors (1)