Advertisement
Guest User

Untitled

a guest
Oct 31st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. public class SQLDatabase extends IDatabase<Connection>{
  2.  
  3. private final String host, database, username, password;
  4. private final int port;
  5. private HikariDataSource hikariDataSource;
  6.  
  7. /**
  8. * Initialisation des informations concernants la connection.
  9. *
  10. * @param host
  11. * @param database
  12. * @param username
  13. * @param password
  14. */
  15. public SQLDatabase(String host, int port, String database, String username, String password) {
  16. this.host = host;
  17. this.port = port;
  18. this.database = database;
  19. this.username = username;
  20. this.password = password;
  21.  
  22. registerModule(AccountModule.class);
  23. registerModule(PlayerInfoModule.class);
  24. }
  25.  
  26. @Override
  27. public void connect() {
  28. final HikariConfig hikariConfig = new HikariConfig();
  29. hikariConfig.setJdbcUrl("jdbc:mysql://" + this.host + ":"+port+"/" + this.database + "?useUnicode=yes");
  30. hikariConfig.setUsername(username);
  31. hikariConfig.setPassword(password);
  32. hikariConfig.setMaxLifetime(4 * 60 * 60 * 1000);
  33. hikariConfig.setLeakDetectionThreshold(300000L);
  34. hikariConfig.setMaximumPoolSize(4);
  35. hikariConfig.setConnectionTimeout(20000L);
  36. hikariConfig.addDataSourceProperty("useSSL", false);
  37. this.hikariDataSource = new HikariDataSource(hikariConfig);
  38.  
  39. /*if (!isConnected()) {
  40. try {
  41. //this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":"+port+"/" + this.database + "?autoReconnect=true&useUnicode=yes", this.username, this.password);
  42. } catch (SQLException exception) {
  43. exception.printStackTrace();
  44. }
  45. } else {
  46. // TODO: Finir le Logger
  47. }*/
  48. }
  49.  
  50. @Override
  51. public void disconnect() {
  52. if (!isConnected()) {
  53. this.hikariDataSource.close();
  54. } else {
  55. // TODO: Finir le Logger
  56. }
  57. }
  58.  
  59. @Override
  60. public boolean isConnected() {
  61. return hikariDataSource != null && !hikariDataSource.isClosed();
  62. }
  63.  
  64. @Override
  65. public Connection getResource() {
  66. try{
  67. return hikariDataSource.getConnection();
  68. }catch (SQLException e){
  69. e.printStackTrace();
  70. }
  71. return null;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement