Advertisement
stonar96

Connection Pool Tutorial onEnable

Jan 31st, 2016
2,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1.     private final ComboPooledDataSource dataSource = new ComboPooledDataSource();  //The connection pool
  2.    
  3.     @Override
  4.     public void onEnable() {
  5.         saveResource("c3p0-0.9.5.1.jar", true);
  6.         saveResource("mchange-commons-java-0.2.10.jar", true);
  7.         saveResource("mysql-connector-java-5.1.37-bin.jar", true);
  8.        
  9.         getConfig().options().copyDefaults(true);
  10.         getConfig().addDefault("database.driver-class", "com.mysql.jdbc.Driver");
  11.         getConfig().addDefault("database.jdbc-url", "jdbc:mysql://<host>:<port>/<database>");
  12.         getConfig().addDefault("database.host", "localhost");
  13.         getConfig().addDefault("database.port", "3306");
  14.         getConfig().addDefault("database.database", "database");
  15.         getConfig().addDefault("database.user", "user");
  16.         getConfig().addDefault("database.password", "password");
  17.         getConfig().addDefault("database.checkout-timeout", 5000);
  18.         saveConfig();
  19.        
  20.         ConfigurationSection databaseSection = getConfig().getConfigurationSection("database");
  21.         String driverClass = databaseSection.getString("driver-class");
  22.         String jdbcUrl = databaseSection.getString("jdbc-url");
  23.         String host = databaseSection.getString("host");
  24.         String port = databaseSection.getString("port");
  25.         String database = databaseSection.getString("database");
  26.         String user = databaseSection.getString("user");
  27.         String password = databaseSection.getString("password");
  28.         int checkoutTimeout = databaseSection.getInt("checkout-timeout");
  29.  
  30.         try {
  31.             dataSource.setDriverClass(driverClass);
  32.         } catch (PropertyVetoException e) {
  33.             e.printStackTrace();
  34.         }
  35.        
  36.         dataSource.setJdbcUrl(jdbcUrl.replace("<host>", host).replace("<port>", port).replace("<database>", database));
  37.         dataSource.setUser(user);
  38.         dataSource.setPassword(password);
  39.         dataSource.setCheckoutTimeout(checkoutTimeout);
  40.     }
  41.    
  42.     @Override
  43.     public void onDisable() {
  44.         dataSource.close();
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement