Skip to content

Instantly share code, notes, and snippets.

@bufferings
Last active May 29, 2019 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bufferings/5145468 to your computer and use it in GitHub Desktop.
Save bufferings/5145468 to your computer and use it in GitHub Desktop.
http://d.hatena.ne.jp/taedium/20100314/p2 を読んで。自分だったらどうかくかなーと思って。こんな感じかな。のメモ。
Integer result = tx(new UnitWork<Integer>() {
@Override
public Integer run() {
Employee employee = dao.selectById(1);
employee.setName("hoge");
employee.setJobType(JobType.PRESIDENT);
return dao.update(employee);
}
});
public class TxHandler {
public interface UnitWork<S> {
S run();
}
public static <T> T tx(UnitWork<T> unitWork) {
LocalTransaction tx = AppConfig.getLocalTransaction();
try{
tx.begin();
T result = unitWork.run();
tx.commit();
return result;
} finally {
tx.rollback();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment