use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalRequestExecutor method internalExecute.
private <T> T internalExecute(DalHints hints, DalRequest<T> request, boolean nullable, final ShardExecutionCallback<T> callback) throws SQLException {
T result = null;
Throwable error = null;
LogContext logContext = logger.start(request);
long startTime = System.currentTimeMillis();
try {
request.validateAndPrepare();
if (request.isCrossShard())
result = crossShardExecute(logContext, hints, request, callback);
else
result = nonCrossShardExecute(logContext, hints, request, callback);
if (result == null && !nullable)
throw new DalException(ErrorCode.AssertNull);
request.endExecution();
} catch (Throwable e) {
error = e;
}
logContext.setDaoExecuteTime(System.currentTimeMillis() - startTime);
logger.end(logContext, error);
handleCallback(hints, result, error);
if (error != null)
throw DalException.wrap(error);
return result;
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class SingleUpdateTask method execute.
@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
if (fields.size() == 0)
throw new DalException(ErrorCode.ValidateFieldCount);
Map<String, ?> pks = getPrimaryKeys(fields);
Object version = getVersion(fields);
Map<String, ?> filted = null;
if (rawPojo instanceof UpdatableEntity)
filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
else
filted = filterNullColumns(hints, fields);
// If there is no columns changed, we will not perform update
if (filted.size() == 0)
return 0;
String tableName = getRawTableName(hints, fields);
if (taskContext instanceof DalContextConfigure)
((DalContextConfigure) taskContext).addTables(tableName);
String updateSql = buildUpdateSql(quote(tableName), filted, hints);
StatementParameters parameters = new StatementParameters();
addParameters(parameters, filted);
addParameters(parameters, pks);
addVersion(parameters, version);
if (client instanceof DalContextClient)
return ((DalContextClient) client).update(updateSql, parameters, hints.setFields(fields), taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class ConnectionActionTest method testCleanupCloseConnection.
@Test
public void testCleanupCloseConnection() {
SQLException e1 = new SQLException("test discard", "1234");
e1.setNextException(new SQLException("test discard", "08006"));
SQLException e2 = new SQLException("test discard", "1234");
e2.setNextException(new SQLException("test discard", "08S01"));
Exception[] el = new Exception[] { // Case 1 detect direct SQLException
new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
new RuntimeException("test discard", e1), // Case 1 detect direct SQLException
new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
new RuntimeException("test discard", e2) };
for (Exception e : el) {
try {
TestConnectionAction test = new TestConnectionAction();
DalConnection connHolder = getDalConnection();
test.connHolder = connHolder;
test.statement = test.connHolder.getConn().createStatement();
test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
test.rs.next();
PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
connHolder.error(e);
test.cleanup();
// Connection discarding moved into datasource level
/* assertTrue(c.isDiscarded());
assertTrue(c.isReleased());*/
assertNotNull(test);
assertTrue(test.conn == null);
assertTrue(test.statement == null);
assertTrue(test.rs == null);
assertTrue(test.connHolder == null);
} catch (Exception ex) {
ex.printStackTrace();
fail("There should be no exception here");
}
}
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalDirectClient method call.
@Override
public Map<String, ?> call(String callString, StatementParameters parameters, final DalHints hints, final DalTaskContext dalTaskContext) throws SQLException {
ConnectionAction<Map<String, ?>> action = new ConnectionAction<Map<String, ?>>() {
@Override
public Map<String, ?> execute() throws Exception {
List<StatementParameter> resultParameters = new ArrayList<StatementParameter>();
List<StatementParameter> callParameters = new ArrayList<StatementParameter>();
resultParameters.addAll(parameters.getResultParameters());
for (StatementParameter parameter : parameters.values()) {
if (parameter.isOutParameter()) {
callParameters.add(parameter);
}
}
if (hints.is(DalHintEnum.retrieveAllSpResults) && resultParameters.size() > 0)
throw new DalException("Dal hint 'autoRetrieveAllResults' should only be used when there is no special result parameter specified");
conn = getConnection(hints, this);
callableStatement = createCallableStatement(conn, callString, parameters, hints);
beginExecute();
boolean retVal = executeCall(callableStatement, entry);
int updateCount = callableStatement.getUpdateCount();
endExecute();
Map<String, Object> returnedResults = new LinkedHashMap<String, Object>();
if (retVal || updateCount != -1) {
returnedResults.putAll(extractReturnedResults(callableStatement, resultParameters, updateCount, hints));
}
returnedResults.putAll(extractOutputParameters(callableStatement, callParameters));
return returnedResults;
}
};
action.populateSp(callString, parameters, dalTaskContext);
return doInConnection(action, hints);
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalTransaction method endTransaction.
public void endTransaction(int startLevel) throws SQLException {
if (rolledBack || completed)
throw new DalException(ErrorCode.TransactionState);
if (startLevel != (level - 1)) {
rollback();
throw new DalException(ErrorCode.TransactionLevelMatch, (level - 1), startLevel);
}
if (level > FIRST_LEVEL) {
setTransactionStatusOnCommit();
level--;
return;
}
if (status == DalTransactionStatus.Rollback || status == DalTransactionStatus.Conflict) {
setTransactionStatusOnCommit();
rollbackIfNeeded();
throwExceptionOnCommitConflicted();
}
try {
processRollbackOnlyIfExist();
} catch (Throwable e) {
throw new DalException(e.getMessage(), e);
}
commitIfNeeded();
}
Aggregations