Neo.TransientError.Transaction.DeadlockDetected

最近在倒腾neo4j,在使用多线程方式往neo4j插入关系数据时,有些线程抛出异常:

Neo.TransientError.Transaction.DeadlockDetected

查了下原因,在创建关系数据时(nodea->nodeb),nodeb(endnode)会被write-lock,当这个节点上了写锁后,其他的线程就会抛出这个异常,解决办法就是让相同的endnode使用同一个线程处理,我这边直接将相同的endnode的相关数据放在了同一个文件,一个线程单独处理一个文件,避免多线程处理统一个endnode

 

好吧,这事我想简单了,测试一段时间后,还是出现了这个异常,但比之前好很多;为了解决这个问题换了一种方案,添加重试机制,先引入maven坐标

<dependency>
            <groupId>com.github.rholder</groupId>
            <artifactId>guava-retrying</artifactId>
            <version>2.0.0</version>
        </dependency>

具体实现如下

public static void executeSql(String sql) throws Exception {
        Class.forName("org.neo4j.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:neo4j:http://hadoop:7474?user=neo4j&password=neo4j");
        con.setAutoCommit(false);
        Statement statement = con.createStatement();

        Callable<Integer> task = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                statement.execute(sql);
                return 1;
            }
        };

        Retryer<Integer> retryer = RetryerBuilder.<Integer>newBuilder()
                .retryIfResult(Predicates.<Integer>isNull())
                .retryIfResult(Predicates.equalTo(2))
                .retryIfExceptionOfType(Exception.class)
                .withStopStrategy(StopStrategies.stopAfterAttempt(5))
                .withWaitStrategy(WaitStrategies.fixedWait(300, TimeUnit.MILLISECONDS))
                .build();
        try {
            retryer.call(task);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (RetryException e) {
            e.printStackTrace();
        }

        con.commit();
        con.close();
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值