如何使用 Glassfish 2.1 在 JPA 中实现悲观锁定?

发布于 2024-08-04 14:26:32 字数 1260 浏览 3 评论 0原文

当尝试使用以下代码设置锁定(悲观)时:

em.lock(controlnumbers, LockModeType.WRITE);
em.refresh(controlnumbers);

我收到以下异常:

[#|2009-09-10T15:42:48.324-0400|INFO|sun-appserver2.1|javax.enterprise.system.container.ejb|_ThreadID=31;_ThreadName=httpSSLWorkerThread-8080-19;|
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.lock(EntityManagerImpl.java:619)
        at com.sun.enterprise.util.EntityManagerWrapper.lock(EntityManagerWrapper.java:582)
        at com.eximtechnologies.transactionserver.persistence.session.ControlNumbersFacade.lock(ControlNumbersFacade.java:43)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

How can I Implement pessimisticlocking with Glassfish 2.1?

When trying to setup a lock (pessimistic), with the following code:

em.lock(controlnumbers, LockModeType.WRITE);
em.refresh(controlnumbers);

I am getting the following exception:

[#|2009-09-10T15:42:48.324-0400|INFO|sun-appserver2.1|javax.enterprise.system.container.ejb|_ThreadID=31;_ThreadName=httpSSLWorkerThread-8080-19;|
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.lock(EntityManagerImpl.java:619)
        at com.sun.enterprise.util.EntityManagerWrapper.lock(EntityManagerWrapper.java:582)
        at com.eximtechnologies.transactionserver.persistence.session.ControlNumbersFacade.lock(ControlNumbersFacade.java:43)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

How can I implement pessimistic locking with Glassfish 2.1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-08-11 14:26:32

有一个 Toplink Essentials(GF 2.1 默认)特定的方法可以做到这一点:

public MyObject lock (MyObject controlnumbers) {
    String qStr = "select object(o) from MyObject as o where o.pk = :pk";
    Query q = em.createQuery(qStr);
    q.setParameter("pk", "a");
    q.setHint("toplink.pessimistic-lock", "Lock");
    controlnumbers = (MyObject)q.getSingleResult();
    return controlnumbers;
}

我相信 Hibernate 调用 em.lock 实际上会起作用。

There is a Toplink Essentials (GF 2.1 default) specific way to do this:

public MyObject lock (MyObject controlnumbers) {
    String qStr = "select object(o) from MyObject as o where o.pk = :pk";
    Query q = em.createQuery(qStr);
    q.setParameter("pk", "a");
    q.setHint("toplink.pessimistic-lock", "Lock");
    controlnumbers = (MyObject)q.getSingleResult();
    return controlnumbers;
}

I believe with Hibernate calling em.lock will actually work.

宛菡 2024-08-11 14:26:32

你能展示一下你的班级的映射吗?看起来您缺少版本属性...看看

http:// en.wikibooks.org/wiki/Java_Persistence/Locking#Timestamp_Locking

问候,Jan

Can you show the mapping for your class? It looks like you are missing a version property... Take a look at

http://en.wikibooks.org/wiki/Java_Persistence/Locking#Timestamp_Locking

Regards, Jan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文