|
1 | 1 | package simplexity.simplevanish.saving; |
2 | 2 |
|
| 3 | +import com.zaxxer.hikari.HikariConfig; |
| 4 | +import com.zaxxer.hikari.HikariDataSource; |
3 | 5 | import simplexity.simplevanish.SimpleVanish; |
4 | 6 | import simplexity.simplevanish.config.ConfigHandler; |
5 | 7 | import simplexity.simplevanish.objects.PlayerVanishSettings; |
6 | 8 |
|
7 | 9 | import java.sql.Connection; |
8 | | -import java.sql.DriverManager; |
9 | 10 | import java.sql.PreparedStatement; |
10 | 11 | import java.sql.ResultSet; |
11 | 12 | import java.sql.SQLException; |
|
16 | 17 | @SuppressWarnings({"FieldCanBeLocal", "CallToPrintStackTrace"}) |
17 | 18 |
|
18 | 19 | public class SqlHandler { |
| 20 | + private static final HikariConfig hikariConfig = new HikariConfig(); |
| 21 | + private static HikariDataSource dataSource; |
| 22 | + |
19 | 23 | Connection connection; |
20 | 24 | Logger logger = SimpleVanish.getInstance().getLogger(); |
21 | 25 | private final String initStatement = """ |
@@ -65,6 +69,7 @@ public static SqlHandler getInstance() { |
65 | 69 |
|
66 | 70 |
|
67 | 71 | public void init() { |
| 72 | + setupConfig(); |
68 | 73 | try { |
69 | 74 | connection = getConnection(); |
70 | 75 | try (Statement statement = connection.createStatement()) { |
@@ -135,16 +140,23 @@ public void updateSettings(UUID uuid) { |
135 | 140 | } |
136 | 141 | } |
137 | 142 |
|
138 | | - private Connection getConnection() throws SQLException { |
| 143 | + private static Connection getConnection() throws SQLException { |
| 144 | + return dataSource.getConnection(); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + private void setupConfig() { |
139 | 149 | if (ConfigHandler.getInstance().isMysqlEnabled()) { |
140 | | - return DriverManager.getConnection("jdbc:mysql://" + ConfigHandler.getInstance().getMysqlIP() + "/" |
141 | | - + ConfigHandler.getInstance().getDatabaseName(), |
142 | | - ConfigHandler.getInstance().getDatabaseUsername(), |
143 | | - ConfigHandler.getInstance().getDatabasePassword()); |
| 150 | + hikariConfig.setJdbcUrl("jdbc:mysql://" + ConfigHandler.getInstance().getMysqlIP() + "/" |
| 151 | + + ConfigHandler.getInstance().getDatabaseName()); |
| 152 | + hikariConfig.setUsername(ConfigHandler.getInstance().getDatabaseUsername()); |
| 153 | + hikariConfig.setPassword(ConfigHandler.getInstance().getDatabasePassword()); |
| 154 | + dataSource = new HikariDataSource(hikariConfig); |
| 155 | + return; |
144 | 156 | } |
145 | | - return DriverManager.getConnection("jdbc:sqlite:" |
146 | | - + SimpleVanish.getInstance().getDataFolder() |
147 | | - + "/vanish-settings.db"); |
| 157 | + hikariConfig.setJdbcUrl("jdbc:sqlite:" + SimpleVanish.getInstance().getDataFolder() + "/vanish-settings.db"); |
| 158 | + hikariConfig.setConnectionTestQuery("PRAGMA journal_mode = WAL;"); |
| 159 | + dataSource = new HikariDataSource(hikariConfig); |
148 | 160 | } |
149 | 161 |
|
150 | 162 | } |
0 commit comments