#1119 - Deprecate TxRunnable and TxCallable - migrate to plan JDK Runnable and Callable. Add explicit executeCall() method

This commit is contained in:
rob bygrave committed 2017-09-10 20:47:26 +12:00
1 parent 9a7646195c
commit f77f3c3d6e
7 files changed
+127 -59

No files matched your search

+31 -14
View File
@@ -14,6 +14,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@@ -1182,20 +1183,20 @@ public final class Ebean {
* *
* }</pre> * }</pre>
*/ */
public static void execute(TxScope scope, TxRunnable r) { public static void execute(TxScope scope, Runnable r) {
serverMgr.getDefaultServer().execute(scope, r); serverMgr.getDefaultServer().execute(scope, r);
} }
/** /**
* Execute a TxRunnable in a Transaction with the default scope. * Execute a Runnable in a Transaction with the default scope.
* <p> * <p>
* The default scope runs with REQUIRED and by default will rollback on any * The default scope runs with REQUIRED and by default will rollback on any
* exception (checked or runtime). * exception (checked or runtime).
* </p> * </p>
* <pre>{@code * <pre>{@code
* *
* Ebean.execute(new TxRunnable() { * Ebean.execute(() -> {
* public void run() { *
* User u1 = Ebean.find(User.class, 1); * User u1 = Ebean.find(User.class, 1);
* User u2 = Ebean.find(User.class, 2); * User u2 = Ebean.find(User.class, 2);
* *
@@ -1204,17 +1205,17 @@ public final class Ebean {
* *
* Ebean.save(u1); * Ebean.save(u1);
* Ebean.save(u2); * Ebean.save(u2);
* } *
* }); * });
* *
* }</pre> * }</pre>
*/ */
public static void execute(TxRunnable r) { public static void execute(Runnable r) {
serverMgr.getDefaultServer().execute(r); serverMgr.getDefaultServer().execute(r);
} }
/** /**
* Execute a TxCallable in a Transaction with an explicit scope. * Execute a Callable in a Transaction with an explicit scope.
* <p> * <p>
* The scope can control the transaction type, isolation and rollback * The scope can control the transaction type, isolation and rollback
* semantics. * semantics.
@@ -1224,7 +1225,7 @@ public final class Ebean {
* // set specific transactional scope settings * // set specific transactional scope settings
* TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE);
* *
* Ebean.execute(scope, new TxCallable<String>() { * Ebean.executeCall(scope, new Callable<String>() {
* public String call() { * public String call() {
* User u1 = Ebean.find(User.class, 1); * User u1 = Ebean.find(User.class, 1);
* ... * ...
@@ -1234,12 +1235,20 @@ public final class Ebean {
* *
* }</pre> * }</pre>
*/ */
public static <T> T execute(TxScope scope, TxCallable<T> c) { public static <T> T executeCall(TxScope scope, Callable<T> c) {
return serverMgr.getDefaultServer().execute(scope, c); return serverMgr.getDefaultServer().executeCall(scope, c);
} }
/** /**
* Execute a TxCallable in a Transaction with the default scope. * Deprecated - please migrate to executeCall().
*/
@Deprecated
public static <T> T execute(TxScope scope, TxCallable<T> c) {
return serverMgr.getDefaultServer().executeCall(scope, c);
}
/**
* Execute a Callable in a Transaction with the default scope.
* <p> * <p>
* The default scope runs with REQUIRED and by default will rollback on any * The default scope runs with REQUIRED and by default will rollback on any
* exception (checked or runtime). * exception (checked or runtime).
@@ -1250,8 +1259,8 @@ public final class Ebean {
* </p> * </p>
* <pre>{@code * <pre>{@code
* *
* Ebean.execute(new TxCallable<String>() { * Ebean.executeCall(() -> {
* public String call() { *
* User u1 = Ebean.find(User.class, 1); * User u1 = Ebean.find(User.class, 1);
* User u2 = Ebean.find(User.class, 2); * User u2 = Ebean.find(User.class, 2);
* *
@@ -1262,11 +1271,19 @@ public final class Ebean {
* Ebean.save(u2); * Ebean.save(u2);
* *
* return u1.getEmail(); * return u1.getEmail();
* } *
* }); * });
* *
* }</pre> * }</pre>
*/ */
public static <T> T executeCall(Callable<T> c) {
return serverMgr.getDefaultServer().executeCall(c);
}
/**
* Deprecated - please migrate to executeCall().
*/
@Deprecated
public static <T> T execute(TxCallable<T> c) { public static <T> T execute(TxCallable<T> c) {
return serverMgr.getDefaultServer().execute(c); return serverMgr.getDefaultServer().execute(c);
} }
+24 -15
View File
@@ -16,6 +16,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -1587,7 +1588,7 @@ public interface EbeanServer {
int execute(CallableSql callableSql, Transaction transaction); int execute(CallableSql callableSql, Transaction transaction);
/** /**
* Execute a TxRunnable in a Transaction with an explicit scope. * Execute a Runnable in a Transaction with an explicit scope.
* <p> * <p>
* The scope can control the transaction type, isolation and rollback * The scope can control the transaction type, isolation and rollback
* semantics. * semantics.
@@ -1598,7 +1599,7 @@ public interface EbeanServer {
* // set specific transactional scope settings * // set specific transactional scope settings
* TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE);
* *
* ebeanServer.execute(scope, new TxRunnable() { * ebeanServer.execute(scope, new Runnable() {
* public void run() { * public void run() {
* User u1 = Ebean.find(User.class, 1); * User u1 = Ebean.find(User.class, 1);
* ... * ...
@@ -1607,10 +1608,10 @@ public interface EbeanServer {
* *
* }</pre> * }</pre>
*/ */
void execute(TxScope scope, TxRunnable runnable); void execute(TxScope scope, Runnable runnable);
/** /**
* Execute a TxRunnable in a Transaction with the default scope. * Execute a Runnable in a Transaction with the default scope.
* <p> * <p>
* The default scope runs with REQUIRED and by default will rollback on any * The default scope runs with REQUIRED and by default will rollback on any
* exception (checked or runtime). * exception (checked or runtime).
@@ -1618,8 +1619,8 @@ public interface EbeanServer {
* <p> * <p>
* <pre>{@code * <pre>{@code
* *
* ebeanServer.execute(new TxRunnable() { * ebeanServer.execute(() -> {
* public void run() { *
* User u1 = ebeanServer.find(User.class, 1); * User u1 = ebeanServer.find(User.class, 1);
* User u2 = ebeanServer.find(User.class, 2); * User u2 = ebeanServer.find(User.class, 2);
* *
@@ -1628,12 +1629,12 @@ public interface EbeanServer {
* *
* ebeanServer.save(u1); * ebeanServer.save(u1);
* ebeanServer.save(u2); * ebeanServer.save(u2);
* } *
* }); * });
* *
* }</pre> * }</pre>
*/ */
void execute(TxRunnable runnable); void execute(Runnable runnable);
/** /**
* Execute a TxCallable in a Transaction with an explicit scope. * Execute a TxCallable in a Transaction with an explicit scope.
@@ -1647,7 +1648,7 @@ public interface EbeanServer {
* // set specific transactional scope settings * // set specific transactional scope settings
* TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE);
* *
* ebeanServer.execute(scope, new TxCallable<String>() { * ebeanServer.executeCall(scope, new Callable<String>() {
* public String call() { * public String call() {
* User u1 = ebeanServer.find(User.class, 1); * User u1 = ebeanServer.find(User.class, 1);
* ... * ...
@@ -1657,22 +1658,24 @@ public interface EbeanServer {
* *
* }</pre> * }</pre>
*/ */
<T> T executeCall(TxScope scope, Callable<T> callable);
/**
* Deprecated - please migrate to executeCall().
*/
@Deprecated
<T> T execute(TxScope scope, TxCallable<T> callable); <T> T execute(TxScope scope, TxCallable<T> callable);
/** /**
* Execute a TxCallable in a Transaction with the default scope. * Execute a Callable in a Transaction with the default scope.
* <p> * <p>
* The default scope runs with REQUIRED and by default will rollback on any * The default scope runs with REQUIRED and by default will rollback on any
* exception (checked or runtime). * exception (checked or runtime).
* </p> * </p>
* <p> * <p>
* This is basically the same as TxRunnable except that it returns an Object
* (and you specify the return type via generics).
* </p>
* <p>
* <pre>{@code * <pre>{@code
* *
* ebeanServer.execute(new TxCallable<String>() { * ebeanServer.executeCall(new Callable<String>() {
* public String call() { * public String call() {
* User u1 = ebeanServer.find(User.class, 1); * User u1 = ebeanServer.find(User.class, 1);
* User u2 = ebeanServer.find(User.class, 2); * User u2 = ebeanServer.find(User.class, 2);
@@ -1689,6 +1692,12 @@ public interface EbeanServer {
* *
* }</pre> * }</pre>
*/ */
<T> T executeCall(Callable<T> callable);
/**
* Deprecated - please migrate to executeCall().
*/
@Deprecated
<T> T execute(TxCallable<T> callable); <T> T execute(TxCallable<T> callable);
/** /**
+7 -4
View File
@@ -1,9 +1,13 @@
package io.ebean; package io.ebean;
import java.util.concurrent.Callable;
/** /**
* Deprecated - please migrate to just using Callable instead with executeCall().
*
* Execute a TxCallable in a Transaction scope. * Execute a TxCallable in a Transaction scope.
* <p> * <p>
* Use this with the {@link Ebean#execute(TxCallable)} method. * Use this with the {@link Ebean#executeCall(Callable)} method.
* </p> * </p>
* <p> * <p>
* Note that this is basically the same as TxRunnable except that it returns an * Note that this is basically the same as TxRunnable except that it returns an
@@ -31,10 +35,9 @@ package io.ebean;
* }); * });
* *
* }</pre> * }</pre>
*
* @see TxRunnable
*/ */
public interface TxCallable<T> { @Deprecated
public interface TxCallable<T> extends Callable<T> {
/** /**
* Execute the method within a transaction scope returning the result. * Execute the method within a transaction scope returning the result.
+6 -5
View File
@@ -1,12 +1,12 @@
package io.ebean; package io.ebean;
/** /**
* Deprecated - please migrate to using just Runnable.
*
*
* Execute a TxRunnable in a Transaction scope. * Execute a TxRunnable in a Transaction scope.
* <p> * <p>
* Use this with the {@link Ebean#execute(TxRunnable)} method. * Use this with the {@link Ebean#execute(Runnable)} method.
* </p>
* <p>
* See also {@link TxCallable}.
* </p> * </p>
* <p> * <p>
* <pre>{@code * <pre>{@code
@@ -31,7 +31,8 @@ package io.ebean;
* *
* @see TxCallable * @see TxCallable
*/ */
public interface TxRunnable { @Deprecated
public interface TxRunnable extends Runnable {
/** /**
* Run the method in a transaction sope. * Run the method in a transaction sope.
@@ -106,6 +106,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -665,6 +666,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
@Override @Override
public <T> T execute(TxScope scope, TxCallable<T> c) { public <T> T execute(TxScope scope, TxCallable<T> c) {
return executeCall(scope, c);
}
@Override
public <T> T executeCall(Callable<T> c) {
return executeCall(null, c);
}
@Override
public <T> T executeCall(TxScope scope, Callable<T> c) {
ScopeTrans scopeTrans = createScopeTrans(scope); ScopeTrans scopeTrans = createScopeTrans(scope);
try { try {
return c.call(); return c.call();
@@ -672,8 +683,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
} catch (Error e) { } catch (Error e) {
throw scopeTrans.caughtError(e); throw scopeTrans.caughtError(e);
} catch (RuntimeException e) { } catch (Exception e) {
throw scopeTrans.caughtThrowable(e); throw new PersistenceException(scopeTrans.caughtThrowable(e));
} finally { } finally {
scopeTrans.onFinally(); scopeTrans.onFinally();
@@ -681,12 +692,12 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
} }
@Override @Override
public void execute(TxRunnable r) { public void execute(Runnable r) {
execute(null, r); execute(null, r);
} }
@Override @Override
public void execute(TxScope scope, TxRunnable r) { public void execute(TxScope scope, Runnable r) {
ScopeTrans scopeTrans = createScopeTrans(scope); ScopeTrans scopeTrans = createScopeTrans(scope);
try { try {
r.run(); r.run();
@@ -694,8 +705,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
} catch (Error e) { } catch (Error e) {
throw scopeTrans.caughtError(e); throw scopeTrans.caughtError(e);
} catch (RuntimeException e) { } catch (Exception e) {
throw scopeTrans.caughtThrowable(e); throw new PersistenceException(scopeTrans.caughtThrowable(e));
} finally { } finally {
scopeTrans.onFinally(); scopeTrans.onFinally();
@@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -709,12 +710,12 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
} }
@Override @Override
public void execute(TxScope scope, TxRunnable r) { public void execute(TxScope scope, Runnable r) {
} }
@Override @Override
public void execute(TxRunnable r) { public void execute(Runnable r) {
} }
@@ -728,6 +729,16 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
return null; return null;
} }
@Override
public <T> T executeCall(TxScope scope, Callable<T> callable) {
return null;
}
@Override
public <T> T executeCall(Callable<T> callable) {
return null;
}
@Override @Override
public ServerCacheManager getServerCacheManager() { public ServerCacheManager getServerCacheManager() {
return null; return null;
@@ -2,31 +2,47 @@ package org.tests.transaction;
import io.ebean.BaseTestCase; import io.ebean.BaseTestCase;
import io.ebean.Ebean; import io.ebean.Ebean;
import io.ebean.TxRunnable;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import javax.persistence.PersistenceException;
import java.util.concurrent.Callable;
import static org.assertj.core.api.Assertions.assertThat;
public class TestNested extends BaseTestCase { public class TestNested extends BaseTestCase {
@Test @Test
public void test() { public void testRunnableFail() {
try { try {
Ebean.execute(() -> willFail()); Ebean.execute(this::willFail);
} catch (RuntimeException e) { } catch (PersistenceException e) {
Assert.assertEquals(e.getMessage(), "test rollback"); assertThat(e.getMessage()).contains("test runnable rollback");
}
}
@Test
public void testCallableFail() {
try {
Ebean.execute(this::willFailCallable);
} catch (PersistenceException e) {
assertThat(e.getMessage()).contains("test callable rollback");
} }
} }
private void willFail() { private void willFail() {
Ebean.execute(new TxRunnable() { Ebean.executeCall(() -> {
@Override if (false) {
public void run() { return 123;
String msg = "test rollback";
throw new RuntimeException(msg);
} }
throw new RuntimeException("test runnable rollback");
});
}
private void willFailCallable() {
Ebean.executeCall((Callable<String>) () -> {
throw new Exception("test callable rollback");
}); });
} }
} }