From cef0c4c81631e7b33bf7f905a814efbc93df231d Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Mon, 19 Feb 2018 13:24:34 -0800 Subject: [PATCH] Add connection lifespan option --- .../btk5h/skriptdb/skript/ExprDataSource.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java b/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java index f5f3a24..ba9749b 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java +++ b/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java @@ -12,6 +12,7 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.Timespan; import ch.njol.util.Kleenean; /** @@ -22,7 +23,7 @@ import ch.njol.util.Kleenean; * * @name Data Source * @index -1 - * @pattern [the] data(base|[ ]source) [(of|at)] %string% + * @pattern [the] data(base|[ ]source) [(of|at)] %string% [with [a] [max[imum]] [connection] life[ ]time of %timespan%]" * @return datasource * @example set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin&password=12345&useSSL=false" * @since 0.1.0 @@ -30,12 +31,14 @@ import ch.njol.util.Kleenean; public class ExprDataSource extends SimpleExpression { static { Skript.registerExpression(ExprDataSource.class, HikariDataSource.class, - ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%"); + ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " + + "[with [a] [max[imum]] [connection] life[ ]time of %timespan%]"); } private static Map connectionCache = new HashMap<>(); private Expression url; + private Expression maxLifetime; @Override protected HikariDataSource[] get(Event e) { @@ -55,6 +58,14 @@ public class ExprDataSource extends SimpleExpression { HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl(jdbcUrl); + if (maxLifetime != null) { + Timespan l = maxLifetime.getSingle(e); + + if (l != null) { + ds.setMaxLifetime(l.getMilliSeconds()); + } + } + connectionCache.put(jdbcUrl, ds); return new HikariDataSource[]{ds}; @@ -80,6 +91,7 @@ public class ExprDataSource extends SimpleExpression { public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { url = (Expression) exprs[0]; + maxLifetime = (Expression) exprs[1]; return true; } }