论坛首页 Java企业应用论坛

Summary of Terracotta Locks

浏览 1561 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-15   最后修改:2009-09-27
Summary of Terracotta Locks
cluster lock失效的情况:
1.if the object is shared after it is locked. Instead, an error occurs. See an example in the following gotcha.

share object应该在锁定(不管是local lock还是cluster lock)它之前操作。clsuter lock块中只操作cluster object.


    private static void lockBeforeShared() {
        Map<String, Object> map = new HashMap<String, Object>();
        Object obj = new Object();
        synchronized (obj) {
            synchronized (map) {
                map.put("obj", obj);

            }
            //tried to unlock the foo object which is now shared, but a cluster lock was never acquired
        }
    }


    private static void shareBeforeLock() {
        Map<String, Object> map = new HashMap<String, Object>();
        Object obj = new Object();
        synchronized (map) {
            map.put("obj", obj);
        }
        synchronized (obj) {// obtain cluster lock
            //other obj operation
        }
        //release cluster lock
    }


2.in methods that have no synchronization, unless you configure auto-synchronize to be true.

如果方法没有sync,那么即使在tc-config.xml中配置了auto-lock。这个cluster lock也不会起作用。auto-lock针对没有sync的方法,必须使用auto-synchronize= true.
另外注意,如果方法有sync,但是没有配置tc-config.xml的auto-lock部分,肯定连cluster-lock都不会生成。

3.if you aren't synchronizing on a clustered object.
如果sync不是针对cluster object,那么即使配置了也不会起作用。

4.if you synchronize on an object that isn't clustered yet, even if it becomes clustered within the scope of your synchronization. The object must be clustered before you synchronize on it or no cluster behavior will occur.
如果sync块内才将object变成cluster object,那么已经晚了。所以sync只能针对sync块之前就已经是cluster object的对象,才起作用。

5.in methods that do not match a lock configuration stanza. Of course, you do not need to configure locking for code that is already addressed in the configuration of a Terracotta Integration Module that you are using.
方法不满足通配符,肯定不会起作用。



操作一个cluster object时出现的情况:

1.outside the scope of a clustered lock, you will get an error.
如果在一个cluster lock范围之外操作,肯定会报错。所以结合上面的3,4可以确认,cluster object的操作与cluster lock肯定是结伴同行的。

2.in code that is not instrumented by Terracotta, the changes to that object will be invisible to Terracotta. As a result, the clustered object may be in an inconsistent state. Therefore, you MUST ensure that you instrument all classes that manipulate clustered objects. See Gotchas: Uninstrumented Access for more details.

类没有声明为instrument-class,那么该类针对clustered object的修改比如重新赋值这样的操作不会提交到TC,进而导致clustered object的状态不连续。
解决的办法有二种,一种是通过方法去访问cluster object而不是直接访问;另一种方法就是将直接访问clustered object的类定义为instrumented class
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics