Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. package net.xawierstudio.tools.mysql.store.modes;
  2.  
  3. import com.zaxxer.hikari.HikariDataSource;
  4. import net.xawierstudio.tools.base.ToolsPlugin;
  5. import net.xawierstudio.tools.mysql.store.Callback;
  6. import net.xawierstudio.tools.mysql.store.Store;
  7. import net.xawierstudio.tools.utils.Logger;
  8. import net.xawierstudio.tools.utils.TimeUtil;
  9. import org.bukkit.scheduler.BukkitRunnable;
  10.  
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.concurrent.Executor;
  17. import java.util.concurrent.Executors;
  18. import java.util.concurrent.atomic.AtomicInteger;
  19.  
  20. public class StoreMySQL implements Store
  21. {
  22. private final HikariDataSource dataSource;
  23.  
  24. private String host;
  25. private String user;
  26. private String pass;
  27. private String name;
  28. private String prefix;
  29. private int port;
  30. private Connection conn;
  31. private long time;
  32. private Executor executor;
  33. private AtomicInteger ai;
  34.  
  35. public StoreMySQL(String host, int port, String user, String pass, String name, String prefix) {
  36. this.host = host;
  37. this.port = port;
  38. this.user = user;
  39. this.pass = pass;
  40. this.name = name;
  41. this.prefix = prefix;
  42. this.executor = Executors.newSingleThreadExecutor();
  43. this.time = System.currentTimeMillis();
  44. this.ai = new AtomicInteger();
  45. new BukkitRunnable() {
  46. public void run() {
  47. if (System.currentTimeMillis() - time > TimeUtil.SECOND.getTime(30)) {
  48. update(false, "DO 1");
  49. }
  50. }
  51. }.runTaskTimerAsynchronously(ToolsPlugin.getPlugin(), 600L, 600L);
  52.  
  53. this.dataSource = new HikariDataSource();
  54. this.dataSource.setMaximumPoolSize(16);
  55. this.dataSource.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + this.name);
  56. this.dataSource.setUsername(this.user);
  57. this.dataSource.setPassword(this.pass);
  58. this.dataSource.addDataSourceProperty("cachePrepStmts", true);
  59. this.dataSource.addDataSourceProperty("prepStmtCacheSize", 250);
  60. this.dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
  61. }
  62.  
  63. static long access$0(StoreMySQL storeMySQL) {
  64. return storeMySQL.time;
  65. }
  66.  
  67. static Connection access$1(StoreMySQL storeMySQL) {
  68. return storeMySQL.conn;
  69. }
  70.  
  71. static String access$2(StoreMySQL storeMySQL) {
  72. return storeMySQL.prefix;
  73. }
  74.  
  75. @Override
  76. public void update(boolean now, String update) {
  77. this.time = System.currentTimeMillis();
  78.  
  79. Runnable r = () ->
  80. {
  81. try (Connection connection = this.dataSource.getConnection())
  82. {
  83. PreparedStatement statement = connection.prepareStatement(update.replace("{P}", prefix));
  84. statement.executeUpdate();
  85. }
  86. catch (SQLException ex)
  87. {
  88. ex.printStackTrace();
  89. }
  90. };
  91.  
  92. if (now) {
  93. r.run();
  94. } else {
  95. this.executor.execute(r);
  96. }
  97. }
  98.  
  99. @Override
  100. public ResultSet update(String update) {
  101. try (Connection connection = this.dataSource.getConnection()) {
  102. PreparedStatement statement = connection.prepareStatement(update);
  103. statement.executeUpdate(update.replace("{P}", this.prefix), Statement.RETURN_GENERATED_KEYS);
  104.  
  105. try (ResultSet rs = statement.getGeneratedKeys()) {
  106. if (rs.next()) {
  107. return rs;
  108. }
  109. }
  110. } catch (SQLException e) {
  111. Logger.warning("An error occurred with given query '" + update.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
  112. e.printStackTrace();
  113. }
  114. return null;
  115. }
  116.  
  117. @Override
  118. public void disconnect() {
  119. if (this.dataSource != null) {
  120. this.dataSource.close();
  121. }
  122. }
  123.  
  124. @Override
  125. public boolean isConnected() {
  126. try {
  127. return !this.dataSource.getConnection().isClosed() || this.dataSource.getConnection() == null;
  128. } catch (SQLException e) {
  129. e.printStackTrace();
  130. return false;
  131. }
  132. }
  133.  
  134. @Override
  135. public ResultSet query(final String query) {
  136. try (Connection connection = this.dataSource.getConnection())
  137. {
  138. PreparedStatement statement = connection.prepareStatement(query.replace("{P}", prefix));
  139. return statement.executeQuery();
  140. }
  141. catch (SQLException ex)
  142. {
  143. ex.printStackTrace();
  144. }
  145. return null;
  146. }
  147.  
  148.  
  149. @Override
  150. public void query(String query, Callback<ResultSet> cb) {
  151. new Thread(() -> {
  152. try (Connection connection = this.dataSource.getConnection()) {
  153. PreparedStatement statement = connection.prepareStatement(query);
  154.  
  155. try (ResultSet rs = statement.executeQuery()) {
  156. cb.done(rs);
  157. }
  158.  
  159. } catch (SQLException e) {
  160. Logger.warning("An error occurred with given query '" + query.replace("{P}", prefix) + "'!", "Error: " + e.getMessage());
  161. cb.error(e);
  162. e.printStackTrace();
  163. }
  164. }, "MySQL Thread #" + this.ai.getAndIncrement()).start();
  165. }
  166.  
  167. @Override
  168. public Connection getConnection() {
  169. try {
  170. return this.dataSource.getConnection();
  171. }
  172. catch (SQLException e) {
  173. e.printStackTrace();
  174. }
  175.  
  176. return null;
  177. }
  178.  
  179. @Override
  180. public StoreMode getStoreMode() {
  181. return StoreMode.MYSQL;
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement