cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1072575 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne: access/DataDomain.java configuration/server/DelegatingDataSourceFactory.java conn/PoolManager.java
Date Sun, 20 Feb 2011 12:26:52 GMT
Author: aadamchik
Date: Sun Feb 20 12:26:51 2011
New Revision: 1072575

URL: http://svn.apache.org/viewvc?rev=1072575&view=rev
Log:
CAY-1542 DI container is not shutting down Cayenne-managed connection pool

adding manual lifecycle management code for created objects to DelegatingDataSourceFactory

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java?rev=1072575&r1=1072574&r2=1072575&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java
Sun Feb 20 12:26:51 2011
@@ -650,11 +650,17 @@ public class DataDomain implements Query
     @BeforeScopeEnd
     public void shutdown() {
         if (!stopped) {
+            stopped = true;
+
             if (sharedSnapshotCache != null) {
                 sharedSnapshotCache.shutdown();
             }
 
-            stopped = true;
+            // deprecated - noop code for backwards compatibility as DataNode shutdown is
+            // no longer needed
+            for (DataNode node : getDataNodes()) {
+                node.shutdown();
+            }
         }
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java?rev=1072575&r1=1072574&r2=1072575&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
Sun Feb 20 12:26:51 2011
@@ -18,13 +18,19 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.server;
 
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.sql.DataSource;
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.configuration.AdhocObjectFactory;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
 import org.apache.cayenne.configuration.RuntimeProperties;
+import org.apache.cayenne.di.BeforeScopeEnd;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.spi.ScopeEventBinding;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -52,8 +58,48 @@ public class DelegatingDataSourceFactory
     @Inject
     protected RuntimeProperties properties;
 
+    protected Map<DataSource, ScopeEventBinding> managedDataSources;
+
+    public DelegatingDataSourceFactory() {
+        managedDataSources = new ConcurrentHashMap<DataSource, ScopeEventBinding>();
+    }
+
     public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
-        return getDataSourceFactory(nodeDescriptor).getDataSource(nodeDescriptor);
+        DataSource dataSource = getDataSourceFactory(nodeDescriptor).getDataSource(
+                nodeDescriptor);
+        attachToScope(dataSource);
+        return dataSource;
+    }
+
+    @BeforeScopeEnd
+    public void shutdown() {
+        for (ScopeEventBinding binding : managedDataSources.values()) {
+            binding.onScopeEvent();
+        }
+
+        managedDataSources.clear();
+    }
+
+    /**
+     * Ensure that DataSource implementations returned from this factory receive
+     * {@link BeforeScopeEnd} events.
+     */
+    protected void attachToScope(DataSource dataSource) {
+
+        // no real thread-safety here, just checking to speed up processing... in the
+        // worst case we'll replace event binding for the same pool with an equivalent one
+        if (!managedDataSources.containsKey(dataSource)) {
+
+            Class<BeforeScopeEnd> annotationType = BeforeScopeEnd.class;
+            for (Method method : dataSource.getClass().getMethods()) {
+
+                if (method.isAnnotationPresent(annotationType)) {
+                    managedDataSources.put(dataSource, new ScopeEventBinding(
+                            dataSource,
+                            method));
+                }
+            }
+        }
     }
 
     protected DataSourceFactory getDataSourceFactory(DataNodeDescriptor nodeDescriptor) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java?rev=1072575&r1=1072574&r2=1072575&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java
Sun Feb 20 12:26:51 2011
@@ -32,6 +32,8 @@ import javax.sql.ConnectionPoolDataSourc
 import javax.sql.DataSource;
 import javax.sql.PooledConnection;
 
+import org.apache.cayenne.di.BeforeScopeEnd;
+
 /**
  * PoolManager is a pooling DataSource impementation. Internally to obtain connections
  * PoolManager uses either a JDBC driver or another pooling datasource.
@@ -182,6 +184,7 @@ public class PoolManager implements Data
      * 
      * @since 3.1
      */
+    @BeforeScopeEnd
     public void shutdown() throws SQLException {
         synchronized (this) {
             // clean connections from the pool



Mime
View raw message