SHARE
TWEET

Untitled

a guest Jun 27th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.exel.dwrp.api.data;
  2.  
  3. import com.exel.dwrp.api.system.task.DBBlocklistWordReloadTask;
  4. import com.exel.dwrp.api.system.task.DBReloadTask;
  5. import com.exel.dwrp.api.utils.LogUtils;
  6. import com.zaxxer.hikari.HikariConfig;
  7. import com.zaxxer.hikari.HikariDataSource;
  8.  
  9. import java.io.PrintWriter;
  10. import java.io.StringWriter;
  11. import java.sql.Connection;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Timer;
  15.  
  16. /**
  17.  * MySQL Database connection using Hikari as pooling library
  18.  *
  19.  * @author usman
  20.  * @since 24-03-2017
  21.  */
  22. public class DBConnector {
  23.  
  24.     private static DBConnector instance = null;
  25.     Connection con;
  26.     HikariDataSource ds;
  27.  
  28.     /**
  29.      * Init timer and configuration
  30.      */
  31.     private DBConnector() {
  32.         try {
  33.             initDBPool();
  34.             con = ds.getConnection();
  35.             if (Constant.db_auto_reload) {
  36. //                Timer timer = new Timer(true);
  37. //                timer.scheduleAtFixedRate(new DBReloadTask(), 10000, Constant.db_auto_reload_timer);
  38.                 Timer timer1 = new Timer(true);
  39.                 timer1.scheduleAtFixedRate(new DBBlocklistWordReloadTask(), 10000, Constant.db_auto_reload_blockword_timer);
  40.             }
  41.         } catch (Exception e) {
  42.             StringWriter errors = new StringWriter();
  43.             e.printStackTrace(new PrintWriter(errors));
  44.             LogUtils.getInstance().writeLog(DBConnector.class, LogUtils.Level.ERROR, errors.toString());
  45.             System.out.println("<=== System shutdown. ===>");
  46.             System.out.println("<=== Database connection failed.. please check error log. ===>");
  47.             System.exit(0);
  48.         }
  49.  
  50.     }
  51.  
  52.     public static DBConnector getInstance() {
  53.         if (instance == null) {
  54.             instance = new DBConnector();
  55.         }
  56.         return instance;
  57.     }
  58.  
  59.     /**
  60.      * Init database configuration and pooling library
  61.      */
  62.     private void initDBPool() {
  63.         HikariConfig config = new HikariConfig();
  64.         config.setJdbcUrl("jdbc:mysql://" + Constant.db_host + ":" + Constant.db_port + "/" + Constant.db_name);
  65.         config.setUsername(Constant.db_username);
  66.         config.setPassword(Constant.db_password);
  67.         config.setMaximumPoolSize(Constant.db_connection);
  68.         config.addDataSourceProperty("cachePrepStmts", "true");
  69.         config.addDataSourceProperty("prepStmtCacheSize", "500");
  70.         config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
  71.         config.addDataSourceProperty("useServerPrepStmts","true");
  72.         config.addDataSourceProperty("useLocalSessionState","true");
  73.         config.addDataSourceProperty("useLocalTransactionState","true");
  74.         config.addDataSourceProperty("rewriteBatchedStatements","true");
  75.         config.addDataSourceProperty("cacheResultSetMetadata","true");
  76.         config.addDataSourceProperty("cacheServerConfiguration","true");
  77.         config.addDataSourceProperty("elideSetAutoCommits","true");
  78.         config.addDataSourceProperty("maintainTimeStats","false");
  79.         config.addDataSourceProperty("autoReconnect","true");
  80.         config.addDataSourceProperty("useSSL","false");
  81.         config.setMaxLifetime(Constant.maxLifetime);
  82.         config.setIdleTimeout(Constant.idleTimeout);
  83.         config.setConnectionTimeout(Constant.connectionTimeout);
  84.         config.setValidationTimeout(Constant.validationTimeout);
  85.         config.setConnectionInitSql("SELECT 1");
  86.         config.setConnectionTestQuery("SELECT 1");
  87.         config.setLeakDetectionThreshold(Constant.leakDetectionThreshold);
  88.         ds = new HikariDataSource(config);
  89.     }
  90.  
  91.     /**
  92.      * getting statement of database
  93.      * @return Statement variable
  94.      */
  95.     public Statement getStatement() {
  96.         Statement statement = null;
  97.         try {
  98.             if (con.isClosed()) {
  99.                 initDBPool();
  100.                 con = ds.getConnection();
  101.                 statement = con.createStatement();
  102.             }
  103.         } catch (SQLException e) {
  104.             StringWriter errors = new StringWriter();
  105.             e.printStackTrace(new PrintWriter(errors));
  106.             LogUtils.getInstance().writeLog(DBConnector.class, LogUtils.Level.ERROR, errors.toString());
  107.         }
  108.         try {
  109.             statement = con.createStatement();
  110.         } catch (SQLException e) {
  111.             StringWriter errors = new StringWriter();
  112.             e.printStackTrace(new PrintWriter(errors));
  113.             LogUtils.getInstance().writeLog(DBConnector.class, LogUtils.Level.ERROR, errors.toString());
  114.         }
  115.         return statement;
  116.     }
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top