use of com.ctrip.platform.dal.dao.DalCommand 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