Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit ec18e30

Browse files
committed
Cleanup jdbc module
1 parent cbfea51 commit ec18e30

File tree

2 files changed

+15
-61
lines changed

2 files changed

+15
-61
lines changed

modules/jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@
226226
import org.jooby.funzy.Try;
227227

228228
import javax.sql.DataSource;
229-
import java.util.ArrayList;
230229
import java.util.Arrays;
231230
import java.util.HashMap;
232231
import java.util.HashSet;
@@ -406,16 +405,6 @@
406405
*/
407406
public final class Jdbc implements Jooby.Module {
408407

409-
static final Function<Throwable, Void> CCE = x -> {
410-
if (x instanceof ClassCastException) {
411-
StackTraceElement src = x.getStackTrace()[0];
412-
if (src.getFileName() == null || src.getClassName().equals(Jdbc.class.getName())) {
413-
return null;
414-
}
415-
}
416-
throw Throwing.sneakyThrow(x);
417-
};
418-
419408
public static Function<String, String> DB_NAME = url -> {
420409
Throwing.Function3<String, String, String, Object[]> indexOf = (str, t1,
421410
t2) -> {
@@ -444,8 +433,7 @@ public final class Jdbc implements Jooby.Module {
444433

445434
private static final int DEFAULT_POOL_SIZE = 10;
446435

447-
@SuppressWarnings("rawtypes")
448-
private final List<BiConsumer> callback = new ArrayList<>();
436+
private BiConsumer<HikariConfig, Config> callback;
449437

450438
protected final String dbref;
451439

@@ -471,40 +459,34 @@ public Jdbc() {
471459
}
472460

473461
/**
474-
* Configurer callback to apply advanced configuration while bootstrapping hibernate:
462+
* Apply advanced configuration options:
475463
*
476464
* <pre>{@code
477465
* {
478466
* use(new Jdbc()
479-
* .doWith((HikariConfig conf) -> {
480-
* // do with conf
481-
* })
482-
* .doWith((HikariDataSource ds) -> {
483-
* // do with ds
467+
* .doWith((hikari, conf) -> {
468+
* // do with hikari
484469
* })
485470
* );
486471
* }
487472
* }</pre>
488473
*
489474
* @param configurer Configurer callback.
490-
* @return This module
475+
* @return This module.
491476
*/
492-
public <T> Jdbc doWith(final BiConsumer<T, Config> configurer) {
493-
this.callback.add(requireNonNull(configurer, "Configurer required."));
477+
public <T> Jdbc doWith(final BiConsumer<HikariConfig, Config> configurer) {
478+
this.callback = requireNonNull(configurer, "Callback required.");
494479
return this;
495480
}
496481

497482
/**
498-
* Configurer callback to apply advanced configuration while bootstrapping hibernate:
483+
* Apply advanced configuration options:
499484
*
500485
* <pre>{@code
501486
* {
502487
* use(new Jdbc()
503-
* .doWith((HikariConfig conf) -> {
504-
* // do with conf
505-
* })
506-
* .doWith((HikariDataSource ds) -> {
507-
* // do with ds
488+
* .doWith(hikari -> {
489+
* // do with hikari
508490
* })
509491
* );
510492
* }
@@ -513,9 +495,9 @@ public <T> Jdbc doWith(final BiConsumer<T, Config> configurer) {
513495
* @param configurer Configurer callback.
514496
* @return This module
515497
*/
516-
public <T> Jdbc doWith(final Consumer<T> configurer) {
498+
public Jdbc doWith(final Consumer<HikariConfig> configurer) {
517499
requireNonNull(configurer, "Configurer required.");
518-
return doWith((final T b, final Config c) -> configurer.accept(b));
500+
return doWith((h, c) -> configurer.accept(h));
519501
}
520502

521503
@Override
@@ -543,7 +525,9 @@ public void configure(final Env env, final Config config, final Binder binder) {
543525
props.setProperty("url", url);
544526
}
545527

546-
callback(hikariConf, config);
528+
if (callback != null) {
529+
callback.accept(hikariConf, config);
530+
}
547531
HikariDataSource ds = new HikariDataSource(hikariConf);
548532

549533
// bind datasource using dbkey and dbname
@@ -680,15 +664,6 @@ private boolean dataSourcePresent(final ClassLoader loader, final String classNa
680664
}
681665
}
682666

683-
@SuppressWarnings("unchecked")
684-
protected void callback(final Object value, final Config conf) {
685-
callback.forEach(it -> Try.apply(() -> {
686-
it.accept(value, conf);
687-
return null;
688-
// FIXME: review recover
689-
}).recover(CCE::apply).get());
690-
}
691-
692667
private Optional<String> dbtype(final String url) {
693668
String type = Arrays.stream(url.toLowerCase().split(":"))
694669
.filter(

modules/jooby-jdbc/src/test/java/org/jooby/jdbc/JdbcTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,6 @@ public void fsdb() throws Exception {
169169
});
170170
}
171171

172-
@Test
173-
public void cceExceptionInSource() throws Exception {
174-
ClassCastException cce = new ClassCastException();
175-
StackTraceElement e = new StackTraceElement(Jdbc.class.getName(), "accept", null, 0);
176-
cce.setStackTrace(new StackTraceElement[]{e});
177-
Jdbc.CCE.apply(cce);
178-
}
179-
180-
@Test
181-
public void cceExceptionWithoutSource() throws Exception {
182-
ClassCastException cce = new ClassCastException();
183-
StackTraceElement e = new StackTraceElement(JdbcTest.class.getName(), "accept", null, 0);
184-
cce.setStackTrace(new StackTraceElement[]{e});
185-
Jdbc.CCE.apply(cce);
186-
}
187-
188172
@Test
189173
public void dbWithCallback() throws Exception {
190174
Config config = ConfigFactory.parseResources(getClass(), "jdbc.conf");
@@ -785,11 +769,6 @@ public void unknownDb() throws Exception {
785769
});
786770
}
787771

788-
@Test(expected = IllegalStateException.class)
789-
public void cceShouldRethrowException() throws Exception {
790-
Jdbc.CCE.apply(new IllegalStateException());
791-
}
792-
793772
private Block serviceKey(final String db) {
794773
return serviceKey(db, null, null);
795774
}

0 commit comments

Comments
 (0)