jdbc连接一定要关闭connection, resultset和preparedstatement

今天写了个程序往数据库插入100万条记录,代码如下

Java代码 复制代码 收藏代码
  1. publicstaticvoid insertRecords() throws Exception {
  2. Connection conn = null;
  3. try {
  4. conn = DBUtil.getConnection();
  5. conn.setAutoCommit(false);
  6. while (count < TOTALNUM) {
  7. PreparedStatement ps = conn.prepareStatement("select * from custfund limit ?");
  8. ps.setInt(1, COPYNUM);
  9. ResultSet rs2 = ps.executeQuery();
  10. while (rs2.next() && count < TOTALNUM) {
  11. PreparedStatement ps2 = conn
  12. .prepareStatement("sql here...");
  13. ps2.setInt(1, ++maxCustId);
  14. ps2.setFloat(2, rs2.getFloat(2));
  15. ps2.setFloat(3, rs2.getFloat(3));
  16. ps2.setFloat(4, rs2.getFloat(4));
  17. ps2.setFloat(5, rs2.getFloat(5));
  18. ps2.setFloat(6, rs2.getFloat(6));
  19. ps2.setFloat(7, rs2.getFloat(7));
  20. ps2.setInt(8, rs2.getInt(8));
  21. ps2.executeUpdate();
  22. ps2.close();
  23. count++;
  24. if (count % 2000 == 0) {
  25. System.out.println(count);
  26. long curtime = new Date().getTime();
  27. long timeUsed = curtime - time;
  28. int remaincount = TOTALNUM - count;
  29. System.out.println("remaining time:" + (remaincount / 2000) * timeUsed / 1000 + "s");
  30. time = curtime;
  31. conn.commit();
  32. }
  33. }
  34. rs2.close();********
  35. ps.close();********
  36. }
  37. System.out.println("total time:" + (new Date().getTime() - starttime) / 1000 + "S");
  38. } finally {
  39. if (conn != null) {
  40. conn.close();
  41. }
  42. }
  43. }
public static void insertRecords() throws Exception {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
                      
            while (count < TOTALNUM) {
                PreparedStatement ps = conn.prepareStatement("select * from custfund limit ?");
                ps.setInt(1, COPYNUM);
                ResultSet rs2 = ps.executeQuery();
                while (rs2.next() && count < TOTALNUM) {
                    PreparedStatement ps2 = conn
                            .prepareStatement("sql here...");
                    ps2.setInt(1, ++maxCustId);
                    ps2.setFloat(2, rs2.getFloat(2));
                    ps2.setFloat(3, rs2.getFloat(3));
                    ps2.setFloat(4, rs2.getFloat(4));
                    ps2.setFloat(5, rs2.getFloat(5));
                    ps2.setFloat(6, rs2.getFloat(6));
                    ps2.setFloat(7, rs2.getFloat(7));
                    ps2.setInt(8, rs2.getInt(8));
                   
                    ps2.executeUpdate();
                    ps2.close();
                    count++;
                    if (count % 2000 == 0) {
                        System.out.println(count);
                        long curtime = new Date().getTime();
                        long timeUsed = curtime - time;
                        int remaincount = TOTALNUM - count;
                        System.out.println("remaining time:" + (remaincount / 2000) * timeUsed / 1000 + "s");
                        time = curtime;
                        conn.commit();
                    }
                }
                rs2.close();********
                ps.close();********
            }
            System.out.println("total time:" + (new Date().getTime() - starttime) / 1000 + "S");
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    }

 

在****处如果不对rs, ps close,就会有java虚拟机out of memory.

按理说java虚拟机会自动垃圾回收,但为什么这里会这样呢?

原因是rs, ps没有关闭,java虚拟机认为它还在被使用。很多循环以后,就内存满了。

posted @ 2012-05-28 08:58  血马雄风  阅读(1813)  评论(0)    收藏  举报