From 3c485cf542e4e3ddefa11676846a69e709a71941 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Tue, 19 Dec 2017 17:01:55 -0800 Subject: [PATCH] Cache connections with the same jdbc url (fixes #2) --- .../com/btk5h/skriptdb/skript/ExprDataSource.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java b/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java index ffce075..f5f3a24 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java +++ b/src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java @@ -4,6 +4,9 @@ import com.zaxxer.hikari.HikariDataSource; import org.bukkit.event.Event; +import java.util.HashMap; +import java.util.Map; + import ch.njol.skript.Skript; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; @@ -30,6 +33,8 @@ public class ExprDataSource extends SimpleExpression { ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%"); } + private static Map connectionCache = new HashMap<>(); + private Expression url; @Override @@ -43,9 +48,15 @@ public class ExprDataSource extends SimpleExpression { jdbcUrl = "jdbc:" + jdbcUrl; } + if (connectionCache.containsKey(jdbcUrl)) { + return new HikariDataSource[]{connectionCache.get(jdbcUrl)}; + } + HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl(jdbcUrl); + connectionCache.put(jdbcUrl, ds); + return new HikariDataSource[]{ds}; }