简体   繁体   English

Spring 的水平放大问题数据 JPA 批量/批量插入:PostgreSQL

[英]Horizontal scale up issue with Spring Data JPA Batch/bulk insert : PostgreSQL

I have implemented the Spring data JPA batch insert and it's working perfectly fine.我已经实施了 Spring 数据 JPA 批量插入,并且运行良好。 Also I have seen very good improvement in performance.我也看到了非常好的性能改进。 For this implementation I have used Spring, Spring Data JPA and PostgreSQL.对于此实现,我使用了 Spring、Spring 数据 JPA 和 PostgreSQL。

If I scale up my application horizontally(like have 2 node in the cluster).The application is not working because the application in each node have same logic to generate the value of primary key.如果我水平扩展我的应用程序(比如集群中有 2 个节点)。应用程序无法运行,因为每个节点中的应用程序具有相同的逻辑来生成主键的值。 It means, two node are generating the same primary key and trying to insert the records.这意味着,两个节点正在生成相同的主键并尝试插入记录。 As this violating primary key constraints, service is getting failed.由于这违反了主键约束,服务将失败。

Is there any strategy to generate the primary key which will be unique multi-threaded environment(The application is deployed in multiple nodes)?是否有任何策略来生成唯一的多线程环境(应用程序部署在多个节点中)的主键?

I have referred this My implementation is perfectly working fine when there is only one node in cluster.我已经提到当集群中只有一个节点时,我的实现工作得很好。

I came to know that if the entities use GenerationType.IDENTITY identifier generator, Hibernate will silently disable batch inserts/updates.我开始知道如果实体使用 GenerationType.IDENTITY 标识符生成器,Hibernate 将静默禁用批量插入/更新。 Also I have tried with "GenerationType.SEQUENCE" but have seen the same issue.On further research, I have seen some comments in stack-overflow question that since mysql/PostgreSQL doesn't support sequence我也尝试过使用“GenerationType.SEQUENCE”,但看到了同样的问题。在进一步的研究中,我在堆栈溢出问题中看到了一些评论,因为 mysql/PostgreSQL 不支持序列

The main two algorithms that are worth to consider are HiLo which reserves a batch of ids on the db side and uses them one by one on the application side.值得考虑的主要两种算法是HiLo ,它在 db 端保留一批 id,并在应用程序端一个一个地使用它们。 Or just using UUIDs which are constructed in a way that they are unique without requiring any synchronization .或者只是使用以不需要任何同步的方式构造的 UUID

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

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