sqlserver2000中使用JDBC,addBatch方法,报内存溢出???

lyx262 2010-10-19 01:39:33
望高手指导!
异常:
Exception in thread "Thread-5" java.lang.OutOfMemoryError: Java heap space
at com.microsoft.jdbc.base.BaseParameters.<init>(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.addBatch(Unknown Source)
伪代码:list为数据集,list数据达到一定数据量后执行以下for循环插入数据,插入完成后清空list。再填充list,执行以下循环....往复循环此操作,当循环10次是没有问题的,大于10次后就报如上错误。


for ( list//数据集)
{

StringBuilder commandBuilder = new StringBuilder(
"INSERT INTO [" + flowType + "] VALUES(?" );
for ( ... )
{
commandBuilder.append( ", ?" );
}
commandBuilder.append( ")" );
pstmt = conn.getConnection().prepareStatement( command );
// 缓冲区中的数据量
int rowCount = 0;
for ( ... )
{

pstmt.setString( j , *** );
pstmt.addBatch();

if ( rowCount == 1000 )
{
conn.getConnection().setAutoCommit( false );
pstmt.executeBatch();
conn.getConnection().setAutoCommit( true );
pstmt.clearBatch();
rowCount = 0;
}
}
if ( rowCount != 0 )
{
conn.getConnection().setAutoCommit( false );
pstmt.executeBatch();
conn.getConnection().setAutoCommit( true );
}
list.clear();//数据集清空
} catch ( Exception e )
{
log.error( "", e );
} finally
{

try
{
if ( pstmt != null )
{
pstmt.close();
}

if ( conn != null )
{
conn.close();
}

} catch ( SQLException e )
{
log.error( "", e );
}

}
}
...全文
263 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2010-10-20
  • 打赏
  • 举报
回复
prepareStatement不支持很多参数,

int rowCount = 0;
for ( ... )
{

pstmt.setString( j , *** );
pstmt.addBatch();

if ( rowCount == 1000 )
{
conn.getConnection().setAutoCommit( false );
pstmt.executeBatch();
conn.getConnection().setAutoCommit( true );
pstmt.clearBatch();
rowCount = 0;
//这里应该再清空list list.clear();//数据集清空
}
}
if ( rowCount != 0 )//这里逻辑有问题,同时还须判断list是否为空 list.count
{
conn.getConnection().setAutoCommit( false );
pstmt.executeBatch();
conn.getConnection().setAutoCommit( true );
}
list.clear();//数据集清空
lyx262 2010-10-20
  • 打赏
  • 举报
回复
经过反复测试,发现这段没有问题,内存溢出是其他地方内存泄露。结贴.
ForFumm 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 claro 的回复:]
不了解,帮顶。
[/Quote]0
lyx262 2010-10-19
  • 打赏
  • 举报
回复
怎么就没人回答呢??????
lyx262 2010-10-19
  • 打赏
  • 举报
回复
大家都来帮忙看看,那里肯定有内存泄漏的地方,所以这样一直累积才会溢出。
Mr_Nice 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]

涉及到C语言 望着了
[/Quote]

深了,帮顶...
lyx262 2010-10-19
  • 打赏
  • 举报
回复
有没有谁碰到过同样的问题呢?
lyx262 2010-10-19
  • 打赏
  • 举报
回复
代码逻辑:pstmt.addBatch();到1000条后,执行 pstmt.executeBatch();插入数据。
关键是我的list数据量很大,插入完后,清空list,再次封装list,再插入这样好多循环。到10几次循环以上就溢出了。

--小F-- 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lyx262 的回复:]
哪是c,是java用JDBC。PreparedStatement的addBatch()方法
[/Quote]

一样不会 呵呵
lyx262 2010-10-19
  • 打赏
  • 举报
回复
哪是c,是java用JDBC。PreparedStatement的addBatch()方法
--小F-- 2010-10-19
  • 打赏
  • 举报
回复
涉及到C语言 望着了

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧