Java JDBC: MySQL write Out of Memory -


where memory leak in code? function executed millions of times extensive usage of memory, causing out of memory exception after 2.4million execusions.

public static void savecall(call call) {         conn = getinstance();          if (conn != null) {             try {                 calendar.settime(call.getdate());                 string sql = "insert calls(id, datetime, duration, customer_phone_id, partner_phone_id) "                         + "values(null, ?, ?, ?, ?)";                 preparedstatement preparedstatement = conn                         .preparestatement(sql);                 preparedstatement.setstring(1, dateformat.format(calendar.gettime()));                 preparedstatement.setlong(2, call.getduration());                 preparedstatement.setlong(3, call.getphone().getphonenumber());                 preparedstatement.setlong(4, call.getphonepartner()                         .getphonenumber());                  preparedstatement.executeupdate();             } catch (sqlexception e) {                 e.printstacktrace();             }         }     } 

my 2 cents

jdbc tricky.

in case, preparedstatement should closed in order avoid resource leak.

public static void savecall(call call) {     conn = getinstance();      if (conn != null) {         calendar.settime(call.getdate());         string sql = "insert calls(id, datetime, duration, customer_phone_id, partner_phone_id) "                     + "values(null, ?, ?, ?, ?)";          preparedstatement preparedstatement = null;                     try {                            conn.preparestatement(sql);             preparedstatement.setstring(1, dateformat.format(calendar.gettime()));             preparedstatement.setlong(2, call.getduration());             preparedstatement.setlong(3, call.getphone().getphonenumber());             preparedstatement.setlong(4, call.getphonepartner()                     .getphonenumber());              preparedstatement.executeupdate();         } catch (sqlexception e) {             e.printstacktrace();         } {             if (preparedstatement != null) {                 preparedstatement.close();             }         }     } } 

here java 7 solution:

public static void savecall(call call) {     conn = getinstance();      if (conn != null) {         calendar.settime(call.getdate());         string sql = "insert calls(id, datetime, duration, customer_phone_id, partner_phone_id) "                     + "values(null, ?, ?, ?, ?)";          try (preparedstatement preparedstatement = conn.preparestatement(sql)) {                             preparedstatement.setstring(1, dateformat.format(calendar.gettime()));             preparedstatement.setlong(2, call.getduration());             preparedstatement.setlong(3, call.getphone().getphonenumber());             preparedstatement.setlong(4, call.getphonepartner()                     .getphonenumber());              preparedstatement.executeupdate();         } catch (sqlexception e) {             e.printstacktrace();         }     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -