Author: mszefler
Date: Thu Sep 21 10:26:42 2006
New Revision: 448610

URL: http://svn.apache.org/viewvc?view=rev&rev=448610
Log:
Fixed problem with SELECT for UPDATE not working in Derby. Implemented explicit 
update to lock selector rows.

Modified:
    
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/CorrelatorDaoImpl.java
    
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/hobj/HCorrelatorSelector.java

Modified: 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/CorrelatorDaoImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/CorrelatorDaoImpl.java?view=diff&rev=448610&r1=448609&r2=448610
==============================================================================
--- 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/CorrelatorDaoImpl.java
 (original)
+++ 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/CorrelatorDaoImpl.java
 Thu Sep 21 10:26:42 2006
@@ -48,13 +48,16 @@
      * Note: the hk.messageExchange=null is a hack to get around a Hibernate 
bug where the query
      * does not properly discriminate for the proper subclass.
      */
-    private static final String QRY_MESSAGE = "where this.correlationKey = 
?".intern();
+    private static final String QRY_MESSAGE = " where this.correlationKey = 
?".intern();
 
     /** filter for finding a matching selector. */
-    private static final String FLTR_SELECTORS = "where this.correlationKey = 
?" + " and " +
+    private static final String FLTR_SELECTORS = " where this.correlationKey = 
?" + " and " +
             "(this.instance.state = " + ProcessState.STATE_ACTIVE + " or 
this.instance.state = " 
             + ProcessState.STATE_READY + ")".intern();
 
+    private static final String LOCK_SELECTORS = "update " + 
HCorrelatorSelector.class.getName() + 
+        " set lock = lock+1 where correlationKey = :ckey and correlator= 
:corr".intern();
+    
     /** Query for removing routes. */
     private static final String QRY_DELSELECTORS = "delete from " + 
HCorrelatorSelector.class.getName()
             + " where groupId = ? and instance = ?".intern();
@@ -95,14 +98,27 @@
         if (__log.isDebugEnabled())
             __log.debug(hdr);
 
-        Query q = getSession().createFilter(_hobj.getSelectors(), 
FLTR_SELECTORS);
-        q.setString(0, key == null ? null : key.toCanonicalString());
-        q.setLockMode("this", LockMode.UPGRADE);
-
-        HCorrelatorSelector selector = (HCorrelatorSelector) q.uniqueResult();
-
-        __log.debug(hdr + "found " + selector);
-        return selector == null ? null : new MessageRouteDaoImpl(_sm, 
selector);
+        // Make sure we obtain a lock for the selector we want to find. Note 
that a SELECT FOR UPDATE
+        // will not necessarily work, as different DB vendors attach a 
different meaning to this syntax.
+        // In particular it is not clear how long the lock should be held, for 
the lifetime of the 
+        // resulting cursor, or for the lifetime of the transaction. So 
really, an UPDATE of the row
+        // is a much safer alternative. 
+        Query lockQry = getSession().createQuery(LOCK_SELECTORS);
+        lockQry.setString("ckey", key == null ? null : 
key.toCanonicalString());
+        lockQry.setEntity("corr",_hobj);
+        if (lockQry.executeUpdate() > 0) {
+            
+            Query q = getSession().createFilter(_hobj.getSelectors(), 
FLTR_SELECTORS);
+            q.setString(0, key == null ? null : key.toCanonicalString());
+            q.setLockMode("this", LockMode.UPGRADE);
+    
+            HCorrelatorSelector selector = (HCorrelatorSelector) 
q.uniqueResult();
+    
+            __log.debug(hdr + "found " + selector);
+            return selector == null ? null : new MessageRouteDaoImpl(_sm, 
selector);
+        } 
+        
+        return null;
     }
 
     /**
@@ -148,6 +164,7 @@
         HCorrelatorSelector hsel = new HCorrelatorSelector();
         hsel.setGroupId(routeGroupId);
         hsel.setIndex(idx);
+        hsel.setLock(0);
         hsel.setCorrelationKey(correlationKey.toCanonicalString());
         hsel.setInstance((HProcessInstance) ((ProcessInstanceDaoImpl) 
target).getHibernateObj());
         hsel.setCorrelator(_hobj);

Modified: 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/hobj/HCorrelatorSelector.java
URL: 
http://svn.apache.org/viewvc/incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/hobj/HCorrelatorSelector.java?view=diff&rev=448610&r1=448609&r2=448610
==============================================================================
--- 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/hobj/HCorrelatorSelector.java
 (original)
+++ 
incubator/ode/trunk/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/hobj/HCorrelatorSelector.java
 Thu Sep 21 10:26:42 2006
@@ -33,6 +33,8 @@
     private HCorrelator _correlator;
 
     private String _correlationKey;
+    
+    private int _lock;
 
     /**
      * @hibernate.many-to-one column="PIID"
@@ -66,6 +68,17 @@
 
     public void setIndex(int idx) {
         _idx = idx;
+    }
+    
+    /**
+     * @hibernate.property column="LOCK" not-null="true"
+     */
+    public int getLock() {
+        return _lock;
+    }
+    
+    public void setLock(int lock) {
+        _lock = lock;
     }
     
     @Override


Reply via email to