use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.
the class DalDirectClientTestStub method executeTestWithMultipleCommandsNotAllSuccessed.
/**
* Test execute multiple commands and not all successes.
* If one failed, the after commands will be dropped.
* @throws SQLException
*/
@Test
public void executeTestWithMultipleCommandsNotAllSuccessed() throws SQLException {
final DalHints hints = new DalHints();
List<DalCommand> commands = new ArrayList<DalCommand>();
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
StatementParameters parameters = new StatementParameters();
client.update(sql, parameters, hints);
return false;
}
});
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 100";
StatementParameters parameters = new StatementParameters();
return client.update(sql, parameters, hints) == 1;
}
});
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
StatementParameters parameters = new StatementParameters();
return client.update(sql, parameters, hints) == 1;
}
});
client.execute(commands, hints);
List<ClientTestModel> models = this.queryModelsByIds();
Assert.assertEquals(2, models.size());
}
use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.
the class DalConcurrentMysqlTest method testExecute.
@Test
public void testExecute() throws SQLException {
List<DalCommand> commands = new ArrayList<DalCommand>();
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "INSERT INTO " + TABLE_NAME + "(quantity,type,address)" + " VALUES(10, 1, 'SH INFO')";
int ret = client.update(sql, new StatementParameters(), new DalHints());
if (ret > 0) {
System.out.println("insert success.");
} else {
System.out.println("insert failed.");
}
return ret > 0;
}
});
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME + " WHERE address = '" + "SH INFO'";
int ret = client.update(sql, new StatementParameters(), new DalHints());
if (ret > 0) {
System.out.println("delete success");
} else {
System.out.println("delete failed");
}
return ret > 0;
}
});
client.execute(commands, new DalHints());
String sql = "SELECT count(1) from " + TABLE_NAME;
Number count = (Number) client.query(sql, new StatementParameters(), new DalHints(), new DalScalarExtractor());
Assert.assertEquals(INSERT_COUNT, count.intValue());
}
use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.
the class DalDirectClient method execute.
@Override
public void execute(final List<DalCommand> commands, final DalHints hints) throws SQLException {
final DalClient client = this;
ConnectionAction<?> action = new ConnectionAction<Object>() {
@Override
public Object execute() throws Exception {
for (DalCommand cmd : commands) {
if (!cmd.execute(client))
break;
}
return null;
}
};
action.populate(commands);
doInTransaction(action, hints);
}
use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.
the class ManualMarkDownTest method logicDbMarkdownTest.
@Test
public void logicDbMarkdownTest() {
String logicDb = "dao_test_sqlsvr_tableShard_simple";
DalStatusManager.getDatabaseSetStatus(logicDb).setMarkdown(true);
DalClient client = DalClientFactory.getClient(logicDb);
try {
client.query("select 1", new StatementParameters(), new DalHints(), new DalScalarExtractor());
Assert.fail();
} catch (SQLException e) {
}
DalStatusManager.getDatabaseSetStatus(logicDb).setMarkdown(false);
try {
client.query("select 1", new StatementParameters(), new DalHints(), new DalScalarExtractor());
} catch (SQLException e) {
Assert.fail();
}
}
use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.
the class DalTransactionManagerTest method testCommitListeners.
@Test
public void testCommitListeners() {
final DalHints hints = new DalHints();
final DalTransactionListener testListener = new DalTransactionListener() {
@Override
public void beforeCommit() throws SQLException {
Assert.assertTrue(DalTransactionManager.isInTransaction());
DalClient c = DalClientFactory.getClient(DalTransactionManager.getLogicDbName());
c.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Object>() {
@Override
public Object extract(ResultSet rs) throws SQLException {
return null;
}
});
}
@Override
public void beforeRollback() {
fail();
}
@Override
public void afterCommit() {
Assert.assertFalse(DalTransactionManager.isInTransaction());
}
@Override
public void afterRollback() {
fail();
}
};
final DalTransactionListener testListener1 = new DalTransactionListener() {
@Override
public void beforeCommit() throws SQLException {
Assert.assertTrue(DalTransactionManager.isInTransaction());
DalCommand cmd = new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
client.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Object>() {
@Override
public Object extract(ResultSet rs) throws SQLException {
return null;
}
});
return false;
}
};
DalClientFactory.getClient(DalTransactionManager.getLogicDbName()).execute(cmd, new DalHints());
}
@Override
public void beforeRollback() {
fail();
}
@Override
public void afterCommit() {
Assert.assertFalse(DalTransactionManager.isInTransaction());
}
@Override
public void afterRollback() {
fail();
}
};
try {
final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
ConnectionAction<?> action = new ConnectionAction<Object>() {
public Object execute() throws Exception {
DalTransactionManager.register(testListener);
DalTransactionManager.register(testListener1);
return null;
}
};
action.operation = DalEventEnum.EXECUTE;
test.doInTransaction(action, hints);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
Aggregations