cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject [2/3] cayenne git commit: CAY-2028 | Wrap DataChannelFilter calls in the main transaction
Date Thu, 22 Oct 2015 09:32:26 GMT
CAY-2028 | Wrap DataChannelFilter calls in the main transaction


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/0428fb11
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/0428fb11
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/0428fb11

Branch: refs/heads/master
Commit: 0428fb11e121c8abd86bb9a1c3c2cdd97ad0f5fb
Parents: 3cdd487
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Tue Oct 20 15:05:29 2015 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Tue Oct 20 15:05:29 2015 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/access/DataDomain.java   | 40 +++------
 .../configuration/server/ServerModule.java      |  8 +-
 .../apache/cayenne/tx/TransactionFilter.java    | 86 ++++++++++++++++++++
 .../cayenne/access/DataDomainFiltersIT.java     |  5 +-
 4 files changed, 108 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/0428fb11/cayenne-server/src/main/java/org/apache/cayenne/access/DataDomain.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/DataDomain.java b/cayenne-server/src/main/java/org/apache/cayenne/access/DataDomain.java
index 718b55a..040aa5e 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/DataDomain.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/DataDomain.java
@@ -19,20 +19,10 @@
 
 package org.apache.cayenne.access;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.DataChannelFilter;
 import org.apache.cayenne.DataChannelFilterChain;
-import org.apache.cayenne.DataChannelSyncCallbackAction;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.cache.QueryCache;
@@ -54,6 +44,15 @@ import org.apache.cayenne.tx.TransactionManager;
 import org.apache.cayenne.tx.TransactionalOperation;
 import org.apache.cayenne.util.ToStringBuilder;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
 /**
  * DataDomain performs query routing functions in Cayenne. DataDomain creates
  * single data source abstraction hiding multiple physical data sources from the
@@ -587,34 +586,21 @@ public class DataDomain implements QueryEngine, DataChannel {
 	}
 
 	GraphDiff onSyncNoFilters(final ObjectContext originatingContext, final GraphDiff changes,
int syncType) {
-		DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(
-				getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes,
syncType);
-
-		callbackAction.applyPreCommit();
 
-		GraphDiff result;
-		switch (syncType) {
+        GraphDiff result;
+        switch (syncType) {
 		case DataChannel.ROLLBACK_CASCADE_SYNC:
 			result = onSyncRollback(originatingContext);
 			break;
-		// "cascade" and "no_cascade" are the same from the DataDomain
-		// perspective,
-		// including transaction handling logic
+		// "cascade" and "no_cascade" are the same from the DataDomain perspective
 		case DataChannel.FLUSH_NOCASCADE_SYNC:
 		case DataChannel.FLUSH_CASCADE_SYNC:
-			result = transactionManager.performInTransaction(new TransactionalOperation<GraphDiff>()
{
-				@Override
-				public GraphDiff perform() {
-					return onSyncFlush(originatingContext, changes);
-				}
-			});
-
+			result =  onSyncFlush(originatingContext, changes);
 			break;
 		default:
 			throw new CayenneRuntimeException("Invalid synchronization type: " + syncType);
 		}
 
-		callbackAction.applyPostCommit();
 		return result;
 	}
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0428fb11/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
index 997a579..c761b4c 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
@@ -22,6 +22,7 @@ import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.DefaultObjectMapRetainStrategy;
 import org.apache.cayenne.access.ObjectMapRetainStrategy;
+import org.apache.cayenne.tx.TransactionFilter;
 import org.apache.cayenne.access.dbsync.SchemaUpdateStrategy;
 import org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy;
 import org.apache.cayenne.access.jdbc.SQLTemplateProcessor;
@@ -165,8 +166,11 @@ public class ServerModule implements Module {
 				.add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class)
 				.add(MySQLSniffer.class);
 
-		// configure an empty filter chain
-		binder.bindList(Constants.SERVER_DOMAIN_FILTERS_LIST);
+        binder.bind(TransactionFilter.class).to(TransactionFilter.class);
+
+		// configure a filter chain with only one TransactionFilter as default
+		binder.bindList(Constants.SERVER_DOMAIN_FILTERS_LIST)
+                .add(TransactionFilter.class);
 
 		// configure extended types
 		binder.bindList(Constants.SERVER_DEFAULT_TYPES_LIST).add(new VoidType()).add(new BigDecimalType())

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0428fb11/cayenne-server/src/main/java/org/apache/cayenne/tx/TransactionFilter.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/tx/TransactionFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/tx/TransactionFilter.java
new file mode 100644
index 0000000..e87ca34
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/tx/TransactionFilter.java
@@ -0,0 +1,86 @@
+/*****************************************************************
+ *   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.cayenne.tx;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.DataChannelFilter;
+import org.apache.cayenne.DataChannelFilterChain;
+import org.apache.cayenne.DataChannelSyncCallbackAction;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.QueryResponse;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.graph.GraphDiff;
+import org.apache.cayenne.query.Query;
+
+/**
+ * A {@link DataChannelFilter} that provides transactions.
+ *
+ * @since 4.0
+ */
+public class TransactionFilter implements DataChannelFilter {
+
+    @Inject
+    protected TransactionManager transactionManager;
+
+    @Override
+    public void init(DataChannel channel) {
+    }
+
+    @Override
+    public QueryResponse onQuery(ObjectContext originatingContext, Query query, DataChannelFilterChain
filterChain) {
+        return filterChain.onQuery(originatingContext, query);
+    }
+
+    @Override
+    public GraphDiff onSync(final ObjectContext originatingContext, final GraphDiff changes,
final int syncType, final DataChannelFilterChain filterChain) {
+        DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(
+                originatingContext.getChannel().getEntityResolver().getCallbackRegistry(),
+                originatingContext.getGraphManager(),
+                changes, syncType);
+
+        callbackAction.applyPreCommit();
+
+        GraphDiff result;
+        switch (syncType) {
+            case DataChannel.ROLLBACK_CASCADE_SYNC:
+                result = filterChain.onSync(originatingContext, changes, syncType);;
+                break;
+
+            // including transaction handling logic
+            case DataChannel.FLUSH_NOCASCADE_SYNC:
+            case DataChannel.FLUSH_CASCADE_SYNC:
+                result = transactionManager.performInTransaction(new TransactionalOperation<GraphDiff>()
{
+                    @Override
+                    public GraphDiff perform() {
+                        return filterChain.onSync(originatingContext, changes, syncType);
+                    }
+                });
+
+                break;
+            default:
+                throw new CayenneRuntimeException("Invalid synchronization type: " + syncType);
+        }
+
+        callbackAction.applyPostCommit();
+        return result;
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0428fb11/cayenne-server/src/test/java/org/apache/cayenne/access/DataDomainFiltersIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/DataDomainFiltersIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/access/DataDomainFiltersIT.java
index 36dd7f8..8e84fd0 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/DataDomainFiltersIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/DataDomainFiltersIT.java
@@ -52,10 +52,11 @@ public class DataDomainFiltersIT extends ServerCase {
     private ServerRuntime runtime;
 
     @Test
-    public void testDefaultNoFilters() {
+    public void testDefaultFilters() {
 
+        // There is a default TransactionFilter
         DataDomain domain = runtime.getDataDomain();
-        assertEquals(0, domain.filters.size());
+        assertEquals(1, domain.filters.size());
     }
 
     @Test


Mime
View raw message