Skip to content

Commit 3499e5b

Browse files
committed
isTransactional(..) to Route, support for jooby-ebean.
1 parent eb60834 commit 3499e5b

File tree

6 files changed

+55
-37
lines changed

6 files changed

+55
-37
lines changed

jooby/src/main/java/io/jooby/Context.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.annotations.Transactional;
98
import io.jooby.internal.ReadOnlyContext;
109
import io.jooby.internal.WebSocketSender;
1110

@@ -1284,33 +1283,6 @@ public interface Context extends Registry {
12841283
*/
12851284
@Nonnull Context onComplete(@Nonnull Route.Complete task);
12861285

1287-
/* **********************************************************************************************
1288-
* Default methods
1289-
* **********************************************************************************************
1290-
*/
1291-
1292-
/**
1293-
* Returns whether the matched route is marked as transactional, or returns
1294-
* {@code defaultValue} if the route has not been marked explicitly.
1295-
*
1296-
* @param defaultValue the value to return if the route was not explicitly marked
1297-
* @return whether the route should be considered as transactional
1298-
*/
1299-
default boolean isTransactional(boolean defaultValue) {
1300-
Object attribute = getRoute().attribute(Transactional.ATTRIBUTE);
1301-
1302-
if (attribute == null) {
1303-
return defaultValue;
1304-
}
1305-
1306-
if (attribute instanceof Boolean) {
1307-
return (Boolean) attribute;
1308-
}
1309-
1310-
throw new RuntimeException("Invalid value for route attribute "
1311-
+ Transactional.ATTRIBUTE + ": " + attribute);
1312-
}
1313-
13141286
/* **********************************************************************************************
13151287
* Factory methods
13161288
* **********************************************************************************************

jooby/src/main/java/io/jooby/Route.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby;
77

8+
import io.jooby.annotations.Transactional;
89
import io.jooby.exception.MethodNotAllowedException;
910
import io.jooby.exception.NotAcceptableException;
1011
import io.jooby.exception.NotFoundException;
@@ -971,6 +972,28 @@ public boolean isHttpHead() {
971972
return setDescription(description);
972973
}
973974

975+
/**
976+
* Returns whether this route is marked as transactional, or returns
977+
* {@code defaultValue} if this route has not been marked explicitly.
978+
*
979+
* @param defaultValue the value to return if this route was not explicitly marked
980+
* @return whether this route should be considered as transactional
981+
*/
982+
public boolean isTransactional(boolean defaultValue) {
983+
Object attribute = attribute(Transactional.ATTRIBUTE);
984+
985+
if (attribute == null) {
986+
return defaultValue;
987+
}
988+
989+
if (attribute instanceof Boolean) {
990+
return (Boolean) attribute;
991+
}
992+
993+
throw new RuntimeException("Invalid value for route attribute "
994+
+ Transactional.ATTRIBUTE + ": " + attribute);
995+
}
996+
974997
@Override public String toString() {
975998
return method + " " + pattern;
976999
}

jooby/src/main/java/io/jooby/annotations/Transactional.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
/**
1414
* Useful together with the various route decorators like {@code TransactionalRequest}
15-
* provided by extensions {@code jooby-hibernate} or {@code jooby-jdbi} to toggle it's
16-
* effect for a single route.
15+
* provided by extensions {@code jooby-hibernate}, {@code jooby-jdbi} or {@code jooby-ebean}
16+
* to toggle it's effect for a single route.
1717
* <p>
1818
* Although {@code TransactionalRequest} is configured to be enabled by default,
1919
* for a route method annotated with {@code @Transactional(false)} it won't take effect.

modules/jooby-ebean/src/main/java/io/jooby/ebean/TransactionalRequest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.ebean.Transaction;
1010
import io.jooby.Route;
1111
import io.jooby.ServiceKey;
12+
import io.jooby.annotations.Transactional;
1213

1314
import javax.annotation.Nonnull;
1415

@@ -22,6 +23,8 @@ public class TransactionalRequest implements Route.Decorator {
2223

2324
private ServiceKey<Database> key;
2425

26+
private boolean enabledByDefault = true;
27+
2528
/**
2629
* Creates a transactional request.
2730
*
@@ -40,13 +43,33 @@ public TransactionalRequest() {
4043
key = ServiceKey.key(Database.class);
4144
}
4245

46+
/**
47+
* Sets whether all routes in the scope of this decorator instance
48+
* should be transactional or not ({@code true} by default).
49+
* <p>
50+
* You can use the {@link Transactional} annotation to override this
51+
* option on a single route.
52+
*
53+
* @param enabledByDefault whether routes should be transactional by default
54+
* @return this instance
55+
* @see Transactional
56+
*/
57+
public TransactionalRequest enabledByDefault(boolean enabledByDefault) {
58+
this.enabledByDefault = enabledByDefault;
59+
return this;
60+
}
61+
4362
@Nonnull @Override public Route.Handler apply(@Nonnull Route.Handler next) {
4463
return ctx -> {
45-
Database db = ctx.require(key);
46-
try (Transaction transaction = db.beginTransaction()) {
47-
Object result = next.apply(ctx);
48-
transaction.commit();
49-
return result;
64+
if (ctx.getRoute().isTransactional(enabledByDefault)) {
65+
Database db = ctx.require(key);
66+
try (Transaction transaction = db.beginTransaction()) {
67+
Object result = next.apply(ctx);
68+
transaction.commit();
69+
return result;
70+
}
71+
} else {
72+
return next.apply(ctx);
5073
}
5174
};
5275
}

modules/jooby-hibernate/src/main/java/io/jooby/hibernate/TransactionalRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public TransactionalRequest enabledByDefault(boolean enabledByDefault) {
9797

9898
@Nonnull @Override public Route.Handler apply(@Nonnull Route.Handler next) {
9999
return sessionRequest.apply(ctx -> {
100-
if (ctx.isTransactional(enabledByDefault)) {
100+
if (ctx.getRoute().isTransactional(enabledByDefault)) {
101101
SessionFactory sessionFactory = ctx.require(sessionRequest.getSessionFactoryKey());
102102
Transaction trx = null;
103103
try {

modules/jooby-jdbi/src/main/java/io/jooby/jdbi/TransactionalRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public TransactionalRequest enabledByDefault(boolean enabledByDefault) {
109109

110110
@Nonnull @Override public Route.Handler apply(@Nonnull Route.Handler next) {
111111
return ctx -> {
112-
if (ctx.isTransactional(enabledByDefault)) {
112+
if (ctx.getRoute().isTransactional(enabledByDefault)) {
113113
Jdbi jdbi = ctx.require(key);
114114
try (Handle handle = jdbi.open()) {
115115
RequestScope.bind(jdbi, handle);

0 commit comments

Comments
 (0)