在weblogic集群环境(集群内多个节点)上工作时行锁定失败

发布于 2024-12-26 11:31:10 字数 1875 浏览 3 评论 0原文

我的应用程序部署在具有 2 个节点的 weblogic 9 集群环境上,并使用持久性提供程序 toplink 连接到 MS SQL Server 2005。部署的应用程序是用 Java 编写的。

我的应用程序在服务请求 A 时需要执行一个简单的操作:

  1. 开始事务
  2. 从表 A 中选择条目并对其进行行锁定
  3. 处理它们
  4. 用结果更新表 A。
  5. 似乎

java代码如下:

EntityManager em =Persistence.createEntityManagerFactory("NewPersistenceLevelPU").createEntityManager;
EntityTransaction transaction = em.getTransaction();
transaction.begin();

// step 2
em.createNativeQuery("select * from Table_A with(updlock, rowlock) where id = 123");
List<List<Object>> results = (List<List<Object>>) query.getResultList();

//step 3
SomeOperation(results);

// step 4, using the results from step 3
em.createNativeQuery("update Table_A set Column A = 'something' where id = 123");

// step 4
em.flush();
transaction.commit();

如果我同时在节点1和节点2上启动请求A,两个节点都将能够继续执行步骤3,这超出了我的预期。正如我预期的那样,数据库应该已经被节点锁定(例如节点 1),首先执行步骤 2(因为 with(updlock, rowlock)),而后面的一个应该被阻止,直到第一个事务已提交。

我可以知道我是否做错了什么吗?非常感谢您的帮助


感谢您的回复。经过更多调查,我发现以下内容:

  1. 我们的方法实际上是由 MDB 调用的
  2. persistence.xml 如下

    
    <持久化版本=“1.0”xmlns=“http://java.sun.com/xml/ns/persistence”xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi: schemaLocation =“http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd”>
    <持久性单元名称=“NewPersistenceLevelPU”事务类型=“JTA”>
    oracle.toplink.essentials.PersistenceProvider
        TestDB
            <属性>
                <属性名称=“toplink.cache.type.default”值=“”/>
            
    
    
    

所以我现在想知道: 1. 它是应用程序管理的事务管理器还是容器管理的事务管理器? 2. 在这两种情况下,我如何处理应用程序内的交易?使用 UserTransaction 还是 EntityTransaction?

非常感谢

My application is deployed on a weblogic 9 cluster environment with 2 nodes and connecting to MS SQL Server 2005 using persistence provider toplink. The application deployed is written in Java.

My application needs to do a simple operation when serving request A:

  1. begin transaction
  2. Select entries from table A and place row locking on them
  3. Process them
  4. Update table A with the result.
  5. end transaction

The java code is like:

EntityManager em =Persistence.createEntityManagerFactory("NewPersistenceLevelPU").createEntityManager;
EntityTransaction transaction = em.getTransaction();
transaction.begin();

// step 2
em.createNativeQuery("select * from Table_A with(updlock, rowlock) where id = 123");
List<List<Object>> results = (List<List<Object>>) query.getResultList();

//step 3
SomeOperation(results);

// step 4, using the results from step 3
em.createNativeQuery("update Table_A set Column A = 'something' where id = 123");

// step 4
em.flush();
transaction.commit();

It seems that if I launch request A on Node 1 and Node 2 at the same time, both Node will be able to proceed to step 3 which is out of my expectation. As I am expecting the database should be already locked by the Node (e.g. Node 1) goes through the step 2 first (because with(updlock, rowlock)), while the later one should be blocked until the first transaction is committed.

May I know if I got something wrong? Many thanks for the help


Thanks for your response. After some more investigation, I found the following:

  1. Our method is actually invoked by the MDB
  2. the persistence.xml is as following

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="NewPersistenceLevelPU" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
        <jta-data-source>TestDB</jta-data-source>
            <properties>
                <property name="toplink.cache.type.default" value="" />
            </properties>
    </persistence-unit>
    </persistence>
    

So I am wondering now:
1. Is it an application-managed or container-managed transaction manager?
2. in both case, how can I handle the transaction within the application? use UserTransaction or EntityTransaction?

Many thanks

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

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

发布评论

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

评论(2

梅窗月明清似水 2025-01-02 11:31:10

两者都将进入步骤 3 并完成,但第二个事务应该等到第一个事务提交后才能完成。

如果你没有看到这一点,那就很奇怪了。您确定第 123 行存在吗?它们是否连接到同一个数据库,您是否正确配置了事务?如果你尝试使用(updlock)它会起作用吗?这可能与数据库的配置方式有关,也许在数据库 shell 中尝试相同的操作。

除了选择之外,您还可以尝试更新行(将某些列设置为其自身),这应该确保获取行锁。

Both will get to step 3 and complete, the second transaction should wait until the first commits before it goes through though.

It is odd if you are not seeing this. Are you sure row 123 exists? Are they connecting to the same database, do you have transactions configured correctly? If you try just with(updlock) does it work? It may be something related to how your database is configured, maybe try the same in a database shell.

Instead of the select, you could also try updating the row (set some column equal to itself), this should ensure a row lock is acquired.

沦落红尘 2025-01-02 11:31:10

在 persistence.xml 中,您使用 JTA 事务,但在代码中,您使用 JPA EntityManager 事务。您不能同时使用两者,只能使用其中之一。

如果您使用 MDB,则应该使用注入的 JTA 管理的 EntityManager。您不应使用 JPA 事务,并且 MDB 应自动启动/结束 JTA 事务。

我的猜测是您尚未设置“toplink.target-server”,而您必须执行此操作才能在 WLS 上启用 JTA 集成。我不确定 TopLink Essentials 是否附带了 WLS 服务器,因此您可能需要升级到 EclipseLink。

如果您想使用 JPA EntityManager 事务,请在 persistence.xml 中设置 transaction-type="RESOURCE_LOCAL"。

From your persistence.xml you are using JTA transactions, but from your code you are using JPA EntityManager transactions. You cannot use both, only one or the other.

If you are using an MDB, you should be using an injected JTA managed EntityManager. You should not use JPA transactions, and the MDB should automatically start/end a JTA transaction.

My guess is you have not set a "toplink.target-server", which you must do, to enable JTA integration on WLS. I'm not sure TopLink Essentials shipped a WLS server, so you might want to upgrade to EclipseLink.

If you want to use JPA EntityManager transactions, then set transaction-type="RESOURCE_LOCAL" in your persistence.xml.

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