日期:2014-05-16  浏览次数:20486 次

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 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);
		}

	}





1 楼 hunnuxiaobo 2011-10-18