Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. public class AbstractMySqlHikari extends MySqlExecutor {
  2.     private final HikariDataSource dataSource;
  3.  
  4.     public AbstractMySqlHikari(String username, String password, String url) {
  5.         super(username, password, url);
  6.         this.dataSource = configureDataSource();
  7.     }
  8.     @NotNull
  9.     private HikariDataSource configureDataSource() {
  10.         HikariDataSource source = new HikariDataSource();
  11.         source.setPoolName("RoflanHikariSqlPool");
  12.         source.setUsername(getUsername());
  13.         source.setPassword(getPassword());
  14.         source.setJdbcUrl(getUrl());
  15.         source.addDataSourceProperty("useSSL", "true");
  16.         source.addDataSourceProperty("cachePrepStmts", "true");
  17.         source.addDataSourceProperty("prepStmtCacheSize", "250");
  18.         source.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");//600
  19.         source.addDataSourceProperty("useServerPrepStmts", "true");
  20.         return source;
  21.     }
  22.     @Override
  23.     public Connection getConnection() {
  24.         try {
  25.             return dataSource.getConnection();
  26.         } catch (SQLException e) {
  27.             throw new RuntimeException("[HikariSQLConnection]: Конекшин-Ексепшин 0_o - " + this.getUrl(), e);
  28.         }
  29.     }
  30.     @Override
  31.     public void preparedStatement(final String sql,
  32.                                   StatementConsumer<? super PreparedStatement> statement,
  33.                                   boolean async) {
  34.         execute(() -> {
  35.             try (Connection connection = getConnection();
  36.                  PreparedStatement ps = connection.prepareStatement(sql)
  37.             ) {
  38.                 statement.accept(ps);//execute
  39.             } catch (SQLException e) {
  40.                 e.printStackTrace();
  41.             }
  42.         }, async);
  43.     }
  44.     @Override
  45.     public <T> T preparedStatementExecuteQuery(final String sql,
  46.                                                StatementConsumer<? super PreparedStatement> statement,
  47.                                                ResultConsumer<T> result,
  48.                                                boolean async) {
  49.         return submit(() -> {
  50.             try (Connection connection = getConnection();
  51.                  PreparedStatement ps = connection.prepareStatement(sql)
  52.             ) {
  53.                 statement.accept(ps);
  54.                 try (ResultSet rs = ps.executeQuery()) {
  55.                     return result.get(rs);
  56.                 }
  57.             } catch (SQLException e) {
  58.                 throw new RuntimeException("[MySQL] обосрався результат", e);
  59.             }
  60.         }, async);
  61.     }
  62.     @Override
  63.     public CompletableFuture<Void> preparedStatementExecuteQuery(final String sql,
  64.                                                                  StatementConsumer<? super PreparedStatement> statement,
  65.                                                                  StatementConsumer<? super ResultSet> result,
  66.                                                                  boolean async) {
  67.  
  68.         return execute(() -> {
  69.             try (Connection connection = getConnection();
  70.                  PreparedStatement ps = connection.prepareStatement(sql)
  71.             ) {
  72.                 statement.accept(ps);
  73.                 try (ResultSet rs = ps.executeQuery()) {
  74.                     result.accept(rs);
  75.                 }
  76.             } catch (SQLException e) {
  77.                 e.printStackTrace();
  78.             }
  79.         }, async);
  80.     }
  81.     @Override
  82.     public int preparedStatementAutoGeneratedKeys(final String sql,
  83.                                                   StatementConsumer<? super PreparedStatement> statement,
  84.                                                   int autoGeneratedKeys,
  85.                                                   boolean async) {
  86.         return submit(() -> {
  87.             try (Connection connection = getConnection();
  88.                  PreparedStatement ps = connection.prepareStatement(sql, autoGeneratedKeys)
  89.             ) {
  90.                 statement.accept(ps);
  91.                 try (ResultSet rs = ps.getGeneratedKeys()) {
  92.                     if (rs.next()) {
  93.                         return rs.getInt(1);
  94.                     }
  95.                 }
  96.             } catch (SQLException e) {
  97.                 e.printStackTrace();
  98.             }
  99.             return -1;
  100.         }, async);
  101.     }
  102.     @Override
  103.     public void statementExecute(final String sql, boolean async) {
  104.         execute(() -> {
  105.             try (Connection connection = getConnection();
  106.                  Statement statement = connection.createStatement()
  107.             ) {
  108.                 statement.execute(sql);
  109.             } catch (SQLException e) {
  110.                 e.printStackTrace();
  111.             }
  112.         }, async);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement