portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwat...@apache.org
Subject svn commit: r775413 - in /portals/jetspeed-2/portal/trunk: components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/ jetspeed-portal-resources/src/main/resou...
Date Sat, 16 May 2009 04:37:43 GMT
Author: rwatler
Date: Sat May 16 04:37:42 2009
New Revision: 775413

URL: http://svn.apache.org/viewvc?rev=775413&view=rev
Log:
JS2-1014 - Unit tests fail in Portals Components using JDK6
---------------------------------------------------------------
This problem turned out to be related to test order and not JDK6 per se. OJB internals are
based on static state and running multiple tests w/o forking lead to connections and their
pools to be reused from test to test. When a test ran first that did not allocate all connections
possible, subsequent tests attempted to get new connections from connection pools that
belonged to prior tests which were by now closed. These DB connection attempts failed.

Fix involves static tracking of OBJ specific connection manager implementation instances and
resetting their internal state on startup. This code does nothing on the initial invocation,
(for the first test and production instances), but serves to clear the statically held OJB
state between test cases. 

- added management class that statically tracks all instances of OJB ConnectionManagerImpl.
- added management instance bean that triggers reset of connections and connection factories
in the
  ConnectionManagerImpl when the Spring context is initialized.


Added:
    portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerManagementBean.java
Modified:
    portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerImpl.java
    portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/datasource.xml
    portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/boot/datasource.xml

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerImpl.java?rev=775413&r1=775412&r2=775413&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerImpl.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerImpl.java
Sat May 16 04:37:42 2009
@@ -81,63 +81,76 @@
     {
         this.broker = broker;
         this.pbKey = broker.getPBKey();
-        this.jcd = MetadataManager.getInstance().connectionRepository().getDescriptor(pbKey);
       
+        this.jcd = MetadataManager.getInstance().connectionRepository().getDescriptor(pbKey);
+        this.connectionFactory = setupConnectionFactory(jcd);
+        this.platform = PlatformFactory.getPlatformFor(jcd);
+        /*
+        by default batch mode is not enabled and after use of a PB
+        instance, before instance was returned to pool, batch mode
+        was set to false again (PB implementation close method)
+        Be carefully in modify this behaviour, changes could cause
+        unexpected behaviour
+        */
+        setBatchMode(false);
+
+        // save connection manager instance
+        ConnectionManagerManagementBean.addConnectionManager(this);
+    }
+    
+    /**
+     * Construct new connection factory.
+     * 
+     * @param jcd JDBC connection descriptor
+     * @return
+     */
+    private static ConnectionFactory setupConnectionFactory(JdbcConnectionDescriptor jcd)
+    {
+        ConnectionFactory cf = null;
         ConnectionPoolDescriptor cpd = jcd.getConnectionPoolDescriptor();        
         if (cpd != null && cpd.getConnectionFactory() != null)
         {
-            connectionFactory = (ConnectionFactory)connectionFactories.get(cpd.getConnectionFactory());
-            if ( connectionFactory == null )
+            cf = (ConnectionFactory)connectionFactories.get(cpd.getConnectionFactory());
+            if ( cf == null )
             {
                 try
                 {
-                    if (Boolean.valueOf(this.jcd.getAttribute("org.apache.jetspeed.engineScoped",
"false")).booleanValue()) {
+                    if (Boolean.valueOf(jcd.getAttribute("org.apache.jetspeed.engineScoped",
"false")).booleanValue()) {
                         ClassLoader cl = Thread.currentThread().getContextClassLoader();
               
                         try
                         {
-                            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
-                            connectionFactory = (ConnectionFactory)
-                                ClassHelper.newInstance (cpd.getConnectionFactory(), true);
-                            connectionFactories.put(cpd.getConnectionFactory(), connectionFactory);
+                            Thread.currentThread().setContextClassLoader(ConnectionManagerImpl.class.getClassLoader());
+                            cf = (ConnectionFactory) ClassHelper.newInstance(cpd.getConnectionFactory(),
true);
+                            connectionFactories.put(cpd.getConnectionFactory(), cf);
                         }
                         finally
                         {
                             Thread.currentThread().setContextClassLoader(cl);
-                            connectionFactories.put(cpd.getConnectionFactory(), connectionFactory);
+                            cf = (ConnectionFactory) ClassHelper.newInstance(cpd.getConnectionFactory(),
true);
+                            connectionFactories.put(cpd.getConnectionFactory(), cf);
                         }
                     }
                     else
                     {
-                        connectionFactory = (ConnectionFactory)
-                        ClassHelper.newInstance (cpd.getConnectionFactory(), true);
+                        cf = (ConnectionFactory) ClassHelper.newInstance(cpd.getConnectionFactory(),
true);
                     }
                 }
                 catch (InstantiationException e)
                 {
                     String err = "Can't instantiate class " + cpd.getConnectionFactory();
-                    log.error(err, e);
                     throw (IllegalStateException)(new IllegalStateException(err)).initCause(e);
                 }
                 catch (IllegalAccessException e)
                 {
                     String err = "Can't instantiate class " + cpd.getConnectionFactory();
-                    log.error(err, e);
                     throw (IllegalStateException)(new IllegalStateException(err)).initCause(e);
                 }
             }
         }
         else 
         {                
-            this.connectionFactory = ConnectionFactoryFactory.getInstance().createConnectionFactory();
+            cf = (ConnectionFactory) ConnectionFactoryFactory.getInstance().createNewInstance();
         }
-        this.platform = PlatformFactory.getPlatformFor(jcd);
-        /*
-        by default batch mode is not enabled and after use of a PB
-        instance, before instance was returned to pool, batch mode
-        was set to false again (PB implementation close method)
-        Be carefully in modify this behaviour, changes could cause
-        unexpected behaviour
-        */
-        setBatchMode(false);
+        return cf;
     }
 
     /**
@@ -485,4 +498,31 @@
             batchCon.clearBatch();
         }
     }
+    
+    /**
+     * Reset cached connection and connection factory.
+     */
+    protected void reset()
+    {
+        // roll back and/or release cached connection
+        if (isInLocalTransaction())
+        {
+            localRollback();
+        }
+        else
+        {
+            releaseConnection();
+        }
+        // reset connection factory
+        connectionFactory = setupConnectionFactory(jcd);
+    }
+
+    /**
+     * Reset cached connection factories.
+     */
+    protected static void resetConnectionFactories()
+    {
+        // clear cached connection factories
+        connectionFactories.clear();
+    }
 }

Added: portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerManagementBean.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerManagementBean.java?rev=775413&view=auto
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerManagementBean.java
(added)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-rdbms/src/main/java/org/apache/jetspeed/components/rdbms/ojb/ConnectionManagerManagementBean.java
Sat May 16 04:37:42 2009
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.components.rdbms.ojb;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Manage OJB ConnectionManagerImpl instance state.
+ * 
+ * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
+ * @version $Id$
+ */
+public class ConnectionManagerManagementBean
+{
+	private static List<ConnectionManagerImpl> connectionManagers = new ArrayList<ConnectionManagerImpl>();
+	
+	/**
+	 * Add connection manager to list of ConnectionManagerImpl instances.
+	 * 
+	 * @param connectionManager connection manager instance
+	 */
+	protected static void addConnectionManager(ConnectionManagerImpl connectionManager)
+	{
+	    // manage connection manager instances
+		synchronized (connectionManagers)
+		{
+			connectionManagers.add(connectionManager);
+		}
+	}
+	
+    /**
+     * Manage connection manager instances on bean creation.
+     */
+    public ConnectionManagerManagementBean()
+    {
+    	// reset connection manager instances
+		synchronized (connectionManagers)
+		{
+		    ConnectionManagerImpl.resetConnectionFactories();
+			for (ConnectionManagerImpl connectionManager : connectionManagers)
+			{
+				connectionManager.reset();
+			}
+		}
+    }
+}

Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/datasource.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/datasource.xml?rev=775413&r1=775412&r2=775413&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/datasource.xml
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/alternate/debug/datasource.xml
Sat May 16 04:37:42 2009
@@ -29,9 +29,11 @@
            <value>com.fmr</value>
          </list>
        </property>
-   </bean>
+    </bean>
        		
-    <bean id="ojbConfigurer" class="org.springframework.orm.ojb.support.LocalOjbConfigurer"/>

+    <bean id="ojbConnectionManagement" class="org.apache.jetspeed.components.rdbms.ojb.ConnectionManagerManagementBean"
/>
+
+    <bean id="ojbConfigurer" class="org.springframework.orm.ojb.support.LocalOjbConfigurer"
depends-on="ojbConnectionManagement" />
 
     <!-- 
          Dynamically configures Database Platform for OJB by looking at the connection string

Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/boot/datasource.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/boot/datasource.xml?rev=775413&r1=775412&r2=775413&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/boot/datasource.xml
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/boot/datasource.xml
Sat May 16 04:37:42 2009
@@ -27,7 +27,9 @@
     <property name="jndiName" value="java:comp/env/jdbc/jetspeed" />
   </bean>
 
-  <bean id="ojbConfigurer" class="org.springframework.orm.ojb.support.LocalOjbConfigurer"
/>
+  <bean id="ojbConnectionManagement" class="org.apache.jetspeed.components.rdbms.ojb.ConnectionManagerManagementBean"
/>
+
+  <bean id="ojbConfigurer" class="org.springframework.orm.ojb.support.LocalOjbConfigurer"
depends-on="ojbConnectionManagement" />
 
   <!-- 
     Dynamically configures Database Platform for OJB by looking at the connection string



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message