Author: aadamchik
Date: Thu Dec 6 12:05:52 2012
New Revision: 1417811
URL: http://svn.apache.org/viewvc?rev=1417811&view=rev
Log:
CAY-1778 TransactionManager to simplify user-managed transactions
(shortening ServerRuntime tx api)
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerRuntime.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerRuntime.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerRuntime.java?rev=1417811&r1=1417810&r2=1417811&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerRuntime.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerRuntime.java
Thu Dec 6 12:05:52 2012
@@ -28,13 +28,15 @@ import org.apache.cayenne.configuration.
import org.apache.cayenne.configuration.rop.client.ClientRuntime;
import org.apache.cayenne.di.Module;
import org.apache.cayenne.tx.TransactionManager;
+import org.apache.cayenne.tx.TransactionalOperation;
/**
- * An object representing Cayenne server-stack that connects directly to the
database via
- * JDBC. This is an entry point for user applications to access Cayenne, which
- * encapsulates the dependency injection internals. The term "server" is used
as opposed
- * to ROP "client" (see {@link ClientRuntime}). Any application, desktop,
server, etc.
- * that has a direct JDBC connection should be using this runtime.
+ * An object representing Cayenne server-stack that connects directly to the
+ * database via JDBC. This is an entry point for user applications to access
+ * Cayenne, which encapsulates the dependency injection internals. The term
+ * "server" is used as opposed to ROP "client" (see {@link ClientRuntime}). Any
+ * application, desktop, server, etc. that has a direct JDBC connection should
+ * be using this runtime.
*
* @since 3.1
*/
@@ -45,49 +47,58 @@ public class ServerRuntime extends Cayen
}
/**
- * Creates a server runtime configuring it with a standard set of services
contained
- * in {@link ServerModule}. CayenneServerModule is created with provided
- * 'configurationLocation'. An optional array of extra modules may contain
service
- * overrides and/or user services.
+ * Creates a server runtime configuring it with a standard set of services
+ * contained in {@link ServerModule}. CayenneServerModule is created with
+ * provided 'configurationLocation'. An optional array of extra modules may
+ * contain service overrides and/or user services.
*/
public ServerRuntime(String configurationLocation, Module... extraModules)
{
super(mergeModules(mainModule(configurationLocation), extraModules));
}
/**
- * Creates a server runtime configuring it with a standard set of services
contained
- * in {@link ServerModule}. CayenneServerModule is created with one or more
- * 'configurationLocations'. An optional array of extra modules may
contain service
- * overrides and/or user services.
+ * Creates a server runtime configuring it with a standard set of services
+ * contained in {@link ServerModule}. CayenneServerModule is created with
+ * one or more 'configurationLocations'. An optional array of extra modules
+ * may contain service overrides and/or user services.
*/
public ServerRuntime(String[] configurationLocations, Module...
extraModules) {
super(mergeModules(mainModule(configurationLocations), extraModules));
}
-
+
/**
+ * Runs provided operation wrapped in a single transaction. Transaction
+ * handling delegated to the internal {@link TransactionManager}. Nested
+ * calls to 'performInTransaction' are safe and attached to the same
+ * in-progress transaction. TransactionalOperation can be some arbitrary
+ * user code, which most often than not will consist of multiple Cayenne
+ * operations.
+ *
* @since 3.2
*/
- public TransactionManager getTransactionManager() {
- return injector.getInstance(TransactionManager.class);
+ public <T> T performInTransaction(TransactionalOperation<T> op) {
+ TransactionManager tm = injector.getInstance(TransactionManager.class);
+ return tm.performInTransaction(op);
}
/**
- * Returns the main runtime DataDomain. Note that by default the returned
DataDomain
- * is the same as the main DataChannel returned by {@link #getChannel()}.
Although
- * users may redefine DataChannel provider in the DI registry, for
instance to
- * decorate this DataDomain with a custom wrapper.
+ * Returns the main runtime DataDomain. Note that by default the returned
+ * DataDomain is the same as the main DataChannel returned by
+ * {@link #getChannel()}. Although users may redefine DataChannel provider
+ * in the DI registry, for instance to decorate this DataDomain with a
+ * custom wrapper.
*/
public DataDomain getDataDomain() {
return injector.getInstance(DataDomain.class);
}
/**
- * Provides access to the JDBC DataSource assigned to a given DataNode. A
null
- * argument will work if there's only one DataNode configured.
+ * Provides access to the JDBC DataSource assigned to a given DataNode. A
+ * null argument will work if there's only one DataNode configured.
* <p>
- * Normally Cayenne applications don't need to access DataSource or any
other JDBC
- * code directly, however in some unusual conditions it may be needed, and
this method
- * provides a shortcut to raw JDBC.
+ * Normally Cayenne applications don't need to access DataSource or any
+ * other JDBC code directly, however in some unusual conditions it may be
+ * needed, and this method provides a shortcut to raw JDBC.
*/
public DataSource getDataSource(String dataNodeName) {
DataDomain domain = getDataDomain();
@@ -95,8 +106,7 @@ public class ServerRuntime extends Cayen
if (dataNodeName != null) {
DataNode node = domain.getDataNode(dataNodeName);
if (node == null) {
- throw new IllegalArgumentException("Unknown DataNode name: "
- + dataNodeName);
+ throw new IllegalArgumentException("Unknown DataNode name: " +
dataNodeName);
}
return node.getDataSource();