/** * Handle exception. * * @param exception to be handled exception * @throws SQLException SQL exception */ public static void handleException(final Exception exception) throws SQLException { if (isExceptionThrown()) { if (exception instanceof SQLException) { throw (SQLException) exception; } throw new ShardingException(exception); } log.error("exception occur: ", exception); } }
/** * Execute group. * * @param sqlExecuteGroups SQL execute groups * @param firstCallback first SQL execute callback * @param callback SQL execute callback * @param <T> class type of return value * @return execute result * @throws SQLException SQL exception */ @SuppressWarnings("unchecked") public <T> List<T> executeGroup(final Collection<ShardingExecuteGroup<? extends StatementExecuteUnit>> sqlExecuteGroups, final SQLExecuteCallback<T> firstCallback, final SQLExecuteCallback<T> callback) throws SQLException { try { return executeEngine.groupExecute((Collection) sqlExecuteGroups, firstCallback, callback, serial); } catch (final SQLException ex) { ExecutorExceptionHandler.handleException(ex); return Collections.emptyList(); } } }
private T execute0(final StatementExecuteUnit statementExecuteUnit, final boolean isTrunkThread, final Map<String, Object> shardingExecuteDataMap) throws SQLException { ExecutorExceptionHandler.setExceptionThrown(isExceptionThrown); DataSourceMetaData dataSourceMetaData = DataSourceMetaDataFactory.newInstance(databaseType, statementExecuteUnit.getDatabaseMetaData().getURL()); SQLExecutionHook sqlExecutionHook = new SPISQLExecutionHook(); try { sqlExecutionHook.start(statementExecuteUnit.getRouteUnit(), dataSourceMetaData, isTrunkThread, shardingExecuteDataMap); T result = executeSQL(statementExecuteUnit); sqlExecutionHook.finishSuccess(); return result; } catch (final SQLException ex) { sqlExecutionHook.finishFailure(ex); ExecutorExceptionHandler.handleException(ex); return null; } }
private boolean execute(final Executor executor) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<Boolean>(getDatabaseType(), isExceptionThrown) { @Override protected Boolean executeSQL(final StatementExecuteUnit statementExecuteUnit) throws SQLException { return executor.execute(statementExecuteUnit.getStatement(), statementExecuteUnit.getRouteUnit().getSqlUnit().getSql()); } }; List<Boolean> result = executeCallback(executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; } return result.get(0); }
/** * Execute query. * * @return result set list * @throws SQLException SQL exception */ public List<QueryResult> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<QueryResult> executeCallback = new SQLExecuteCallback<QueryResult>(getDatabaseType(), isExceptionThrown) { @Override protected QueryResult executeSQL(final StatementExecuteUnit statementExecuteUnit) throws SQLException { return getQueryResult(statementExecuteUnit); } }; return executeCallback(executeCallback); }
/** * Execute query. * * @return result set list * @throws SQLException SQL exception */ public List<QueryResult> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<QueryResult> executeCallback = new SQLExecuteCallback<QueryResult>(getDatabaseType(), isExceptionThrown) { @Override protected QueryResult executeSQL(final StatementExecuteUnit statementExecuteUnit) throws SQLException { return getQueryResult(statementExecuteUnit); } }; return executeCallback(executeCallback); }
/** * Execute SQL. * * @return return true if is DQL, false if is DML * @throws SQLException SQL exception */ public boolean execute() throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<Boolean> executeCallback = SQLExecuteCallbackFactory.getPreparedSQLExecuteCallback(getDatabaseType(), isExceptionThrown); List<Boolean> result = executeCallback(executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; } return result.get(0); } }
private int executeUpdate(final Updater updater) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(getDatabaseType(), isExceptionThrown) { @Override protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) throws SQLException { return updater.executeUpdate(statementExecuteUnit.getStatement(), statementExecuteUnit.getRouteUnit().getSqlUnit().getSql()); } }; List<Integer> results = executeCallback(executeCallback); if (isAccumulate()) { return accumulate(results); } else { return null == results.get(0) ? 0 : results.get(0); } }
/** * Execute batch. * * @return execute results * @throws SQLException SQL exception */ public int[] executeBatch() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<int[]> callback = new SQLExecuteCallback<int[]>(getDatabaseType(), isExceptionThrown) { @Override protected int[] executeSQL(final StatementExecuteUnit statementExecuteUnit) throws SQLException { return statementExecuteUnit.getStatement().executeBatch(); } }; List<int[]> results = executeCallback(callback); if (isAccumulate()) { return accumulate(results); } else { return results.get(0); } }
/** * Execute update. * * @return effected records count * @throws SQLException SQL exception */ public int executeUpdate() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecuteCallback<Integer> executeCallback = SQLExecuteCallbackFactory.getPreparedUpdateSQLExecuteCallback(getDatabaseType(), isExceptionThrown); List<Integer> results = executeCallback(executeCallback); if (isAccumulate()) { return accumulate(results); } else { return results.get(0); } }
@SuppressWarnings("unchecked") @Override public ExecuteResponse execute(final SQLRouteResult routeResult) throws SQLException { boolean isReturnGeneratedKeys = routeResult.getSqlStatement() instanceof InsertStatement; boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); Collection<ShardingExecuteGroup<StatementExecuteUnit>> sqlExecuteGroups = sqlExecutePrepareTemplate.getExecuteUnitGroups(routeResult.getRouteUnits(), new ProxyJDBCExecutePrepareCallback(isReturnGeneratedKeys)); SQLExecuteCallback<ExecuteResponseUnit> firstProxySQLExecuteCallback = new FirstProxyJDBCExecuteCallback(isExceptionThrown, isReturnGeneratedKeys); SQLExecuteCallback<ExecuteResponseUnit> proxySQLExecuteCallback = new ProxyJDBCExecuteCallback(isExceptionThrown, isReturnGeneratedKeys); Collection<ExecuteResponseUnit> executeResponseUnits = sqlExecuteTemplate.executeGroup((Collection) sqlExecuteGroups, firstProxySQLExecuteCallback, proxySQLExecuteCallback); ExecuteResponseUnit firstExecuteResponseUnit = executeResponseUnits.iterator().next(); return firstExecuteResponseUnit instanceof ExecuteQueryResponseUnit ? getExecuteQueryResponse(((ExecuteQueryResponseUnit) firstExecuteResponseUnit).getQueryResponsePackets(), executeResponseUnits) : new ExecuteUpdateResponse(executeResponseUnits); }
@SuppressWarnings("unchecked") @Override public ExecuteResponse execute(final SQLRouteResult routeResult) throws SQLException { boolean isReturnGeneratedKeys = routeResult.getSqlStatement() instanceof InsertStatement; boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); Collection<ShardingExecuteGroup<StatementExecuteUnit>> sqlExecuteGroups = sqlExecutePrepareTemplate.getExecuteUnitGroups(routeResult.getRouteUnits(), new ProxyJDBCExecutePrepareCallback(isReturnGeneratedKeys)); SQLExecuteCallback<ExecuteResponseUnit> firstProxySQLExecuteCallback = new FirstProxyJDBCExecuteCallback(isExceptionThrown, isReturnGeneratedKeys); SQLExecuteCallback<ExecuteResponseUnit> proxySQLExecuteCallback = new ProxyJDBCExecuteCallback(isExceptionThrown, isReturnGeneratedKeys); Collection<ExecuteResponseUnit> executeResponseUnits = sqlExecuteTemplate.executeGroup((Collection) sqlExecuteGroups, firstProxySQLExecuteCallback, proxySQLExecuteCallback); ExecuteResponseUnit firstExecuteResponseUnit = executeResponseUnits.iterator().next(); return firstExecuteResponseUnit instanceof ExecuteQueryResponseUnit ? getExecuteQueryResponse(((ExecuteQueryResponseUnit) firstExecuteResponseUnit).getPostgreSQLQueryResponsePackets(), executeResponseUnits) : new ExecuteUpdateResponse(executeResponseUnits); }