java代码读取dbsequence的值_MongoDB自增序列实现 - Java多线程同步 synchronized 用法

本文探讨了在MongoDB环境中如何在多线程下实现有序自增ID,通过模拟数据库Sequence模型,展示了不同同步策略的效果,包括非同步、同步方法和同步对象,最终提出了一种基于ConcurrentHashMap和SequenceLock的解决方案,实现了高效且正确的序列分配,适用于分布式ID生成场景。
摘要由CSDN通过智能技术生成

在使用MongoDB的时候 (基于spring-mongo) ,我想在插入对象时获取有序自增的主键 ,但是MongoDB的默认规则是生成一串无序 (大致有序) 的字串 .而Spring Data提供的主键生成方法也是随机的 String/BigInteger.

因为分布式情况下 ,有序ID会变得困难 ( ID中心/分布式锁 )

同步问题

获取有序ID的通常做法是 :

创建sequence : key-start-end-step-current 标识/起值/止值/步长/当前值

获取sequence ,current作为主键值

保存current = current + step到数据库作为下一个主键值

但是在多线程情况下 , 2-3可能被多个线程同时运行 ,导致sequence还未保存成功就被下一个获取.

模拟数据库Sequence模型

@AllArgsConstructor

public class Sequence {

@Setter

long current;

public long getCurrent() {

transTime();

return current;

}

public void setCurrent(long current) {

transTime();

this.current = current;

}

private void transTime() {

try {

Thread.sleep(3);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public class SequenceServiceTest {

Sequence A;//A型sequence

Sequence B;//B型sequence

/**

* 初始化数据

*/

@Before

public void before() {

A = new Sequence(1L);

B = new Sequence(1L);

}

/**

* 模拟数据库取值

*/

private Sequence getSequence(String name) {

switch (name) {

case "A":

return A;

case "B":

return B;

default:

return null;

}

}

private void waitService(ExecutorService executorService) {

executorService.shutdown();

try {

while (!executorService.awaitTermination(1000, TimeUnit.SECONDS)) {

}

System.out.println("final A:" + A.getCurrent());

System.out.println("final B:" + B.getCurrent());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

非同步代码读写sequence

@Test

public void noSynchronizedTest() {

//10个用户同时需要获取id

ExecutorService executorService = Executors.newFixedThreadPool(10);

//一部分需要A ,一部分需要B

for (int i = 0; i < 1000; i++) {

e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值