JDBC executeBatch批量插入数据
JDBC executeBatch批量插入数据
con.setAutoCommit(false);
项目中遇到上传报盘批量处理数据,通过从jpa保存–>改成如下批量处理,10万条数据时间从5个小时–>3分钟;
/**
* 插入数据
*
* @param apply
*/
@Override
// @Transactional(readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void insertDetailForBatch(EmpSalaryApply apply) {
Long startTime = 0L;
Long endTime = 0L;
Connection con = null;
PreparedStatement pst = null;
try {
String url = gfurl; // orcl为数据库的SID
String user = gfusername;
String password = gfpassword;
StringBuffer sql = new StringBuffer("insert into apply_com_salary_emps (ID, name)");
sql.append("VALUES(?,?)");
Class.forName(gfdriverClassName);
con = (Connection) DriverManager.getConnection(url, user, password);
// 关闭事务自动提交
con.setAutoCommit(false);
final int batchSize = 5000;
int count = 0;
int batchNumber=0;
startTime = System.currentTimeMillis();
pst = (PreparedStatement) con.prepareStatement(sql.toString());
List <EmpSalaryDetail> list = apply.getEmpSalaryDetails();
/**
* 第一条执行时已经保存,直接从第二条执行
* 即i=1
*/
for (int i = 1; i < list.size(); i++) {
EmpSalaryDetail empSalaryDetail = list.get(i);
pst.setString(1, apply.getId());
pst.setString(2, empSalaryDetail.getName());
// 把一个SQL命令加入命令列表
pst.addBatch();
if (++count % batchSize == 0) {
++batchNumber;
System.out.println("apply.getId()" + apply.getId()+"遍历"+batchNumber);
pst.executeBatch();
count = 0;
pst.clearBatch();
}
}
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();
endTime = System.currentTimeMillis();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ResourceNotFoundException("整体数据保存失败!");
} catch (SQLException e) {
e.printStackTrace();
throw new ResourceNotFoundException("整体数据保存失败!");
} finally {
System.out.println("用时:" + (endTime - startTime));
try {
pst.close();
con.close();
System.out.println("连接关闭" );
} catch (SQLException e) {
e.printStackTrace();
throw new ResourceNotFoundException("整体数据保存失败!");
}
}
}