@Override public void execute(AtomicInsertOperation operation) throws TransactionRolledbackException { Connection conn = null; try { conn = connections.get(); conn.setAutoCommit(false); Savepoint savepoint = conn.setSavepoint(); Insertion insertion = new InsertionImpl(conn); try { operation.execute(insertion); } catch (SQLException inner) { conn.rollback(savepoint); throw new TransactionRolledbackException(inner); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbc.close(conn); } }
@Override public void execute(AtomicInsertOperation operation) throws TransactionRolledbackException { Connection conn = null; try { conn = connections.get(); conn.setAutoCommit(false); Savepoint savepoint = conn.setSavepoint(); Insertion insertion = new InsertionImpl(conn); try { operation.execute(insertion); } catch (SQLException inner) { conn.rollback(savepoint); throw new TransactionRolledbackException(inner); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbc.close(conn); } }
@Override public void execute(AtomicInsertOperation operation) throws TransactionRolledbackException { Connection conn = null; try { conn = connections.get(); conn.setAutoCommit(false); Savepoint savepoint = conn.setSavepoint(); Insertion insertion = new InsertionImpl(conn); try { operation.execute(insertion); } catch (SQLException inner) { conn.rollback(savepoint); throw new TransactionRolledbackException(inner); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbc.close(conn); } }
@Override public <I extends Insertable> I executeUpdate(AtomicUpdateOperation<I> operation) throws TransactionRolledbackException { I entity = null; Connection conn = null; try { conn = connections.get(); conn.setAutoCommit(false); Savepoint savepoint = conn.setSavepoint(); UpdateImpl update = new UpdateImpl(conn); try { entity = operation.execute(update); } catch (SQLException inner) { conn.rollback(savepoint); throw new TransactionRolledbackException(inner); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbc.close(conn); } return entity; }
@Override public <I extends AbstractInsertable> I executeUpdate(AtomicUpdateOperation<I> operation) throws TransactionRolledbackException { I entity = null; Connection conn = null; try { conn = connections.get(); conn.setAutoCommit(false); Savepoint savepoint = conn.setSavepoint(); UpdateImpl update = new UpdateImpl(conn); try { entity = operation.execute(update); } catch (SQLException inner) { conn.rollback(savepoint); throw new TransactionRolledbackException(inner); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbc.close(conn); } return entity; }
private void update0(String sql, Iterator<? extends Updatable> entities, int batchSize) { try (Connection trx = connectionProvider.get(); PreparedStatement stmt = trx.prepareStatement(sql)) { trx.setAutoCommit(false); PreparedStatementWrapper wrapper = new PreparedStatementWrapper(stmt); while (entities.hasNext()) { Updatable entity = entities.next(); Update update = entity.getUpdate(); update.prepare(wrapper); wrapper.addBatch(); wrapper.executeBatchWhen(batchSize); } wrapper.executeBatch(); trx.commit(); } catch (SQLException e) { throw new SQLRuntimeException(e); } }
private void restoreSqlList(ConnectionProvider connectionProvider, Iterable<String> sqlList) { Connection connection = null; String currentSql = null; try { connection = connectionProvider.get(); connection.setAutoCommit(false); disableReferentialIntegrity(connection); for (String sql : sqlList) { currentSql = sql; execute(connection, sql); } enableReferentialIntegrity(connection); connection.commit(); } catch (SQLException e) { throw new SQLRuntimeException(currentSql, e); } finally { Jdbc.close(connection); } }
@Override public void insert(NativeSqlImpl sql) { logger.debug("insert <<:sql=\n{}", sql); Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = connections.get(); statement = sql.prepare(connection, Statement.RETURN_GENERATED_KEYS); statement.executeUpdate(); GeneratedKeyCallback keyCallback = sql.getKeyCallback(); if (keyCallback != null) { rs = statement.getGeneratedKeys(); keyCallback.set(rs); rs.close(); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(statement); Jdbc.close(rs); } }
private void restoreSqlList(ConnectionProvider connectionProvider, Iterable<String> sqlList) { Connection connection = null; String currentSql = null; try { connection = connectionProvider.get(); connection.setAutoCommit(false); disableReferentialIntegrity(connection); for (String sql : sqlList) { currentSql = sql; execute(connection, sql); } enableReferentialIntegrity(connection); connection.commit(); } catch (SQLException e) { throw new SQLRuntimeException(currentSql, e); } finally { Jdbc.close(connection); } }
@Override public int execute(NativeSqlImpl sql) { logger.debug("execute <<:sql={}", sql); int result = 0; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); result = stmt.executeUpdate(); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }
private void restoreSqlList(ConnectionProvider connectionProvider, Iterable<String> sqlList) { Connection connection = null; String currentSql = null; try { connection = connectionProvider.get(); connection.setAutoCommit(false); disableReferentialIntegrity(connection); for (String sql : sqlList) { currentSql = sql; execute(connection, sql); } enableReferentialIntegrity(connection); connection.commit(); } catch (SQLException e) { throw new SQLRuntimeException(currentSql, e); } finally { Jdbc.close(connection); } }
@Override public int execute(NativeSqlImpl sql) { logger.debug("execute <<:sql={}", sql); int result = 0; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); result = stmt.executeUpdate(); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }
@Override public int execute(NativeSqlImpl sql) { logger.debug("execute <<:sql={}", sql); int result = 0; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); result = stmt.executeUpdate(); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }
@Override public void insert(NativeSqlImpl sql) { logger.debug("insert <<:sql=\n{}", sql); Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = connections.get(); statement = sql.prepare(connection, Statement.RETURN_GENERATED_KEYS); statement.executeUpdate(); GeneratedKeyCallback keyCallback = sql.getKeyCallback(); if (keyCallback != null) { rs = statement.getGeneratedKeys(); keyCallback.set(rs); rs.close(); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(statement); Jdbc.close(rs); } }
@Override public void insert(NativeSqlImpl sql) { logger.debug("insert <<:sql=\n{}", sql); Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = connections.get(); statement = sql.prepare(connection, Statement.RETURN_GENERATED_KEYS); statement.executeUpdate(); GeneratedKeyCallback keyCallback = sql.getKeyCallback(); if (keyCallback != null) { rs = statement.getGeneratedKeys(); keyCallback.set(rs); rs.close(); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(statement); Jdbc.close(rs); } }
@SuppressWarnings("unchecked") @Override public <T> T single(NativeSqlImpl sql) { logger.debug("single <<:sql=\n{}", sql); T result = null; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); ResultSetLoader<?> loader = sql.getLoader(); rs = stmt.executeQuery(); result = (T) (rs.next() ? loader.load(rs) : null); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }
@SuppressWarnings("unchecked") @Override public <T> T single(NativeSqlImpl sql) { logger.debug("single <<:sql=\n{}", sql); T result = null; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); ResultSetLoader<?> loader = sql.getLoader(); rs = stmt.executeQuery(); result = (T) (rs.next() ? loader.load(rs) : null); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }
private void update0(Iterator<? extends AbstractUpdatable> entities, int batchSize) { AbstractUpdatable entity = entities.next(); AbstractUpdate<?> update = entity.getUpdate(); String sql = update.toString(); try (Connection trx = connectionProvider.get(); PreparedStatement stmt = trx.prepareStatement(sql)) { trx.setAutoCommit(false); PreparedStatementWrapper wrapper = new PreparedStatementWrapper(stmt); update.prepare(wrapper); wrapper.addBatch(); wrapper.executeBatchWhen(batchSize); while (entities.hasNext()) { entity = entities.next(); update = entity.getUpdate(); update.prepare(wrapper); wrapper.addBatch(); wrapper.executeBatchWhen(batchSize); } wrapper.executeBatch(); trx.commit(); } catch (SQLException e) { throw new SQLRuntimeException(e); } }
private void update0(Iterator<? extends AbstractUpdatable> entities, int batchSize) { AbstractUpdatable entity = entities.next(); AbstractUpdate<?> update = entity.getUpdate(); String sql = update.toString(); try (Connection trx = connectionProvider.get(); PreparedStatement stmt = trx.prepareStatement(sql)) { trx.setAutoCommit(false); PreparedStatementWrapper wrapper = new PreparedStatementWrapper(stmt); update.prepare(wrapper); wrapper.addBatch(); wrapper.executeBatchWhen(batchSize); while (entities.hasNext()) { entity = entities.next(); update = entity.getUpdate(); update.prepare(wrapper); wrapper.addBatch(); wrapper.executeBatchWhen(batchSize); } wrapper.executeBatch(); trx.commit(); } catch (SQLException e) { throw new SQLRuntimeException(e); } }
private <T> List<T> list0(NativeSqlImpl sql) { List<T> result = null; Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; try { connection = connections.get(); stmt = sql.prepare(connection); ResultSetLoader<?> loader = sql.getLoader(); rs = stmt.executeQuery(); Iterator<ResultSet> iterator = new ResultSetIterator(rs); Iterator<T> loaded = Iterators.transform(iterator, new LoaderFunction<T>(loader)); result = ImmutableList.copyOf(loaded); } catch (SQLException e) { String msg = sql != null ? sql.toString() : null; throw new SQLRuntimeException(msg, e); } finally { Jdbc.close(connection); Jdbc.close(stmt); Jdbc.close(rs); } return result; }