在群集中共享一个Java同步块,或者使用全局锁? [英] Sharing a Java synchronized block across a cluster, or using a global lock?

查看:135
本文介绍了在群集中共享一个Java同步块,或者使用全局锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



目标环境是WebSphere 6.0,集群中有2个节点。



我有一种感觉,即$ code> synchronized won' t工作,因为每个节点上的应用程序的每个实例都有自己的JVM,对吗?



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



数据库是DB2v9,我直接通过JNDI(无ORM层)访问它。 p>

有人建议,全球锁可能是去这里的方式,但我不知道该怎么做。



有没有人在这个领域有任何指针?



谢谢!

解决方案

正确的是,使用Java同步结构,不同进程之间的同步将无法正常工作。幸运的是,您的问题真的不是代码同步,而是与数据库的同步交互。



处理此问题的正确方法是使用数据库级锁。大概你有一些表包含一个数据库模式版本,所以你应该确保在启动/升级过程中锁定该表。



精确的sql /如果您指定了数据库类型(DB2?)和访问方法(raw sql,jpa等),则涉及的db调用可能会更加清晰。



更新8/4/2009 2:39 PM):我建议 LOCK TABLE 语句在一些持有模式版本号的表上。这将序列化对该表的访问,以防止两个实例一次运行升级代码。


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?

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

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.

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?

Thanks!

解决方案

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.

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.

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).

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同步块,或者使用全局锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆