【问题标题】:Handling race condition in Hibernate在 Hibernate 中处理竞争条件
【发布时间】:2017-06-29 17:12:25
【问题描述】:

希望有人能指出正确的方向,为什么这不起作用:

    Session session = factory.openSession();
    session.refresh(customer);
    session.close();
    Boolean paid = customer.hasPaid();
    if (paid) {
        System.out.println("test1");
    } else {
        System.out.println("test2");
    }

    if (!paid && paymentInput.getRequestType() == PaymentInput.RequestType.NEW) {
        session = factory.openSession();
        Transaction tx = session.beginTransaction();
        customer.setPaid(true);
        customer.update(customer, customer.getId());
        tx.commit();
        session.close();
        System.out.println("updated");

        PaymentProcessor.pay(customer);
    }

我想要的是第一个请求点击“test2”,然后是“更新”,以及所有其他请求点击“test1”并错过第二个条件。如果我当前彼此快速触发多个请求,它会点击“test1”并多次输入第二个条件。我在这里做错了什么?

【问题讨论】:

    标签: java hibernate tomcat servlets


    【解决方案1】:

    其他测试不点击“test1”的唯一方法是让它们第 16 行 - tx.commit(); - 完成其工作后开始他们的事务。你在这里的目标是什么?如果您的请求必须假定事务已提交,则必须强制它们等待,直到该假设成立。这通常通过某种锁来完成。

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2012-01-26
      相关资源
      最近更新 更多