[jboss-svn-commits] JBL Code SVN: r28575 - in labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943: entitymanager and 6 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jul 29 12:53:43 EDT 2009
Author: whitingjr
Date: 2009-07-29 12:53:43 -0400 (Wed, 29 Jul 2009)
New Revision: 28575
Added:
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/IsolationLevel.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/transaction/
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/transaction/ServiceLocator.java
Modified:
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerFactoryImpl.java
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/isolation/Isolation.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/factory/IsolationFactory.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/persistence/STMPersistenceProviderImpl.java
Log:
Added implementation for registering the synchronization to close Connection object.
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerFactoryImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerFactoryImpl.java 2009-07-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerFactoryImpl.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -14,13 +14,16 @@
import javax.persistence.EntityManager;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
-import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.resource.adapter.jdbc.WrappedConnection;
import uk.ac.ncl.sdia.a8905943.ejb3.persistence.STMPersistenceContext;
+import uk.ac.ncl.sdia.a8905943.isolation.IsolationLevel;
import uk.ac.ncl.sdia.a8905943.persistence.jdbc.STMConnection;
+import uk.ac.ncl.sdia.a8905943.persistence.xa.STMXAConnectionImpl;
import uk.ac.ncl.sdia.a8905943.spec.entitymanager.STMEntityManagerFactory;
import uk.ac.ncl.sdia.a8905943.stm.persistence.STMPersistenceProviderImpl;
@@ -36,24 +39,32 @@
{
/* the properties object should be considered immutable after the first
* EntityManager is created. Use it and treat it as read only.*/
- private volatile Map properties ;
+ private volatile Map properties;
+
private static final Logger logger = Logger.getLogger(STMEntityManagerFactoryImpl.class);
+
// enum field to indicate the type.
- private volatile PersistenceUnitTransactionType transactionType ;
+ private volatile PersistenceUnitTransactionType transactionType;
+
// immutable field
//TODO this field cannot be static, refactor necessary
- private volatile static IsolationLevel isolationLevel ;
+ private volatile static IsolationLevel isolationLevel;
+
private volatile boolean isOpen = true;
- private String persistenceContextName ;
- private final PersistenceUnitInfo info ;
-
+
+ private String persistenceContextName;
+
+ private final PersistenceUnitInfo info;
+
+ private final DataSource dataSource;
+
@Override
/**
* Perform cleanup of factory.
*/
public void close()
{
-
+
if (!isOpen())
{// already closed, this is not expected to happen
logger.warn("EntityManagerFactory is already closed. Someone is attempting to close again.");
@@ -67,7 +78,9 @@
@Override
public EntityManager createEntityManager()
{
- return createEntityManager(new HashMap<String, String>());
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(STMPersistenceProviderImpl.PROPERTY_ISOLATION_LEVEL, IsolationLevel.REPEATABLE_READ.toString());
+ return createEntityManager(properties);
}
@Override
@@ -123,12 +136,45 @@
STMConnection connection = null;
try
{
- Connection conn = info.getJtaDataSource().getConnection();
- if (null != conn)
+ // Connection conn = info.getJtaDataSource().getConnection(); -- this doesn't work. Doesn't get a wrappped connection
+ Object xaConnObject = getDataSource().getConnection();
+ if (xaConnObject instanceof STMXAConnectionImpl)
{
- connection= (STMConnection)conn;
+ STMXAConnectionImpl xaConnection = (STMXAConnectionImpl) xaConnObject; // this connection should be wrapped by the container
+
+ Object conObject = xaConnection.getConnection();
+ if (conObject instanceof STMConnection)
+ {
+ connection = (STMConnection)conObject;
+ }
+ else
+ {
+ logger.error("Failed to obtain an expected STMConnection when creating an STM EntityManager. Given an ["+conObject.getClass().getName()+"] instead.");
+ }
}
-
+ else
+ {
+ logger.error("Expected XAConnection object should be an instance of STMXAConnection. Given instead ["+xaConnObject.getClass().getName()+"] instance. Will return a plain old Connection.");
+ }
+ if (xaConnObject instanceof WrappedConnection)
+ {
+ WrappedConnection wrappedConnection = (WrappedConnection) xaConnObject;
+ Connection underlyingConnection = (Connection)wrappedConnection.getUnderlyingConnection();
+
+ if (underlyingConnection instanceof STMConnection)
+ {
+ connection = (STMConnection)underlyingConnection;// got there in the end
+ }
+ else
+ {
+ logger.info("underlying connection is not an STMConnection");
+ }
+ }
+ else
+ {
+ logger.info("Underlying conenction is not an instance of WrappedConnection");
+ }
+
}
catch (SQLException sqle)
{
@@ -143,8 +189,7 @@
{
return this.isOpen;
}
-
-
+
/**
* Create a new STMEntityManagerFactoryImpl. The initialization
* of this object relies on serialization by the caller. Use the
@@ -159,8 +204,9 @@
logger.error("Persistence Context object is null and will cause a problem.");
throw new RuntimeException("PersistenceUnitInfo passed to EntityManagerFactory constructor is null.");
}
+ this.dataSource = info.getJtaDataSource();// only support jta data sources
}
-
+
/**
* Parse the string for expected isolation level. no side effects
*
@@ -175,15 +221,15 @@
IsolationLevel returnValue = null;
if (StringUtils.isNotEmpty(value))
{
- if (StringUtils.equalsIgnoreCase( IsolationLevel.REPEATABLE_READ.toString(), value) )
+ if (StringUtils.equalsIgnoreCase(IsolationLevel.REPEATABLE_READ.toString(), value))
{
returnValue = IsolationLevel.REPEATABLE_READ;
}
- else if (StringUtils.equalsIgnoreCase( IsolationLevel.READ_COMMITTED.toString(), value))
+ else if (StringUtils.equalsIgnoreCase(IsolationLevel.READ_COMMITTED.toString(), value))
{
returnValue = IsolationLevel.READ_COMMITTED;
}
- else if (StringUtils.equalsIgnoreCase( IsolationLevel.SERIALIZABLE.toString(), value))
+ else if (StringUtils.equalsIgnoreCase(IsolationLevel.SERIALIZABLE.toString(), value))
{
returnValue = IsolationLevel.SERIALIZABLE;
}
@@ -196,28 +242,34 @@
{
throw new RuntimeException("Empty isolation value.");
}
-
+
return returnValue;
}
-
+
private boolean notEmpty(Map map)
{
return !map.isEmpty();
}
+
private boolean containsRequiredConfiguration(Map map)
{
return map.containsKey(STMPersistenceProviderImpl.PROPERTY_ISOLATION_LEVEL);
}
-
+
@Override
protected Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException("Cloning of this object is not supported.");
}
-
+
@Override
public STMPersistenceContext getPersistenceContext()
{
return null;
}
+
+ public DataSource getDataSource()
+ {
+ return dataSource;
+ }
}
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-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -9,27 +9,36 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
-import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
import org.apache.log4j.Logger;
-import uk.ac.ncl.sdia.a8905943.ejb3.persistence.STMPersistenceContext;
+import uk.ac.ncl.sdia.a8905943.locator.transaction.ServiceLocator;
import uk.ac.ncl.sdia.a8905943.persistence.jdbc.STMConnection;
import uk.ac.ncl.sdia.a8905943.stm.annotation.STMEntity;
final class STMEntityManagerImpl implements EntityManager
{
private PersistenceUnitTransactionType type;
- private STMConnection connection ;
+
+ // this reference is a wrapped object
+ protected Connection connection;
+
private static final Logger logger = Logger.getLogger(STMEntityManagerImpl.class);
-
+
+ //private Transaction
+
@Override
public void clear()
{
@@ -39,7 +48,43 @@
@Override
public void close()
{
- // FIXME close
+ /* get the currently active transaction on this thread, register a synchronization to close the Connection object.
+ *
+ */
+ TransactionManager transactionManager = ServiceLocator.findLocalPersonManager();
+ try
+ {
+ 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();
+ }
+ }
+ }
+
+ );
+ }
+ }
+ catch (SystemException se)
+ {
+ logger.error("Problem occured when registering a synchronization to close the Connection after the Transaction has completed.", se);
+ }
}
@@ -67,7 +112,7 @@
{
throw new UnsupportedOperationException("Unsupported operation createNamedQuery(String)");
}
-
+
return null;
}
@@ -106,13 +151,13 @@
* 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}
- */
+ */
public <T> T find(Class<T> entityClass, Object primaryKey)
{
T returnValue = null;
// check if the class has been annotated using the STM annotations.
boolean isstmEntity = false;
- for ( Annotation annotation : entityClass.getAnnotations())
+ for (Annotation annotation : entityClass.getAnnotations())
{
if (annotation instanceof STMEntity)
{
@@ -138,11 +183,14 @@
}
catch (NoSuchMethodException nsme)
{
- logger.error("Expected constructor does not exist, please create a public constructor taking the primary key field only.", nsme);
+ logger
+ .error(
+ "Expected constructor does not exist, please create a public constructor taking the primary key field only.",
+ nsme);
}
catch (IllegalArgumentException illae)
{
- logger.error("Expected primary key type is expected to be Long." , illae);
+ logger.error("Expected primary key type is expected to be Long.", illae);
}
catch (InvocationTargetException e)
{
@@ -187,7 +235,8 @@
{
if (PersistenceUnitTransactionType.JTA == this.type)
{
- throw new IllegalStateException("An EntityTransaction used for bye EXTENDED entity managers is not allowed for the JTA transaction types.");
+ throw new IllegalStateException(
+ "An EntityTransaction used bye EXTENDED entity managers is not allowed for the JTA transaction types.");
}
return null;
}
@@ -251,9 +300,20 @@
private STMEntityManagerImpl()
{
}
- public STMEntityManagerImpl( STMConnection connection)
+
+ public STMEntityManagerImpl(Connection connection)
{
this();
this.connection = connection;
}
+
+ public STMConnection getSTMConnection()
+ {
+ return (STMConnection) connection;
+ }
+
+ public void setConnection(Connection connection)
+ {
+ this.connection = connection;
+ }
}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java 2009-07-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -1,6 +1,5 @@
package uk.ac.ncl.sdia.a8905943.isolation;
-import uk.ac.ncl.sdia.a8905943.spec.History;
public interface Isolation {
Added: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/IsolationLevel.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/IsolationLevel.java (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/IsolationLevel.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -0,0 +1,12 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package uk.ac.ncl.sdia.a8905943.isolation;
+
+public enum IsolationLevel {
+
+ READ_COMMITTED , REPEATABLE_READ, SERIALIZABLE
+}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/factory/IsolationFactory.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/factory/IsolationFactory.java 2009-07-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/factory/IsolationFactory.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -1,8 +1,9 @@
package uk.ac.ncl.sdia.a8905943.isolation.factory;
-import org.jboss.cache.lock.IsolationLevel;
+
import uk.ac.ncl.sdia.a8905943.isolation.Isolation;
+import uk.ac.ncl.sdia.a8905943.isolation.IsolationLevel;
import uk.ac.ncl.sdia.a8905943.isolation.ReadCommittedIsolationImpl;
import uk.ac.ncl.sdia.a8905943.isolation.RepeatableReadIsolationImpl;
import uk.ac.ncl.sdia.a8905943.isolation.SerializableIsolationImpl;
Added: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/transaction/ServiceLocator.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/transaction/ServiceLocator.java (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/locator/transaction/ServiceLocator.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package uk.ac.ncl.sdia.a8905943.locator.transaction;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.transaction.TransactionManager;
+
+import org.apache.log4j.Logger;
+
+public class ServiceLocator
+{
+ private static final String LOCAL_TRANSACTION_MANAGER = "java:/TransactionManager";
+ private static final String LOCAL_PROVIDER = "localhost:1099";
+ private static Logger logger = Logger.getLogger(ServiceLocator.class);
+
+ public static TransactionManager findLocalPersonManager()
+ {
+ TransactionManager l_return = null;
+ Object object = findObject(LOCAL_TRANSACTION_MANAGER, LOCAL_PROVIDER);
+ if (null != object)
+ {
+ l_return = (TransactionManager)object;
+ }
+ else
+ {
+ logger.error("Service Locator could not find a local PersonManager using ["+LOCAL_TRANSACTION_MANAGER+"]");
+ }
+
+ return l_return;
+ }
+
+ private static Object findObject(String _jndiName, String _provider)
+ {
+ Object l_return = null;
+ try {
+ InitialContext l_context = new InitialContext();
+ l_context.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
+ l_context.addToEnvironment(Context.PROVIDER_URL, _provider);
+ l_return = l_context.lookup(_jndiName);
+ } catch (Exception e) {
+ e.printStackTrace ();
+ }
+ return l_return;
+ }
+}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java 2009-07-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -16,7 +16,6 @@
{
private final String databaseName;
- private Map<String, Map<Long, Object>> store = new HashMap<String, Map<Long, Object>>();
public Object find(Class type, Object identity)
{
@@ -55,21 +54,45 @@
return returnValue;
}
+ /**
+ * Commit the current transaction
+ */
public void commit()
{
}
-
+ /**
+ * The changes stored in this transaction are not going to persistent store
+ *
+ */
public void abort()
{
}
-
+ /**
+ * Notify the transactional system the changes should be locked for future
+ * storing in share transactional memory.
+ */
public void prepare()
{
}
-
+ /**
+ * Inform the transactional system that transactional activity will start
+ * to happen for this thread. Make changes in transactional memory.
+ */
+ public void begin()
+ {
+
+ }
+ /**
+ * A problem occured in the system and transactional changes should be discarded.
+ */
+ public void discard()
+ {
+
+ }
+
public String getDatabaseName()
{
return databaseName;
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/persistence/STMPersistenceProviderImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/persistence/STMPersistenceProviderImpl.java 2009-07-29 16:09:27 UTC (rev 28574)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/persistence/STMPersistenceProviderImpl.java 2009-07-29 16:53:43 UTC (rev 28575)
@@ -17,6 +17,7 @@
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import uk.ac.ncl.sdia.a8905943.ejb3.persistence.STMPersistenceContext;
@@ -63,7 +64,7 @@
}
if (logger.isDebugEnabled())
{
- logger.debug("Dump of PersisteneceUnitInfo["+info.toString()+"] and properties Map ["+map.toString()+"]");
+ logger.debug("Dump of PersisteneceUnitInfo["+info.toString()+"] and properties Map ["+(map != null ? map.toString() : StringUtils.EMPTY)+"]");
}
if (logger.isTraceEnabled())
{
More information about the jboss-svn-commits
mailing list