diff --git a/src/main/java/io/ebean/Ebean.java b/src/main/java/io/ebean/Ebean.java index e05caa93d..a0328ab84 100644 --- a/src/main/java/io/ebean/Ebean.java +++ b/src/main/java/io/ebean/Ebean.java @@ -14,6 +14,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; /** @@ -1182,20 +1183,20 @@ public final class Ebean { * * } */ - public static void execute(TxScope scope, TxRunnable r) { + public static void execute(TxScope scope, Runnable 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. *

* The default scope runs with REQUIRED and by default will rollback on any * exception (checked or runtime). *

*
{@code
    *
-   *   Ebean.execute(new TxRunnable() {
-   *     public void run() {
+   *   Ebean.execute(() -> {
+   *
    *       User u1 = Ebean.find(User.class, 1);
    *       User u2 = Ebean.find(User.class, 2);
    *
@@ -1204,17 +1205,17 @@ public final class Ebean {
    *
    *       Ebean.save(u1);
    *       Ebean.save(u2);
-   *     }
+   *
    *   });
    *
    * }
*/ - public static void execute(TxRunnable r) { + public static void execute(Runnable 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. *

* The scope can control the transaction type, isolation and rollback * semantics. @@ -1224,7 +1225,7 @@ public final class Ebean { * // set specific transactional scope settings * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * - * Ebean.execute(scope, new TxCallable() { + * Ebean.executeCall(scope, new Callable() { * public String call() { * User u1 = Ebean.find(User.class, 1); * ... @@ -1234,12 +1235,20 @@ public final class Ebean { * * } */ - public static T execute(TxScope scope, TxCallable c) { - return serverMgr.getDefaultServer().execute(scope, c); + public static T executeCall(TxScope scope, Callable 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 execute(TxScope scope, TxCallable c) { + return serverMgr.getDefaultServer().executeCall(scope, c); + } + + /** + * Execute a Callable in a Transaction with the default scope. *

* The default scope runs with REQUIRED and by default will rollback on any * exception (checked or runtime). @@ -1250,8 +1259,8 @@ public final class Ebean { *

*
{@code
    *
-   *   Ebean.execute(new TxCallable() {
-   *     public String call() {
+   *   Ebean.executeCall(() -> {
+   *
    *       User u1 = Ebean.find(User.class, 1);
    *       User u2 = Ebean.find(User.class, 2);
    *
@@ -1262,11 +1271,19 @@ public final class Ebean {
    *       Ebean.save(u2);
    *
    *       return u1.getEmail();
-   *     }
+   *
    *   });
    *
    * }
*/ + public static T executeCall(Callable c) { + return serverMgr.getDefaultServer().executeCall(c); + } + + /** + * Deprecated - please migrate to executeCall(). + */ + @Deprecated public static T execute(TxCallable c) { return serverMgr.getDefaultServer().execute(c); } diff --git a/src/main/java/io/ebean/EbeanServer.java b/src/main/java/io/ebean/EbeanServer.java index e19a8d13e..800467d56 100644 --- a/src/main/java/io/ebean/EbeanServer.java +++ b/src/main/java/io/ebean/EbeanServer.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.Callable; import java.util.function.Consumer; import java.util.function.Predicate; @@ -1587,7 +1588,7 @@ public interface EbeanServer { 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. *

* The scope can control the transaction type, isolation and rollback * semantics. @@ -1598,7 +1599,7 @@ public interface EbeanServer { * // set specific transactional scope settings * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * - * ebeanServer.execute(scope, new TxRunnable() { + * ebeanServer.execute(scope, new Runnable() { * public void run() { * User u1 = Ebean.find(User.class, 1); * ... @@ -1607,10 +1608,10 @@ public interface EbeanServer { * * } */ - 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. *

* The default scope runs with REQUIRED and by default will rollback on any * exception (checked or runtime). @@ -1618,8 +1619,8 @@ public interface EbeanServer { *

*

{@code
    *
-   *    ebeanServer.execute(new TxRunnable() {
-   *      public void run() {
+   *    ebeanServer.execute(() -> {
+   *
    *        User u1 = ebeanServer.find(User.class, 1);
    *        User u2 = ebeanServer.find(User.class, 2);
    *
@@ -1628,12 +1629,12 @@ public interface EbeanServer {
    *
    *        ebeanServer.save(u1);
    *        ebeanServer.save(u2);
-   *      }
+   *
    *    });
    *
    * }
*/ - void execute(TxRunnable runnable); + void execute(Runnable runnable); /** * Execute a TxCallable in a Transaction with an explicit scope. @@ -1647,7 +1648,7 @@ public interface EbeanServer { * // set specific transactional scope settings * TxScope scope = TxScope.requiresNew().setIsolation(TxIsolation.SERIALIZABLE); * - * ebeanServer.execute(scope, new TxCallable() { + * ebeanServer.executeCall(scope, new Callable() { * public String call() { * User u1 = ebeanServer.find(User.class, 1); * ... @@ -1657,22 +1658,24 @@ public interface EbeanServer { * * } */ + T executeCall(TxScope scope, Callable callable); + + /** + * Deprecated - please migrate to executeCall(). + */ + @Deprecated T execute(TxScope scope, TxCallable callable); /** - * Execute a TxCallable in a Transaction with the default scope. + * Execute a Callable in a Transaction with the default scope. *

* The default scope runs with REQUIRED and by default will rollback on any * exception (checked or runtime). *

*

- * This is basically the same as TxRunnable except that it returns an Object - * (and you specify the return type via generics). - *

- *

*

{@code
    *
-   *   ebeanServer.execute(new TxCallable() {
+   *   ebeanServer.executeCall(new Callable() {
    *     public String call() {
    *       User u1 = ebeanServer.find(User.class, 1);
    *       User u2 = ebeanServer.find(User.class, 2);
@@ -1689,6 +1692,12 @@ public interface EbeanServer {
    *
    * }
*/ + T executeCall(Callable callable); + + /** + * Deprecated - please migrate to executeCall(). + */ + @Deprecated T execute(TxCallable callable); /** diff --git a/src/main/java/io/ebean/TxCallable.java b/src/main/java/io/ebean/TxCallable.java index fc73bf3d2..8ac3e7499 100644 --- a/src/main/java/io/ebean/TxCallable.java +++ b/src/main/java/io/ebean/TxCallable.java @@ -1,9 +1,13 @@ 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. *

- * Use this with the {@link Ebean#execute(TxCallable)} method. + * Use this with the {@link Ebean#executeCall(Callable)} method. *

*

* Note that this is basically the same as TxRunnable except that it returns an @@ -31,10 +35,9 @@ package io.ebean; * }); * * } - * - * @see TxRunnable */ -public interface TxCallable { +@Deprecated +public interface TxCallable extends Callable { /** * Execute the method within a transaction scope returning the result. diff --git a/src/main/java/io/ebean/TxRunnable.java b/src/main/java/io/ebean/TxRunnable.java index d40c4325d..718ca3789 100644 --- a/src/main/java/io/ebean/TxRunnable.java +++ b/src/main/java/io/ebean/TxRunnable.java @@ -1,12 +1,12 @@ package io.ebean; /** + * Deprecated - please migrate to using just Runnable. + * + * * Execute a TxRunnable in a Transaction scope. *

- * Use this with the {@link Ebean#execute(TxRunnable)} method. - *

- *

- * See also {@link TxCallable}. + * Use this with the {@link Ebean#execute(Runnable)} method. *

*

*

{@code
@@ -31,7 +31,8 @@ package io.ebean;
  *
  * @see TxCallable
  */
-public interface TxRunnable {
+@Deprecated
+public interface TxRunnable extends Runnable {
 
   /**
    * Run the method in a transaction sope.
diff --git a/src/main/java/io/ebeaninternal/server/core/DefaultServer.java b/src/main/java/io/ebeaninternal/server/core/DefaultServer.java
index ec49121e6..d51456427 100644
--- a/src/main/java/io/ebeaninternal/server/core/DefaultServer.java
+++ b/src/main/java/io/ebeaninternal/server/core/DefaultServer.java
@@ -106,6 +106,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
@@ -665,6 +666,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
 
   @Override
   public  T execute(TxScope scope, TxCallable c) {
+    return executeCall(scope, c);
+  }
+
+  @Override
+  public  T executeCall(Callable c) {
+    return executeCall(null, c);
+  }
+
+  @Override
+  public  T executeCall(TxScope scope, Callable c) {
     ScopeTrans scopeTrans = createScopeTrans(scope);
     try {
       return c.call();
@@ -672,8 +683,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
     } catch (Error e) {
       throw scopeTrans.caughtError(e);
 
-    } catch (RuntimeException e) {
-      throw scopeTrans.caughtThrowable(e);
+    } catch (Exception e) {
+      throw new PersistenceException(scopeTrans.caughtThrowable(e));
 
     } finally {
       scopeTrans.onFinally();
@@ -681,12 +692,12 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
   }
 
   @Override
-  public void execute(TxRunnable r) {
+  public void execute(Runnable r) {
     execute(null, r);
   }
 
   @Override
-  public void execute(TxScope scope, TxRunnable r) {
+  public void execute(TxScope scope, Runnable r) {
     ScopeTrans scopeTrans = createScopeTrans(scope);
     try {
       r.run();
@@ -694,8 +705,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
     } catch (Error e) {
       throw scopeTrans.caughtError(e);
 
-    } catch (RuntimeException e) {
-      throw scopeTrans.caughtThrowable(e);
+    } catch (Exception e) {
+      throw new PersistenceException(scopeTrans.caughtThrowable(e));
 
     } finally {
       scopeTrans.onFinally();
diff --git a/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java b/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java
index 6f9f8f228..d371edaba 100644
--- a/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java
+++ b/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.Callable;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
 
@@ -709,12 +710,12 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
   }
 
   @Override
-  public void execute(TxScope scope, TxRunnable r) {
+  public void execute(TxScope scope, Runnable r) {
 
   }
 
   @Override
-  public void execute(TxRunnable r) {
+  public void execute(Runnable r) {
 
   }
 
@@ -728,6 +729,16 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
     return null;
   }
 
+  @Override
+  public  T executeCall(TxScope scope, Callable callable) {
+    return null;
+  }
+
+  @Override
+  public  T executeCall(Callable callable) {
+    return null;
+  }
+
   @Override
   public ServerCacheManager getServerCacheManager() {
     return null;
diff --git a/src/test/java/org/tests/transaction/TestNested.java b/src/test/java/org/tests/transaction/TestNested.java
index 26f893a4a..63dd16298 100644
--- a/src/test/java/org/tests/transaction/TestNested.java
+++ b/src/test/java/org/tests/transaction/TestNested.java
@@ -2,31 +2,47 @@ package org.tests.transaction;
 
 import io.ebean.BaseTestCase;
 import io.ebean.Ebean;
-import io.ebean.TxRunnable;
-import org.junit.Assert;
 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 {
 
   @Test
-  public void test() {
+  public void testRunnableFail() {
 
     try {
-      Ebean.execute(() -> willFail());
-    } catch (RuntimeException e) {
-      Assert.assertEquals(e.getMessage(), "test rollback");
+      Ebean.execute(this::willFail);
+    } catch (PersistenceException e) {
+      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() {
-    Ebean.execute(new TxRunnable() {
-      @Override
-      public void run() {
-
-        String msg = "test rollback";
-        throw new RuntimeException(msg);
-
+    Ebean.executeCall(() -> {
+      if (false) {
+        return 123;
       }
+      throw new RuntimeException("test runnable rollback");
+    });
+  }
+
+  private void willFailCallable() {
+    Ebean.executeCall((Callable) () -> {
+      throw new Exception("test callable rollback");
     });
   }
 }