各位大虾,最近小弟在做程序时碰到一个关于JDBC rs.updateRow()批量更新时的内存溢出问题,每当更新数据到30万左右时就会出现内存溢出问题,在网上查相关信息,但是问题还是没能得到解决,还望大虾们帮忙解决一下。在此小弟先谢各位了。代码如下:public void contrast(String strid){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
conn.setAutoCommit(false);
String sql = "select ver from test_tb_reference " +" where statistics_id='"+strid+"'";
System.out.println("--"+sql);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
int i=0;
while(rs.next()){
i++;
rs.updateString(1,"一版");
rs.updateRow();
if(i%500==0){
conn.commit();
System.gc();
}
System.out.println("---"+i);
}
conn.commit();
System.out.println("commit执行完毕!");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
System.out.println("连接关闭!");
}
}catch(Exception e2){
e2.printStackTrace();
}
}
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
conn.setAutoCommit(false);
String sql = "select ver from test_tb_reference " +" where statistics_id='"+strid+"'";
System.out.println("--"+sql);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
int i=0;
while(rs.next()){
i++;
rs.updateString(1,"一版");
rs.updateRow();
if(i%500==0){
conn.commit();
System.gc();
}
System.out.println("---"+i);
}
conn.commit();
System.out.println("commit执行完毕!");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
System.out.println("连接关闭!");
}
}catch(Exception e2){
e2.printStackTrace();
}
}
}
还有conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 有必要给那么多选项吗, 这个将使创建的语句的的性能下降.