Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / store / raw / xact / RowLocking3Escalate.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/store/raw/xact/RowLocking3Escalate.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/store/raw/xact/RowLocking3Escalate.java
new file mode 100644 (file)
index 0000000..f94841c
--- /dev/null
@@ -0,0 +1,118 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.store.raw.xact.RowLocking3Escalate\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.store.raw.xact;\r
+\r
+import org.apache.derby.iapi.services.locks.LockFactory;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.store.raw.ContainerHandle;\r
+import org.apache.derby.iapi.store.raw.ContainerLock;\r
+import org.apache.derby.iapi.store.raw.RecordHandle;\r
+import org.apache.derby.iapi.store.raw.Transaction;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+\r
+/**\r
+       A locking policy that implements row level locking with isolation degree 3.\r
+\r
+       @see org.apache.derby.iapi.store.raw.LockingPolicy\r
+*/\r
+public class RowLocking3Escalate extends ContainerLocking3 \r
+{\r
+       protected RowLocking3Escalate(LockFactory lf) \r
+    {\r
+               super(lf);\r
+       }\r
+\r
+    /**\r
+     * Escalates Row Locking 3 to Container Locking 3.\r
+     * <p>\r
+     * This call is made by code which tracks the number of locks on a \r
+     * container. When the number of locks exceeds the escalate threshold\r
+     * the caller creates this new locking policy, calls lockContainer(), \r
+     * and substitues it for the old locking policy.  The lockContainer call\r
+     * determines which table lock to get (S or X), gets that table lock, and\r
+     * then releases the row locks on the table.\r
+     *\r
+     * It is assumed that this is called on a open container for lock only.\r
+     * <p>\r
+     *\r
+     * @param t            Transaction to associate lock with.\r
+     * @param container    Container to lock.\r
+     * @param waitForLock  Ignored - will never wait for a lock.\r
+     * @param forUpdate    Ignored, mode determined from current lock state.\r
+     *\r
+     * @return true if the lock was obtained, false if it wasn't. \r
+     *   False should only be returned if the waitForLock policy was set to\r
+     *  "false," and the lock was unavailable.\r
+     *\r
+        * @exception  StandardException  Standard exception policy.\r
+     **/\r
+       public boolean lockContainer(\r
+    Transaction         t, \r
+    ContainerHandle     container, \r
+    boolean             waitForLock,\r
+    boolean             forUpdate)\r
+               throws StandardException \r
+    {\r
+               forUpdate = false;\r
+\r
+        // If an IX lock exists then escalate to X table lock, else escalate\r
+        // to S table lock.\r
+               if (lf.isLockHeld(\r
+                t.getCompatibilitySpace(), t, \r
+                container.getId(), ContainerLock.CIX))\r
+        {\r
+                       forUpdate = true;\r
+        }\r
+\r
+        // Get the new X or S table lock.\r
+               boolean gotLock = \r
+            super.lockContainer(t, container, waitForLock, forUpdate);\r
+\r
+               if (!gotLock)\r
+                       return false;\r
+\r
+        // now remove all matching ROW locks, this is done using the special\r
+        // EscalateContainerKey() class which through the Matchable interface\r
+        // only matches row locks of this container.\r
+               lf.unlockGroup(\r
+            t.getCompatibilitySpace(), t, \r
+            new EscalateContainerKey(container.getId()));\r
+\r
+        if (SanityManager.DEBUG)\r
+        {\r
+            SanityManager.ASSERT(\r
+                lf.isLockHeld(\r
+                    t.getCompatibilitySpace(), t, \r
+                    container.getId(), \r
+                    (forUpdate ? ContainerLock.CX : ContainerLock.CS)),\r
+                "Covering table lock (" +\r
+                (forUpdate ? ContainerLock.CX : ContainerLock.CS) +\r
+                " is not held after lock escalation.");\r
+        }\r
+\r
+               return true;\r
+       }\r
+}\r