qq_38983343 2022-06-04 20:32 采纳率: 16.7%
浏览 105
已结题

JDBC连接池去做批量导入,每次运行500万数据,但是在中间就会出各种问题

自己写了一个JDBC连接池去做批量导入,每次运行500万数据,但是在中间就会出各种问题

问题1:The last packet successfully received from the server was 18,967 milliseconds ago. The last packet sent successfully to the server was 18,963 milliseconds ago.
对于问题一 这个时间默认是8小时,我刚起项目,运行不到30分钟,怎么会连接过期呢?网上说的去改哪个out时间,我也改过无效?单是看这个时间觉得也不是网上说的那样?
问题2:截了个图可以看图片

img


问题3:也是提交的时候报的错误;

代码也贴上了,每次报错都是提交的时候报的错误,ps.executeBatch();;


```java
    private void saveForecastSales(List<ForecastSalesDO> forecastSalesDOS, int count) {
        try {
            if (count > 2) {
                log.info("JDBCOperationServiceImpl->saveForecastSales:: 尝试两次无果");
                return;
            }
            Connection connection = dbPool.getConnection();
            if (!connection.isValid(1000)){
                dbPool.removeConnection(connection);
                log.error("JDBCOperationServiceImpl->saveForecastSales::connection ");
                connection = DbUtil.createConnection();
            }
            PreparedStatement  ps = connection.prepareStatement(JDBCUtils.SQL);
            connection.setAutoCommit(false);
            forecastSalesDOS.forEach(e -> {
                try {
                    ps.setObject(1, e.getSku());
                    ps.setObject(2, e.getErpSku());
                    ps.setObject(3, e.getSite());
                    ps.setObject(4, e.getMarketId());
                    ps.setObject(5, DateUtil.getMinusHours(e.getPaidTime()));
                    ps.setObject(6, e.getAmount());
                    ps.setObject(7, e.getPlatformId());
                    ps.setObject(8, e.getOrderType());
                    ps.setObject(9, e.getOrderCategory());
                    ps.setObject(10, e.getAsin());
                    ps.setObject(11, e.getIdealWarehouse());
                    ps.setObject(12, e.getActualFreight());
                    ps.setObject(13, e.getActualWarehouse());
                    ps.setObject(14, e.getCountry());
                    ps.setObject(15, e.getSimulatedNetProfit());
                    ps.setObject(16, e.getOrderTotalPrice());
                    ps.setObject(17, e.getOrderCode());
                    ps.setObject(18, e.getOrderCurrency());
                    ps.setObject(19, DateUtil.getMinusHours(e.getOrderCreatedTime()));
                    ps.setObject(20, e.getOrderSn());
                    ps.setObject(21, e.getFreight());
                    ps.setObject(22, e.getUnitPrice());
                    ps.setObject(23, e.getIsFba());
                    ps.setObject(24, DateUtil.getMinusHours(e.getCreatedTime()));
                    ps.setObject(25, e.getCreatedBy());
                    ps.setObject(26, DateUtil.getMinusHours(e.getUpdatedTime()));
                    ps.setObject(27, e.getUpdatedBy());
                    ps.setObject(28, e.getIsDelete());
                    ps.addBatch();
//                    ps.clearParameters();
                } catch (SQLException throwables) {
                    log.info("内层:JDBCOperationServiceImpl->batchForecastSales:异常,{}", throwables.getMessage());
                    throw new RuntimeException(throwables);
                }
            });
            DbUtil.executeBatchInsert(connection, ps);
            dbPool.releaseConnection(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            log.info("外层:JDBCOperationServiceImpl->batchForecastSales:异常,SQLException::{}", throwables.getMessage());
            count++;
            log.info("尝试中:::::");
            this.saveForecastSales(forecastSalesDOS, count);
        }
    }

public static void executeBatchInsert(Connection connection, PreparedStatement ps) throws SQLException {
   if(ps==null){
       log.info("executeBatchInsert->为空");
   }
    ps.executeBatch();
    connection.commit();
    ps.clearBatch();
    closePs(ps);
}

```

  • 写回答

4条回答 默认 最新

  • qq_38983343 2022-06-05 13:49
    关注

    一个能打的都没有吗?

    评论

报告相同问题?

问题事件

  • 系统已结题 6月12日
  • 创建了问题 6月4日

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?