解决内存不足错误

时间:2017-05-23 14:01:23

标签: java postgresql openedge

我创建了一个使用AppServer连接到Progress SmartDataObject的java程序。 SmartDataObject为我提供了一个(进度)数据库的结果集。 目前我正在测试一个有100万条目的表。

我必须将Recs从Progress复制到Postgres数据库并更新进度数据库(将某些状态从10设置为80)。

使用40000条记录进行测试工作正常。

但是我继续使用当前测试来解决堆大小的问题。 我已经尝试了-Xms2g,4g,-Xmx4g,之后就将其删除了。

我无法控制从Progress数据库获得的记录数量。 我对两个数据库都使用批量更新。

(我不是一位经验丰富的java程序员)

public static void main(String[] args) {
     final String WRITEREC = "INSERT INTO test "
                           + "(type, fld1, fld2, fld3, fld4, fld5, fld6, fld7, fld8) "
                           + "VALUES (?,?,?,?,?,?,?,?,?)"
                           ;

     boolean lOke = true;
     long start = System.currentTimeMillis();

     Properties properties = new Properties();
     URL url = ClassLoader.getSystemResource("config.properties");
     try {
         properties.load(url.openStream());
     } catch (IOException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
         return;
     }

     try {
         Class.forName("org.postgresql.Driver").newInstance();
     } catch (InstantiationException  | IllegalAccessException | ClassNotFoundException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
         return;
     }

     String SDOApp;
     String myOS = System.getProperty("os.name");

     if (myOS.contains("Windows")) {
         SDOApp = properties.getProperty("SDOApp_Windows");
     }
     else {
         SDOApp = properties.getProperty("SDOApp_Linux");
     }

     String SDO = properties.getProperty("SmartDataObject");

     SDOAppObject appObj;
     SDOResultSet rs = null;

     PreparedStatement statement;
     Timestamp ts = new Timestamp(System.currentTimeMillis());
     Connection con = null;

     try {
         appObj = new SDOAppObject(SDOApp, "", "", "");
         rs = appObj._createSDOResultSet(SDO);
         appObj._release();
     } catch (Open4GLException | ProSQLException | IOException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
         return;
     }

     try {
         con = DriverManager.getConnection(properties.getProperty("Host"), properties.getProperty("User"), properties.getProperty("Wachtwoord"));
         statement = con.prepareStatement(WRITEREC, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
         con.setAutoCommit(false);
     } catch (SQLException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
         return;
     }

     PGobject jsonObject = new PGobject();
     jsonObject.setType("jsonb");

     try {
         while (rs.next()) {
             /* Postgres */
             statement.setString(1, rs.getString("fld1"));
             statement.setString(2, rs.getString("fld2"));
             statement.setString(3, rs.getString("fld3"));
             statement.setTimestamp(4, rs.getTimestamp("fld4"));
             statement.setString(6, rs.getString("fld6"));
             statement.setString(8, rs.getString("fld8"));
             statement.setTimestamp(9, ts);

             jsonObject.setValue(rs.getString("fld5"));
             statement.setObject(5, jsonObject);
             //                if ((jsonString != null) & (!jsonString.isEmpty())) {
             //                    jsonObject.setValue(jsonString);
             //                }

             jsonObject.setValue(rs.getString("fld6"));
             statement.setObject(7, jsonObject);

             //                if ((jsonString != null) & (!jsonString.isEmpty())) {
             //                    jsonObject.setValue(rs.getString("cVorig"));
             //                }

             statement.addBatch();

             /* Progress */
             rs.updateInt("iStatus", 80);
             rs.updateRow();
         }

         rs.sendBatch();
         statement.executeBatch();

     } catch (ProSQLException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
     } catch (OutOfMemoryError e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
     } catch (SQLException e) {
         System.out.println("Some message " + e.getMessage());
         lOke = false;
     }

     finally {
         long millis = System.currentTimeMillis() - start;
         long second = (millis / 1000) % 60;
         long minute = (millis / (1000 * 60)) % 60;
         long hour = (millis / (1000 * 60 * 60)) % 24;
         String time = format("%02d:%02d:%02d:%d", hour, minute, second, millis);

         if (lOke) {
             System.out.println("Ready " + time);

         } else {
             try {
                 if (!con.isClosed()) {
                     con.rollback();
                 }
             } catch (SQLException e) {
                 System.out.println("Some message " + e.getMessage());
             }

             System.out.println("Some message " + time);
         }

         try {
             rs.close();
             statement.close();

             con.setAutoCommit(true);
         } catch (SQLException e) {}

0 个答案:

没有答案