JDBC addbatch批量处理数据时有最大值限制

在用jdbc向数据灌入数据时,发现120000的数据每次只能灌入50000多条,其他的就没有了。
在oracle 9i(windows 2003),oracle 10g(RHEL 4)上试验证有相同的结果。

使用定量灌入的办法,每5W条定义为一个事务,进行提交,将120000数据循环灌入,成功。
对于批量的update,delete操作两样有5W条左右的记录数限制。
结论:jdbc批量数据处理的每个批次是有数量的限制的。
我在本地环境中测试的限制量为每批54464条,其他配置的机器没有试过。

以下是插入数据时的代码:


ConnDB cd = new ConnDB();   
Connection ct = null;
PreparedStatement pst = null;
ResultSet rs = null;

ct = cd.getConn();

String insertSql = "insert into mytable (name,age) values(?,?)";

System.out.println(insertSql);
try {
ct.setAutoCommit(false); // 开始事务
pst = ct.prepareStatement(insertSql);
for (int i = 0; i < 120000; i++) {

pst.setString(1, "name" + i);
pst.setInt(2, i);
pst.addBatch();

System.out.println(i);
//分段提交
if((i%50000==0&& i!=0)||i== (120000 -1)){
pst.executeBatch();
ct.commit();
ct.setAutoCommit(false);// 开始事务
pst = ct.prepareStatement(insertSql);
System.out.println("------------>50000");
}

}
ct.commit();// 提交事务




} catch (SQLException e) {

try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}
e.printStackTrace();
} catch (RuntimeException e) {
try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}

} finally {
          cd.close(ct, pst, rs);
}

}

ConnDB cd = new ConnDB();
Connection ct = null;
PreparedStatement pst = null;
ResultSet rs = null;

ct = cd.getConn();

String insertSql = "insert into mytable (name,age) values(?,?)";

System.out.println(insertSql);
try {
ct.setAutoCommit(false); // 开始事务
pst = ct.prepareStatement(insertSql);
for (int i = 0; i < 120000; i++) {

pst.setString(1, "name" + i);
pst.setInt(2, i);
pst.addBatch();

System.out.println(i);
//分段提交
if((i%50000==0&& i!=0)||i== (120000 -1)){
pst.executeBatch();
ct.commit();
ct.setAutoCommit(false);// 开始事务
pst = ct.prepareStatement(insertSql);
System.out.println("------------>50000");
}

}
ct.commit();// 提交事务




} catch (SQLException e) {

try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}
e.printStackTrace();
} catch (RuntimeException e) {
try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}

} finally {
          cd.close(ct, pst, rs);
}

}


以下是删除数据时的代码:


ConnDB cd = new ConnDB();   
Connection ct = null;
PreparedStatement pst = null;
ResultSet rs = null;

ct = cd.getConn();

String deleteSql = "delete from mytable t where t.name=?";

System.out.println(deleteSql);

int[] intArray = new int[120000];

try {
ct.setAutoCommit(false); // 开始事务
pst = ct.prepareStatement(deleteSql);
for (int i = 0; i < 120000; i++) {

pst.setString(1, "name" + i);
pst.addBatch();

System.out.println(i);
// 分段提交
if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) {
intArray = pst.executeBatch();
ct.commit();
ct.setAutoCommit(false);// 开始事务
pst = ct.prepareStatement(deleteSql);
System.out.println("------------>50000");

}

}

ct.commit();// 提交事务

} catch (SQLException e) {

try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}
e.printStackTrace();
} catch (RuntimeException e) {
try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}

} finally {
cd.close(ct, pst, rs);
}

ConnDB cd = new ConnDB();
Connection ct = null;
PreparedStatement pst = null;
ResultSet rs = null;

ct = cd.getConn();

String deleteSql = "delete from mytable t where t.name=?";

System.out.println(deleteSql);

int[] intArray = new int[120000];

try {
ct.setAutoCommit(false); // 开始事务
pst = ct.prepareStatement(deleteSql);
for (int i = 0; i < 120000; i++) {

pst.setString(1, "name" + i);
pst.addBatch();

System.out.println(i);
// 分段提交
if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) {
intArray = pst.executeBatch();
ct.commit();
ct.setAutoCommit(false);// 开始事务
pst = ct.prepareStatement(deleteSql);
System.out.println("------------>50000");

}

}

ct.commit();// 提交事务

} catch (SQLException e) {

try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}
e.printStackTrace();
} catch (RuntimeException e) {
try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}

} finally {
cd.close(ct, pst, rs);
}

}


以下是更新数据的代码:

ConnDB cd = new ConnDB();   
Connection ct = null;
PreparedStatement pst = null;
ResultSet rs = null;

ct = cd.getConn();
String deleteSql = "update mytable t set t.age =20 where t.name=?";

System.out.println(deleteSql);

int[] intArray = new int[120000];

try {
ct.setAutoCommit(false); // 开始事务
pst = ct.prepareStatement(deleteSql);
for (int i = 0; i < 120000; i++) {

pst.setString(1, "name"+i);
pst.addBatch();

System.out.println(i);
// 分段提交
if ((i % 50000 == 0 && i != 0) || i == (120000 - 1)) {
intArray = pst.executeBatch();
ct.commit();
ct.setAutoCommit(false);// 开始事务
pst = ct.prepareStatement(deleteSql);
System.out.println("------------>50000");

}

}

ct.commit();// 提交事务

} catch (SQLException e) {

try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}
e.printStackTrace();
} catch (RuntimeException e) {
try {
ct.rollback();
} catch (SQLException e1) {

e1.printStackTrace();
}

} finally {
cd.close(ct, pst, rs);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值