Search in sources :

Example 1 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class StringUtils method inputStream2String.

/**
 * Input stream 2 string string.
 *
 * @param is the is
 * @return the string
 */
public static String inputStream2String(InputStream is) {
    if (is == null) {
        return null;
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString(Constants.DEFAULT_CHARSET_NAME);
    } catch (Exception e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 2 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class StringUtils method inputStream2Bytes.

/**
 * Input stream to byte array
 *
 * @param is the is
 * @return the byte array
 */
public static byte[] inputStream2Bytes(InputStream is) {
    if (is == null) {
        return null;
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toByteArray();
    } catch (Exception e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 3 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class ConfigFuture method get.

/**
 * Get object.
 *
 * @param timeout the timeout
 * @param unit    the unit
 * @return the object
 */
public Object get(long timeout, TimeUnit unit) {
    this.timeoutMills = unit.toMillis(timeout);
    Object result;
    try {
        result = origin.get(timeout, unit);
    } catch (ExecutionException e) {
        throw new ShouldNeverHappenException("Should not get results in a multi-threaded environment", e);
    } catch (TimeoutException e) {
        LOGGER.error("config operation timeout,cost:{} ms,op:{},dataId:{}", System.currentTimeMillis() - start, operation.name(), dataId);
        return getFailResult();
    } catch (InterruptedException exx) {
        LOGGER.error("config operate interrupted,error:{}", exx.getMessage(), exx);
        return getFailResult();
    }
    if (operation == ConfigOperation.GET) {
        return result == null ? content : result;
    } else {
        return result == null ? Boolean.FALSE : result;
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class PostgresqlUndoInsertExecutor method buildUndoSQL.

@Override
protected String buildUndoSQL() {
    TableRecords afterImage = sqlUndoLog.getAfterImage();
    List<Row> afterImageRows = afterImage.getRows();
    if (CollectionUtils.isEmpty(afterImageRows)) {
        throw new ShouldNeverHappenException("Invalid UNDO LOG");
    }
    return generateDeleteSql(afterImageRows, afterImage);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Row(io.seata.rm.datasource.sql.struct.Row)

Example 5 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class TCCResourceManager method branchCommit.

/**
 * TCC branch commit
 *
 * @param branchType
 * @param xid             Transaction id.
 * @param branchId        Branch id.
 * @param resourceId      Resource id.
 * @param applicationData Application data bind with this branch.
 * @return BranchStatus
 * @throws TransactionException TransactionException
 */
@Override
public BranchStatus branchCommit(BranchType branchType, String xid, long branchId, String resourceId, String applicationData) throws TransactionException {
    TCCResource tccResource = (TCCResource) tccResourceCache.get(resourceId);
    if (tccResource == null) {
        throw new ShouldNeverHappenException(String.format("TCC resource is not exist, resourceId: %s", resourceId));
    }
    Object targetTCCBean = tccResource.getTargetBean();
    Method commitMethod = tccResource.getCommitMethod();
    if (targetTCCBean == null || commitMethod == null) {
        throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
    }
    try {
        // BusinessActionContext
        BusinessActionContext businessActionContext = getBusinessActionContext(xid, branchId, resourceId, applicationData);
        Object ret = commitMethod.invoke(targetTCCBean, businessActionContext);
        LOGGER.info("TCC resource commit result : {}, xid: {}, branchId: {}, resourceId: {}", ret, xid, branchId, resourceId);
        boolean result;
        if (ret != null) {
            if (ret instanceof TwoPhaseResult) {
                result = ((TwoPhaseResult) ret).isSuccess();
            } else {
                result = (boolean) ret;
            }
        } else {
            result = true;
        }
        return result ? BranchStatus.PhaseTwo_Committed : BranchStatus.PhaseTwo_CommitFailed_Retryable;
    } catch (Throwable t) {
        String msg = String.format("commit TCC resource error, resourceId: %s, xid: %s.", resourceId, xid);
        LOGGER.error(msg, t);
        return BranchStatus.PhaseTwo_CommitFailed_Retryable;
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Method(java.lang.reflect.Method) BusinessActionContext(io.seata.rm.tcc.api.BusinessActionContext)

Aggregations

ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)40 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)11 Row (io.seata.rm.datasource.sql.struct.Row)9 ArrayList (java.util.ArrayList)9 List (java.util.List)8 TransactionException (io.seata.core.exception.TransactionException)7 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)7 CollectionUtils (io.seata.common.util.CollectionUtils)6 ColumnUtils (io.seata.rm.datasource.ColumnUtils)6 Field (io.seata.rm.datasource.sql.struct.Field)6 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)6 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)6 Collectors (java.util.stream.Collectors)6 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)4 Method (java.lang.reflect.Method)4 ResultSet (java.sql.ResultSet)4 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 IndexMeta (io.seata.rm.datasource.sql.struct.IndexMeta)3 HashMap (java.util.HashMap)3