[hibernate-commits] Hibernate SVN: r11475 - in trunk/HibernateExt/shards/src: test/org/hibernate/shards/integration/model and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon May 7 01:53:05 EDT 2007


Author: max.ross
Date: 2007-05-07 01:53:05 -0400 (Mon, 07 May 2007)
New Revision: 11475

Modified:
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
   trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java
Log:
http://opensource.atlassian.com/projects/hibernate/browse/HSHARDS-24
http://opensource.atlassian.com/projects/hibernate/browse/HSHARDS-25

Implemented ShardedSessionImpl.lock (both overloads)

A little bit of refactoring too.

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-05-07 05:03:55 UTC (rev 11474)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-05-07 05:53:05 UTC (rev 11475)
@@ -937,19 +937,25 @@
     applyDeleteOperation(op, object);
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
-  public void lock(Object object, LockMode lockMode) throws HibernateException {
-    throw new UnsupportedOperationException();
+  public void lock(final Object object, final LockMode lockMode) throws HibernateException {
+    SessionOperation<Void> op = new SessionOperation<Void>() {
+      public Void apply(Session session) {
+        session.lock(object, lockMode);
+        return null;
+      }
+    };
+    invokeSessionOperationOnSessionWithObject(op, object);
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
-  public void lock(String entityName, Object object, LockMode lockMode)
+  public void lock(final String entityName, final Object object, final LockMode lockMode)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    SessionOperation<Void> op = new SessionOperation<Void>() {
+      public Void apply(Session session) {
+        session.lock(entityName, object, lockMode);
+        return null;
+      }
+    };
+    invokeSessionOperationOnSessionWithObject(op, object);
   }
 
   /**
@@ -968,12 +974,12 @@
   }
 
   public LockMode getCurrentLockMode(final Object object) throws HibernateException {
-    SessionMethodInvoker<LockMode> invoker = new SessionMethodInvoker<LockMode>() {
-      public LockMode invoke(Session s, Object obj) {
+    SessionOperation<LockMode> invoker = new SessionOperation<LockMode>() {
+      public LockMode apply(Session s) {
         return s.getCurrentLockMode(object);
       }
     };
-    return invokeOnSessionWithObject(invoker, object);
+    return invokeSessionOperationOnSessionWithObject(invoker, object);
   }
 
   public Transaction beginTransaction() throws HibernateException {
@@ -1061,38 +1067,13 @@
     }
   }
 
-  private interface SessionMethodInvoker<T> {
-    T invoke(Session s, Object obj);
-  }
-
-  /**
-   * Helper method we can use when we need to find the Session with which a
-   * specified object is associated and invoke the method on that Session.
-   * If the object isn't associated with a Session we just invoke it on a
-   * random Session with the expectation that this will cause an error.
-   */
-  <T> T invokeOnSessionWithObject(SessionMethodInvoker<T> smi, Object object) throws HibernateException {
-    ShardId shardId = getShardIdForObject(object);
-    Session sessionToUse;
-    if (shardId == null) {
-      // just ask this question of a random shard so we get the proper error
-      sessionToUse = getSomeSession();
-      if (sessionToUse == null) {
-        sessionToUse = shards.get(0).establishSession();
-      }
-    } else {
-      sessionToUse = shardIdsToShards.get(shardId).establishSession();
-    }
-    return smi.invoke(sessionToUse, object);
-  }
-
   public String getEntityName(final Object object) throws HibernateException {
-    SessionMethodInvoker<String> invoker = new SessionMethodInvoker<String>() {
-      public String invoke(Session s, Object obj) {
+    SessionOperation<String> invoker = new SessionOperation<String>() {
+      public String apply(Session s) {
         return s.getEntityName(object);
       }
     };
-    return invokeOnSessionWithObject(invoker, object);
+    return invokeSessionOperationOnSessionWithObject(invoker, object);
   }
 
   public Filter enableFilter(String filterName) {
@@ -1461,4 +1442,29 @@
     currentSubgraphShardId.set(shardId);
   }
 
+  private interface SessionOperation<T> {
+    T apply(Session session);
+  }
+
+  /**
+   * Helper method we can use when we need to find the Session with which a
+   * specified object is associated and invoke the method on that Session.
+   * If the object isn't associated with a Session we just invoke it on a
+   * random Session with the expectation that this will cause an error.
+   */
+  <T> T invokeSessionOperationOnSessionWithObject(SessionOperation<T> sessionOperation, Object object) throws HibernateException {
+    ShardId shardId = getShardIdForObject(object);
+    Session sessionToUse;
+    if (shardId == null) {
+      // just ask this question of a random shard so we get the proper error
+      sessionToUse = getSomeSession();
+      if (sessionToUse == null) {
+        sessionToUse = shards.get(0).establishSession();
+      }
+    } else {
+      sessionToUse = shardIdsToShards.get(shardId).establishSession();
+    }
+    return sessionOperation.apply(sessionToUse);
+  }
+
 }

Modified: trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java
===================================================================
--- trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java	2007-05-07 05:03:55 UTC (rev 11474)
+++ trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java	2007-05-07 05:53:05 UTC (rev 11475)
@@ -955,6 +955,22 @@
     assertEquals(LockMode.WRITE, session.getCurrentLockMode(b));
   }
 
+  public void testLock() {
+    session.beginTransaction();
+    Building b = building("b1");
+    try {
+      session.lock(b, LockMode.READ);
+      fail("expected he");
+    } catch (HibernateException he) {
+      // good
+    }
+    session.save(b);
+    commitAndResetSession();
+    b = reload(b);
+    session.lock(b, LockMode.UPGRADE);
+    assertEquals(LockMode.UPGRADE, session.getCurrentLockMode(b));
+  }
+
   // this is a really good way to shake out synchronization bugs
   public void xtestOverAndOver() throws Exception {
     final boolean[] go = {true};




More information about the hibernate-commits mailing list