简体   繁体   English

如何使用 JPA 和 Hibernate 的多个处理线程来持久化实体

[英]How to persist entities using multiple processing threads with JPA and Hibernate

There is one job which is running in a single threaded environment.有一个作业在单线程环境中运行。 This means I have main method and the main thread is responsible for completing the job.这意味着我有 main 方法,主线程负责完成工作。

I am using Spring and Hibernate.我正在使用 Spring 和 Hibernate。

On the high level I'm doing the following steps:在高层次上,我正在执行以下步骤:

  1. fetching data from a MySQL database using JDBC ( looping resultset and doing point 2. and 3. )使用 JDBC 从 MySQL 数据库中获取数据(循环结果集并执行第 2 点和第 3 点)

  2. populating a Model using the data received from point 1.使用从第 1 点接收的数据填充模型。

  3. validation, calling service layer, dao layer and storing entity in oracle db.验证,调用service层,dao层,在oracle db中存储实体。

This flow is using a for loop.此流程使用for循环。 So 1 by 1 data insertion is there.所以 1 x 1 数据插入是存在的。

Now I want to do it in using multi threading.现在我想使用多线程来做到这一点。

Approach :方法 :

  1. one thread will fetch the data and populate model object and put it in the queue.一个线程将获取数据并填充模型对象并将其放入队列中。

  2. multiple threads will dequeue object from queue and start point 3.多个线程将从队列和起点 3 中取出对象。

Can you help me in implementing this model.你能帮我实现这个模型吗? How to code this type of multithreading framework.如何编写这种类型的多线程框架。

You can do it like this:你可以这样做:

  1. Define an ExecutorService:定义一个 ExecutorService:

     ExecutorService executorService = Executors.newFixedThreadPool(10);
  2. Each for iteration should simply submit the new object to be inserted每个for迭代应该简单地提交要插入的新对象

     final RecordDTO record = ...; executorService.execute(new Runnable() { public void run() { insertService.save(record); } });
  3. The insertService will have a @Transactional save method to insert each record using one of those 10 worker threads. insertService将有一个@Transactional save方法,使用这 10 个工作线程之一插入每条记录。

  4. The connection pool size should be greater or equal to the number of workers.连接池大小应大于或等于工作线程数。

However, it's much more efficient to use JDBC batch updates .但是,使用 JDBC batch updates效率更高。 Therefore, instead of sending just one entity to the worker thread, you can send multiple entries so that they are all inserted in one batch.因此,不是只向工作线程发送一个实体,您可以发送多个条目,以便它们全部插入一批。

you need a single-producer multiple-consumer thread-safe queue.您需要一个单生产者多消费者线程安全队列。 Take a look at LMAX disruptor , that's the best suit for you.看看LMAX 破坏者,那是最适合你的。

LMAX Disruptor is a High Performance Inter-Thread Messaging Library and it's the default messaging system for Intra-worker communication in Apache Storm The underlying data structure is a lock-free ring buffer. LMAX Disruptor 是一个高性能的线程间消息传递库,它是Apache Storm 中工作线程内通信的默认消息传递系统。底层数据结构是一个无锁环形缓冲区。 To make it fast, it use a lot of tricks to reduce false sharing为了让它更快,它使用了很多技巧来减少虚假共享

follow theget started tutorial , there is a very simple example you could refer to.按照入门教程进行操作,您可以参考一个非常简单的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM