TreeCache のトランザクションの働きを見てみましょう。 セットアップの手順は先ほどのセクションとほぼ同じです。 違いは、bsh のスクリプトaop.bshを読み込む代わりにaopWithTx.bshを読み込むことだけです。以下はそのスクリプトの抜粋です。
mport org.jboss.cache.PropertyConfigurator; import org.jboss.cache.aop.PojoCache; import org.jboss.cache.aop.test.Person; import org.jboss.cache.aop.test.Address;// Txインポート import javax.transaction.UserTransaction; import javax.naming.*; import org.jboss.cache.transaction.DummyTransactionManager; show(); // bean shellをverboseモードにします。l // トランザクションマネージャをセットアップします DummyTransactionManager.getInstance(); Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); UserTransaction tx = (UserTransaction)new InitialContext(prop).lookup("UserTransaction"); PojoCache tree = new PojoCache(); PropertyConfigurator config = new PropertyConfigurator(); // configure tree cache. config.configure(tree, "META-INF/replSync-service.xml"); joe = new Person(); joe.setName("Joe Black"); joe.setAge(31); Address addr = new Address(); addr.setCity("Sunnyvale"); addr.setStreet("123 Albert Ave"); addr.setZip(94086); joe.setAddress(addr); tree.startService(); // tree cacheの起動 tree.putObject("/aop/joe", joe); // aop 適用オブジェクト追加 // aop適用されたので、get/set メソッドの呼び出しが自動的にキャッシュの内容に反映されます。 // トランザクションにも対応しています。 tx.begin(); joe.setAge(41); joe.getAddress().setZip(95124); tx.commit();
この例では、デフォルトの default dummy transaction managerが使用されています。
tx.begin(); addr.setZip(95131); tx.rollback();