<bdo id='BKx3g'></bdo><ul id='BKx3g'></ul>
<i id='BKx3g'><tr id='BKx3g'><dt id='BKx3g'><q id='BKx3g'><span id='BKx3g'><b id='BKx3g'><form id='BKx3g'><ins id='BKx3g'></ins><ul id='BKx3g'></ul><sub id='BKx3g'></sub></form><legend id='BKx3g'></legend><bdo id='BKx3g'><pre id='BKx3g'><center id='BKx3g'></center></pre></bdo></b><th id='BKx3g'></th></span></q></dt></tr></i><div id='BKx3g'><tfoot id='BKx3g'></tfoot><dl id='BKx3g'><fieldset id='BKx3g'></fieldset></dl></div>

  1. <small id='BKx3g'></small><noframes id='BKx3g'>

      <legend id='BKx3g'><style id='BKx3g'><dir id='BKx3g'><q id='BKx3g'></q></dir></style></legend>
    1. <tfoot id='BKx3g'></tfoot>

      跨集群共享 Java 同步块,还是使用全局锁?

      时间:2023-09-30
        <tfoot id='GEMgQ'></tfoot>
            <tbody id='GEMgQ'></tbody>
          <legend id='GEMgQ'><style id='GEMgQ'><dir id='GEMgQ'><q id='GEMgQ'></q></dir></style></legend>
          <i id='GEMgQ'><tr id='GEMgQ'><dt id='GEMgQ'><q id='GEMgQ'><span id='GEMgQ'><b id='GEMgQ'><form id='GEMgQ'><ins id='GEMgQ'></ins><ul id='GEMgQ'></ul><sub id='GEMgQ'></sub></form><legend id='GEMgQ'></legend><bdo id='GEMgQ'><pre id='GEMgQ'><center id='GEMgQ'></center></pre></bdo></b><th id='GEMgQ'></th></span></q></dt></tr></i><div id='GEMgQ'><tfoot id='GEMgQ'></tfoot><dl id='GEMgQ'><fieldset id='GEMgQ'></fieldset></dl></div>

          <small id='GEMgQ'></small><noframes id='GEMgQ'>

                <bdo id='GEMgQ'></bdo><ul id='GEMgQ'></ul>
                本文介绍了跨集群共享 Java 同步块,还是使用全局锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一些代码只允许一个线程访问.我知道如何使用 synchronized 块或方法来完成此操作,但这是否适用于集群环境?

                I have some code that I want to only allow access to by one thread. I know how to accomplish this using either synchronized blocks or methods, but will this work in a clustered environment?

                目标环境为 WebSphere 6.0,集群中有 2 个节点.

                The target environment is WebSphere 6.0, with 2 nodes in the cluster.

                我感觉 synchronized 行不通,因为每个节点上的应用程序的每个实例都会有自己的 JVM,对吧?

                I have a feeling that synchronized won't work, since each instance of the application on each node will have its own JVM, right?

                我在这里尝试做的是在系统启动时对数据库记录进行一些更新.它将查找比代码版本更旧的任何数据库记录,并执行特定任务来更新它们.我只希望一个节点来执行这些升级,因为我想确保每个工作项只升级一次,并且这些升级的性能不是一个大问题,因为它只发生在应用程序启动时,它只是真正做任何事情自上次启动以来代码已更改的时间.

                What I am trying to do here is perform some updates to database records when the system is booted. It will look for any database records that are older that the version of the code, and perform specific tasks to update them. I only want one node to perform these upgrades, since I want to be sure that each work item is only upgraded once, and performance of these upgrades is not a big concern, since it only happens at application startup, and it only really does anything when the code has been changed since the last time it started up.

                数据库是DB2v9,我直接通过JNDI(无ORM层)访问.

                The database is DB2v9, and I am accessing it directly via JNDI (no ORM layer).

                有人建议在这里使用全局锁,但我不知道该怎么做.

                It has been suggested that a global lock might be the way to go here, but I'm not sure how to do that.

                有没有人指点一下这个领域?

                Does anyone have any pointers in this arena?

                谢谢!

                推荐答案

                您是正确的,跨进程同步将无法使用 Java 同步构造.幸运的是,您的问题实际上不是代码同步问题,而是与数据库的交互同步问题.

                You are correct that synchronization across processes will not work using the Java synchronization constructs. Fortunately, your problem really isn't one of code synchronization, but rather of synchronizing interactions with the database.

                处理这个问题的正确方法是使用数据库级锁.假设您有一些包含 db 架构版本的表,因此您应该确保在启动/升级过程中锁定该表.

                The right way to deal with this problem is with database level locks. Presumably you have some table that contains a db schema version, so you should make sure to lock that table for the duration of the startup/upgrade process.

                如果您指定数据库类型(DB2?)和访问方法(原始 sql、jpa 等),所涉及的精确 sql/db 调用可能会更清楚.

                The precise sql/db calls involved would probably be more clear if you specified your database type (DB2?) and access method (raw sql, jpa, etc).

                更新(2009 年 8 月 4 日下午 2:39):我建议使用 LOCK TABLE 对一些持有模式版本号的表的声明.这将序列化对该表的访问,防止两个实例同时运行升级代码.

                Update (8/4/2009 2:39PM): I suggest the LOCK TABLE statement on some table holding the version # of the schema. This will serialize access to that table preventing two instances from running through the upgrade code at once.

                这篇关于跨集群共享 Java 同步块,还是使用全局锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Java 创建多个模式连接? 下一篇:如何连接hibernate和DB2

                相关文章

                最新文章

                <i id='8ke3G'><tr id='8ke3G'><dt id='8ke3G'><q id='8ke3G'><span id='8ke3G'><b id='8ke3G'><form id='8ke3G'><ins id='8ke3G'></ins><ul id='8ke3G'></ul><sub id='8ke3G'></sub></form><legend id='8ke3G'></legend><bdo id='8ke3G'><pre id='8ke3G'><center id='8ke3G'></center></pre></bdo></b><th id='8ke3G'></th></span></q></dt></tr></i><div id='8ke3G'><tfoot id='8ke3G'></tfoot><dl id='8ke3G'><fieldset id='8ke3G'></fieldset></dl></div>
                  • <bdo id='8ke3G'></bdo><ul id='8ke3G'></ul>

                  <small id='8ke3G'></small><noframes id='8ke3G'>

                  <tfoot id='8ke3G'></tfoot>
                  <legend id='8ke3G'><style id='8ke3G'><dir id='8ke3G'><q id='8ke3G'></q></dir></style></legend>