Serializable data members not rolled back
objectswitch Oct 28, 2008 4:35 PMI would like to understand if POJO cache supports transactional management for all objects referenced from fields of a @Replicable object.
I find that if a field references an instance of a class also annotated @Replicable, things work as I expect. However, if the field references just a serializable class, that object is not managed.
For example:
package com.example; import java.io.Serializable; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.transaction.UserTransaction; import org.jboss.cache.pojo.PojoCache; import org.jboss.cache.pojo.PojoCacheFactory; import org.jboss.cache.pojo.annotation.Replicable; import org.jboss.cache.pojo.annotation.Transient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Transaction { final static String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; final static String ID = "example/Pojo"; @Replicable public static class Pojo { public int primitiveField = 0; public StringBuilder referenceField = new StringBuilder("0"); public String toString() { return "primitiveField:" + primitiveField + ", " + "referenceField:" + referenceField; } } public static void main(String[] args) { System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); PojoCache pojoCache = PojoCacheFactory.createCache( "resources/cache-service.xml", false); pojoCache.start(); Pojo pojo = new Pojo(); pojoCache.attach(ID, pojo); Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY); try { UserTransaction tx = (UserTransaction) new InitialContext(prop) .lookup("UserTransaction"); System.out.println("\n[POJO initial values ] " + pojo); tx.begin(); pojo.primitiveField = 1; pojo.referenceField.setCharAt(0,'1'); tx.commit(); System.out.println("[POJO commited values] " + pojo); tx.begin(); pojo.primitiveField = 2; pojo.referenceField.setCharAt(0,'2'); tx.rollback(); System.out.println("[POJO aborted values ] " + pojo + "\n"); } catch (Exception e) { System.out.println(e.getStackTrace()); } pojoCache.stop(); } }
Running this yields:
[POJO initial values ] primitiveField:0, referenceField:0, [POJO commited values] primitiveField:1, referenceField:1, [POJO aborted values ] primitiveField:1, referenceField:2,
What I expected is that the StringBuilder object would also be restored.
Re-fetching the object from the cache after the rollback didn't help; the cache definitely appears to have the value assigned in the aborted transaction.
If this is the intended behavior, what if the general strategy for using PojoCache with library classes? Do users need to selectively instrument them via configuration?