-
Notifications
You must be signed in to change notification settings - Fork 0
Java客户端 事务处理
对于需要事物支持的包含复杂逻辑的数据库操作,可以包装在DalCommand接口里,然后调用DalClient接口提供的支持事物的方法
DalClient的execute方法使用事务来处理传入的DalCommand。用户需要实现DalCommand接口,并且将需要放在一个事务里面 的逻辑包含在里面。 同时还支持传入DalCommand list来实现事务逻辑的合理划分和聚合。list中的所有DalCommand都将处于同一个事务里面
如果在单个command的执行中没有任何的exception出现,则会自动提交修改。否则回滚。对支持command list的处理也是一样,任何一个command出现excpetion,之前执行的command连同当前的都会回滚。否则全部提交。
每个command会有一个返回值,该值仅仅在调研command list的时候有判读作用。为true则继续调用当前command后面的command。为false则结束提交当前的transaction,并忽略后面的剩余的command。
command里面既可以调用给定的DalClient,也可以调用已经生成的dao中的任意方法,前提是保证访问的是同一个数据库的表。
例子。单个command直接使用DalClient
DalClient client = DalClientFactory.getClient("dao_test");
DalCommand command = new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String delete = "delete from Person where id > 2000";
String insert = "insert into Person values(NULL, 'bbb', 100, 'aaaaa', 100, 1, '2012-05-01 10:10:00',1)";
String update = "update Person set name='abcde' where id > 2000";
String[] sqls = new String[]{delete, insert, insert, insert, update};
System.out.println(client.batchUpdate(sqls, hints));
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints();
client.update(delete, parameters, hints);
selectPerson(client);
return true;
}
};
DalHints hints = new DalHints();
client.execute(command, hints);
多command方式 DalClient client = DalClientFactory.getClient("dao_test"); List cmds = new LinkedList(); cmds.add(new DalCommand() { @Override public boolean execute(DalClient client) throws SQLException { String delete = "delete from Person where id > 2000"; String insert = "insert into Person values(NULL, 'bbb', 100, 'aaaaa', 100, 1, '2012-05-01 10:10:00')"; String update = "update Person set name='abcde' where id > 2000"; String[] sqls = new String[]{insert, insert, insert, update}; System.out.println(client.batchUpdate(sqls, hints)); StatementParameters parameters = new StatementParameters(); DalHints hints = new DalHints(); client.update(delete, parameters, hints); selectPerson(client); return true; } });
cmds.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
selectPerson(client);
return false; // 返回false,则后面所有的command都不会被执行,当前事物直接commit
}
});
cmds.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
selectPerson(client);
return true;
}
});
DalHints hints = new DalHints();
client.execute(cmds, hints);
DalCommand的execute方法缺省提供一个DalClient以供使用,但这不意味着该方法里面只能使用DalClient。只要是针对同一个逻辑数据库的操作,execute里面可以调用任意的基于同一个逻辑数据库的DAO。
例如: DalClient client = DalClientFactory.getClient(DATA_BASE); private CloggingAppGenDao dao = MysqlDaoFactory.getCloggingAppGenDao();
public AppGenDeleteAndInsertDao(){
}
public void DeleteAndInsert(final CloggingAppGen gen){
DalCommand command = new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
dao.delete(gen);
test();
dao.insert(gen);
return true;
}
};
try {
client.execute(command, hints);
} catch (SQLException e) {
e.printStackTrace();
}
}