Skip to content

Commit

Permalink
Cache connections with the same jdbc url (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
btk5h committed Dec 20, 2017
1 parent b3c5c36 commit 3c485cf
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

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;
Expand All @@ -30,6 +33,8 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%");
}

private static Map<String, HikariDataSource> connectionCache = new HashMap<>();

private Expression<String> url;

@Override
Expand All @@ -43,9 +48,15 @@ protected HikariDataSource[] get(Event e) {
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};
}

Expand Down

0 comments on commit 3c485cf

Please sign in to comment.