[jboss-svn-commits] JBL Code SVN: r28621 - labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 30 17:52:13 EDT 2009


Author: whitingjr
Date: 2009-07-30 17:52:13 -0400 (Thu, 30 Jul 2009)
New Revision: 28621

Modified:
   labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java
Log:
Changed to hold the wrapped connection object. Updated getter to delve into the internals.

Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java	2009-07-30 21:51:30 UTC (rev 28620)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java	2009-07-30 21:52:13 UTC (rev 28621)
@@ -10,6 +10,7 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.Connection;
+import java.sql.SQLException;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
@@ -17,12 +18,14 @@
 import javax.persistence.LockModeType;
 import javax.persistence.Query;
 import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.transaction.RollbackException;
 import javax.transaction.Synchronization;
 import javax.transaction.SystemException;
 import javax.transaction.Transaction;
 import javax.transaction.TransactionManager;
 
 import org.apache.log4j.Logger;
+import org.jboss.resource.adapter.jdbc.WrappedConnection;
 
 import uk.ac.ncl.sdia.a8905943.locator.transaction.ServiceLocator;
 import uk.ac.ncl.sdia.a8905943.persistence.jdbc.STMConnection;
@@ -36,62 +39,57 @@
    protected Connection connection;
 
    private static final Logger logger = Logger.getLogger(STMEntityManagerImpl.class);
+   
+   private boolean isOpen = false;
 
-   //private Transaction
-
    @Override
    public void clear()
    {
-      // FIXME clear
+      //TODO: detatch the entities managed by this connection.
    }
 
    @Override
    public void close()
    {
-      /* get the currently active transaction on this thread, register a synchronization to close the Connection object.
-       * 
-       */
+      /* get the currently active transaction on this thread, register a synchronization 
+       * to close the Connection object. */
+      
+      
       TransactionManager transactionManager = ServiceLocator.findLocalPersonManager();
       try
       {
+         this.connection.close();
          Transaction transaction = transactionManager.getTransaction();
          if (null != transaction)
          {
-            transaction.registerSynchronization(
-                  new Synchronization()
-            {
-               @Override
-               public void beforeCompletion()
-               {
-                  // do nothing                  
-               }
-               @Override
-               public void afterCompletion(int status)
-               {
-                  /* Check to see if the Connection has been release back to the 
-                   * pool.*/
-                  
-                  if (!connection.isClosed())
-                  {
-                     connection.close();
-                  }
-               }
-            }
-                                                
-            );
+            transaction.registerSynchronization(new CloseSTMEM());
          }
       }
       catch (SystemException se)
       {
-         logger.error("Problem occured when registering a synchronization to close the Connection after the Transaction has completed.", se);
+         logger
+               .error(
+                     "Problem occured when registering a synchronization to close the Connection after the Transaction has completed.",
+                     se);
+         throw new RuntimeException(se);
       }
-
+      catch (RollbackException re)
+      {
+         logger.error("Could not register a Synchronization for STMEntityManager");
+         throw new RuntimeException(re);
+      }
+      catch (SQLException sqle)
+      {
+         logger.error("SQLException occured when closing the connection object when, the EM is closed.");
+         throw new RuntimeException(sqle);
+      }
+      this.isOpen = false;
    }
 
    @Override
    public boolean contains(Object entity)
    {
-      // FIXME contains
+      // TODO: jrw check the STM using the open connection to check if this entity exists, use primary key
       return false;
    }
 
@@ -150,7 +148,8 @@
    /**
     * To call this method the entity object is expected to follow this contract.
     * Entity has one constructor that is public, contains only one field and is of
-    * type {@link java.lang.Long}
+    * type {@link java.lang.Long}.
+    * The entity class is annotated with the STMEntity annotation.
     */
    public <T> T find(Class<T> entityClass, Object primaryKey)
    {
@@ -244,8 +243,7 @@
    @Override
    public boolean isOpen()
    {
-      // FIXME isOpen
-      return false;
+      return this.isOpen;
    }
 
    @Override
@@ -309,11 +307,53 @@
 
    public STMConnection getSTMConnection()
    {
-      return (STMConnection) connection;
+      STMConnection returnValue = null;
+      if (null != this.connection )
+      {
+         try
+         {
+            returnValue = (STMConnection)((WrappedConnection)this.connection).getUnderlyingConnection();
+         } 
+         catch (SQLException sqle)
+         {
+            logger.error(sqle);
+            throw new RuntimeException(sqle);
+         }
+      }
+      return returnValue;
    }
 
    public void setConnection(Connection connection)
    {
       this.connection = connection;
    }
+
+   private class CloseSTMEM implements Synchronization
+   {
+      private final Logger logger = Logger.getLogger(CloseSTMEM.class);
+
+      @Override
+      public void beforeCompletion()
+      {
+         // do nothing                  
+      }
+
+      @Override
+      public void afterCompletion(int status)
+      {
+         /* Check to see if the Connection has been release back to the 
+          * pool.*/
+         try
+         {
+            if (!connection.isClosed())
+            {
+               connection.close();// release connection back into container pool
+            }
+         }
+         catch (SQLException e)
+         {
+            logger.error("Error", e);
+         }
+      }
+   }
 }



More information about the jboss-svn-commits mailing list