jdbc批处理+手动事务+多线程实现81秒插入1000万数据(多线程版)

  • 现在来试试多线程能够多少秒钟插入千万数据

/**
 * @Author: guandezhi
 * @Date: 2019/4/13 15:35
 */
public class JdbcUtils {

    private static String url = "jdbc:mysql://localhost:3306/user?useSSL=false&rewriteBatchedStatements=true";
    private static String user = "root";
    private static String password = "root";

    private static ExecutorService threadPool = Executors.newFixedThreadPool(50);


    public static void executeBatch() {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            String sql = "insert into user(name, mobile) values(?,?)";
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (int j = 0; j < 100000; j++) {
                String mobile = "13356544556";
                Integer randNum = 0;
                Random random = new Random();
                for (int i = 0; i < 1000; i++) {
                    randNum = random.nextInt(10);
                }
                ps.setString(1, "官德志");
                ps.setString(2, mobile + String.valueOf(randNum));
                ps.addBatch();
            }
            ps.executeBatch();
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        long beginTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            threadPool.execute(() -> {
                try {
                    JdbcUtils.executeBatch();
                } catch (Exception e) {
                    System.out.println("插入数据异常");
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
        long endTime = System.currentTimeMillis();
        System.out.println("插入一千万数据用时:" + (endTime - beginTime) / 1000 + " 秒");
        threadPool.shutdown();
    }
}
  • 测试一下

 

 

总结: 

      1.多线程确实比单线程快很多

      2.此处开启多线程容易造成OOM,需要合理的设置线程大小和JVM参数。

 

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
您可以参考以下示例代码来实现Spring Boot与MyBatis集成,使用Oracle数据库进行多线程快速插入数据,并保证事务的一致性。 首先,您需要在pom.xml文件中添加所需的依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- Oracle JDBC Driver --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.8.0.0</version> </dependency> </dependencies> ``` 接下来,创建一个实体类来表示您要插入数据: ```java public class Data { private String name; // 其他字段... // Getter和Setter方法... } ``` 然后,创建一个Mapper接口来定义数据访问的方法: ```java @Mapper public interface DataMapper { void insertData(Data data); } ``` 接着,在application.properties文件中配置数据库连接信息: ```properties spring.datasource.url=jdbc:oracle:thin:@localhost:1521/ORCL spring.datasource.username=your-username spring.datasource.password=your-password spring.datasource.driver-class-name=oracle.jdbc.OracleDriver ``` 现在,您可以编写一个Service类来执行插入操作: ```java @Service public class DataService { @Autowired private DataMapper dataMapper; @Transactional public void insertMillionData() { ExecutorService executorService = Executors.newFixedThreadPool(10); // 使用10个线程插入数据 for (int i = 0; i < 1000000; i++) { final int index = i; executorService.execute(() -> { Data data = new Data(); data.setName("Data " + index); // 设置其他字段... dataMapper.insertData(data); }); } executorService.shutdown(); try { executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } ``` 最后,在启动类中添加@SpringBootApplication注解,并在main方法中调用DataService的insertMillionData方法: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); // 调用插入数据的方法 ApplicationContext context = SpringApplication.run(Application.class, args); DataService dataService = context.getBean(DataService.class); dataService.insertMillionData(); } } ``` 以上示例代码演示了如何使用Spring Boot和MyBatis实现多线程快速插入数据,并保证事务的一致性。您可以根据实际需求进行适当的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值