Chapter 8. PojoCache with Transaction
To see TreeCache transaction at work, you start with the same setup with last section except you load the bsh of aopWithTx.bsh instead of aop.bsh. The additional snippets are:
import 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 imports
import javax.transaction.UserTransaction; import javax.naming.*;
import org.jboss.cache.transaction.DummyTransactionManager;
show(); // verbose mode for bean shell
// Set up transaction manager
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(); // kick start tree cache
tree.putObject("/aop/joe", joe); // add aop sanctioned object
// since it is aop-sanctioned, use of plain get/set methods will take care
of cache contents automatically.
// Also it is transacted
tx.begin();
joe.setAge(41);
joe.getAddress().setZip(95124);
tx.commit();
In this example, a default dummy transaction manager is used.
tx.begin();
addr.setZip(95131);
tx.rollback();