Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.xawierstudio.tools.mysql.store.modes;
- import com.zaxxer.hikari.HikariDataSource;
- import net.xawierstudio.tools.base.ToolsPlugin;
- import net.xawierstudio.tools.mysql.store.Callback;
- import net.xawierstudio.tools.mysql.store.Store;
- import net.xawierstudio.tools.utils.Logger;
- import net.xawierstudio.tools.utils.TimeUtil;
- import org.bukkit.scheduler.BukkitRunnable;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- import java.util.concurrent.atomic.AtomicInteger;
- public class StoreMySQL implements Store
- {
- private final HikariDataSource dataSource;
- private String host;
- private String user;
- private String pass;
- private String name;
- private String prefix;
- private int port;
- private Connection conn;
- private long time;
- private Executor executor;
- private AtomicInteger ai;
- public StoreMySQL(String host, int port, String user, String pass, String name, String prefix) {
- this.host = host;
- this.port = port;
- this.user = user;
- this.pass = pass;
- this.name = name;
- this.prefix = prefix;
- this.executor = Executors.newSingleThreadExecutor();
- this.time = System.currentTimeMillis();
- this.ai = new AtomicInteger();
- new BukkitRunnable() {
- public void run() {
- if (System.currentTimeMillis() - time > TimeUtil.SECOND.getTime(30)) {
- update(false, "DO 1");
- }
- }
- }.runTaskTimerAsynchronously(ToolsPlugin.getPlugin(), 600L, 600L);
- this.dataSource = new HikariDataSource();
- this.dataSource.setMaximumPoolSize(16);
- this.dataSource.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + this.name);
- this.dataSource.setUsername(this.user);
- this.dataSource.setPassword(this.pass);
- this.dataSource.addDataSourceProperty("cachePrepStmts", true);
- this.dataSource.addDataSourceProperty("prepStmtCacheSize", 250);
- this.dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
- }
- static long access$0(StoreMySQL storeMySQL) {
- return storeMySQL.time;
- }
- static Connection access$1(StoreMySQL storeMySQL) {
- return storeMySQL.conn;
- }
- static String access$2(StoreMySQL storeMySQL) {
- return storeMySQL.prefix;
- }
- @Override
- public void update(boolean now, String update) {
- this.time = System.currentTimeMillis();
- Runnable r = () ->
- {
- try (Connection connection = this.dataSource.getConnection())
- {
- PreparedStatement statement = connection.prepareStatement(update.replace("{P}", prefix));
- statement.executeUpdate();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- };
- if (now) {
- r.run();
- } else {
- this.executor.execute(r);
- }
- }
- @Override
- public ResultSet update(String update) {
- try (Connection connection = this.dataSource.getConnection()) {
- PreparedStatement statement = connection.prepareStatement(update);
- statement.executeUpdate(update.replace("{P}", this.prefix), Statement.RETURN_GENERATED_KEYS);
- try (ResultSet rs = statement.getGeneratedKeys()) {
- if (rs.next()) {
- return rs;
- }
- }
- } catch (SQLException e) {
- Logger.warning("An error occurred with given query '" + update.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public void disconnect() {
- if (this.dataSource != null) {
- this.dataSource.close();
- }
- }
- @Override
- public boolean isConnected() {
- try {
- return !this.dataSource.getConnection().isClosed() || this.dataSource.getConnection() == null;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- @Override
- public ResultSet query(final String query) {
- try (Connection connection = this.dataSource.getConnection())
- {
- PreparedStatement statement = connection.prepareStatement(query.replace("{P}", prefix));
- return statement.executeQuery();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- return null;
- }
- @Override
- public void query(String query, Callback<ResultSet> cb) {
- new Thread(() -> {
- try (Connection connection = this.dataSource.getConnection()) {
- PreparedStatement statement = connection.prepareStatement(query);
- try (ResultSet rs = statement.executeQuery()) {
- cb.done(rs);
- }
- } catch (SQLException e) {
- Logger.warning("An error occurred with given query '" + query.replace("{P}", prefix) + "'!", "Error: " + e.getMessage());
- cb.error(e);
- e.printStackTrace();
- }
- }, "MySQL Thread #" + this.ai.getAndIncrement()).start();
- }
- @Override
- public Connection getConnection() {
- try {
- return this.dataSource.getConnection();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public StoreMode getStoreMode() {
- return StoreMode.MYSQL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement