forked from Limework/skript-db
Cache connections with the same jdbc url (fixes #2)
This commit is contained in:
parent
b3c5c36d28
commit
3c485cf542
@ -4,6 +4,9 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||||||
|
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
import ch.njol.skript.Skript;
|
||||||
import ch.njol.skript.lang.Expression;
|
import ch.njol.skript.lang.Expression;
|
||||||
import ch.njol.skript.lang.ExpressionType;
|
import ch.njol.skript.lang.ExpressionType;
|
||||||
@ -30,6 +33,8 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
|||||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%");
|
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||||
|
|
||||||
private Expression<String> url;
|
private Expression<String> url;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -43,9 +48,15 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
|||||||
jdbcUrl = "jdbc:" + jdbcUrl;
|
jdbcUrl = "jdbc:" + jdbcUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (connectionCache.containsKey(jdbcUrl)) {
|
||||||
|
return new HikariDataSource[]{connectionCache.get(jdbcUrl)};
|
||||||
|
}
|
||||||
|
|
||||||
HikariDataSource ds = new HikariDataSource();
|
HikariDataSource ds = new HikariDataSource();
|
||||||
ds.setJdbcUrl(jdbcUrl);
|
ds.setJdbcUrl(jdbcUrl);
|
||||||
|
|
||||||
|
connectionCache.put(jdbcUrl, ds);
|
||||||
|
|
||||||
return new HikariDataSource[]{ds};
|
return new HikariDataSource[]{ds};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user