Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AbstractMySqlHikari extends MySqlExecutor {
- private final HikariDataSource dataSource;
- public AbstractMySqlHikari(String username, String password, String url) {
- super(username, password, url);
- this.dataSource = configureDataSource();
- }
- @NotNull
- private HikariDataSource configureDataSource() {
- HikariDataSource source = new HikariDataSource();
- source.setPoolName("RoflanHikariSqlPool");
- source.setUsername(getUsername());
- source.setPassword(getPassword());
- source.setJdbcUrl(getUrl());
- source.addDataSourceProperty("useSSL", "true");
- source.addDataSourceProperty("cachePrepStmts", "true");
- source.addDataSourceProperty("prepStmtCacheSize", "250");
- source.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");//600
- source.addDataSourceProperty("useServerPrepStmts", "true");
- return source;
- }
- @Override
- public Connection getConnection() {
- try {
- return dataSource.getConnection();
- } catch (SQLException e) {
- throw new RuntimeException("[HikariSQLConnection]: Конекшин-Ексепшин 0_o - " + this.getUrl(), e);
- }
- }
- @Override
- public void preparedStatement(final String sql,
- StatementConsumer<? super PreparedStatement> statement,
- boolean async) {
- execute(() -> {
- try (Connection connection = getConnection();
- PreparedStatement ps = connection.prepareStatement(sql)
- ) {
- statement.accept(ps);//execute
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }, async);
- }
- @Override
- public <T> T preparedStatementExecuteQuery(final String sql,
- StatementConsumer<? super PreparedStatement> statement,
- ResultConsumer<T> result,
- boolean async) {
- return submit(() -> {
- try (Connection connection = getConnection();
- PreparedStatement ps = connection.prepareStatement(sql)
- ) {
- statement.accept(ps);
- try (ResultSet rs = ps.executeQuery()) {
- return result.get(rs);
- }
- } catch (SQLException e) {
- throw new RuntimeException("[MySQL] обосрався результат", e);
- }
- }, async);
- }
- @Override
- public CompletableFuture<Void> preparedStatementExecuteQuery(final String sql,
- StatementConsumer<? super PreparedStatement> statement,
- StatementConsumer<? super ResultSet> result,
- boolean async) {
- return execute(() -> {
- try (Connection connection = getConnection();
- PreparedStatement ps = connection.prepareStatement(sql)
- ) {
- statement.accept(ps);
- try (ResultSet rs = ps.executeQuery()) {
- result.accept(rs);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }, async);
- }
- @Override
- public int preparedStatementAutoGeneratedKeys(final String sql,
- StatementConsumer<? super PreparedStatement> statement,
- int autoGeneratedKeys,
- boolean async) {
- return submit(() -> {
- try (Connection connection = getConnection();
- PreparedStatement ps = connection.prepareStatement(sql, autoGeneratedKeys)
- ) {
- statement.accept(ps);
- try (ResultSet rs = ps.getGeneratedKeys()) {
- if (rs.next()) {
- return rs.getInt(1);
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return -1;
- }, async);
- }
- @Override
- public void statementExecute(final String sql, boolean async) {
- execute(() -> {
- try (Connection connection = getConnection();
- Statement statement = connection.createStatement()
- ) {
- statement.execute(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }, async);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement