Java Transactions API (JTA) とその実装の 1 つである Bitronix の下にある値に頭を悩ませようとしています。しかし、ドキュメンテーションを深く掘り下げていくと、次の簡単な例を考えずにはいられません。
public interface Transactional {
public void commit(Object);
public void rollback();
}
public class TransactionalFileWriter extends FileWriter implements Transactional {
@Override
public void commit(Object obj) {
String str = (String)obj;
// Write the String to a file.
write(str);
}
@Override
public void rollback() {
// Obtain a handler to the File we are writing to, and delete the file.
// This returns the file system to the state it was in before we created a file and started writing to it.
File f = getFile();
// This is just pseudo-code for the sake of this example.
File.delete(f);
}
}
// Some method in a class somewhere...
public void doSomething(File someFile) {
TransactionalFileWriter txFileWriter = getTxFW(someFile);
try {
txFileWriter.commit("Create the file and write this message to it.");
} catch(Throwable t) {
txFileWriter.rollback();
}
}
上記の実際のコードにとらわれすぎないでください。アイデアは単純です。ファイルを作成して書き込むトランザクション ファイル ライターです。このrollback()
メソッドはファイルを削除するため、ファイル システムはcommit(Object)
.
ここで何か不足していますか?JTA が提供するのはこれだけですか? それとも、上記の単純な例では表されていない、トランザクションに対する次元/側面のまったく異なるセットがありますか? 私は後者を推測していますが、JTA ドキュメントにはまだ具体的なものは何もありません。何かが足りない場合、それは何ですか?具体的な例を教えてもらえますか? トランザクション性が JDBC の巨大なコンポーネントであることは理解できますが、うまくいけば、データベース以外のもので動作する JTA の例を取得したいと考えています。