Index: test1197/Base.java =================================================================== RCS file: test1197/Base.java diff -N test1197/Base.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/Base.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,16 @@ +package test1197; + +public class Base { + private String id; + private String color; + + public String getId() { return id; } + public void setId(String id) { this.id = id; } + + public String getColor() { return color; } + public void setColor(String color) { this.color = color; } + + public String toString(){ + return super.toString() + " Base: id=" + id + ",color=" + color; + } +} Index: test1197/Container.java =================================================================== RCS file: test1197/Container.java diff -N test1197/Container.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/Container.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,16 @@ +package test1197; + +public class Container { + private String id; + private Base reference; + + public String getId() { return id; } + public void setId(String id) { this.id = id; } + + public Base getReference() { return reference; } + public void setReference(Base reference) { this.reference = reference; } + + public String toString(){ + return super.toString() + "Container: id =" + id + ",reference=[" + reference + "]"; + } +} Index: test1197/Derived.java =================================================================== RCS file: test1197/Derived.java diff -N test1197/Derived.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/Derived.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,12 @@ +package test1197; + +public class Derived extends Base { + private String scent; + + public String getScent() { return scent; } + public void setScent(String scent) { this.scent = scent; } + + public String toString(){ + return super.toString() + ", Derived: scent=" + scent; + } +} Index: test1197/Test1197.java =================================================================== RCS file: test1197/Test1197.java diff -N test1197/Test1197.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/Test1197.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,201 @@ +package test1197; + +import junit.framework.TestCase; + +import org.exolab.castor.jdo.Database; +import org.exolab.castor.jdo.JDO2; +import org.exolab.castor.jdo.OQLQuery; +import org.exolab.castor.jdo.QueryResults; +import org.exolab.castor.mapping.Mapping; + +public class Test1197 extends TestCase { + private static final String JdoConfFile = "jdo-conf.xml"; + private static final String MappingFile = "mapping.xml"; + private static final int REPS = 10000; + + private Mapping mapping; + private JDO2 jdo; + + public static void main(String[] args) { + junit.textui.TestRunner.run( Test1197.class ); + } + + protected void setUp() throws Exception { + super.setUp(); + + mapping = new Mapping( getClass().getClassLoader() ); + mapping.loadMapping( getClass().getResource( MappingFile ) ); + + JDO2.loadConfiguration( getClass().getResource( JdoConfFile ).toString() ); + jdo = JDO2.createInstance( "test" ); + + Database db = jdo.getDatabase(); + + db.begin(); + OQLQuery query = db.getOQLQuery( "select c from " + Container.class.getName() + " c" ); + QueryResults results = query.execute(); + while (results.hasMore()) { db.remove(results.next()); } + db.commit(); + + db.begin(); + query = db.getOQLQuery( "select d from " + Derived.class.getName() + " d" ); + results = query.execute(); + while (results.hasMore()) { db.remove(results.next()); } + db.commit(); + + db.begin(); + query = db.getOQLQuery( "select d from " + Base.class.getName() + " d" ); + results = query.execute(); + while (results.hasMore()) { db.remove(results.next()); } + db.commit(); + + db.begin(); + try{ + Derived d = new Derived(); + d.setId( "100" ); + d.setColor( "blue" ); + d.setScent( "parsimmon" ); + db.create( d ); + + Container c = new Container(); + c.setId( "200" ); + c.setReference( d ); + db.create( c ); + + db.commit(); + } catch ( Exception ex ){ + ex.printStackTrace(); + db.rollback(); + throw ex; + } + + db.close(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private Container loadContainer() throws Exception{ + Database db = jdo.getDatabase(); + db.begin(); + Container container; + try { + container = (Container) db.load( Container.class, "200" ); + db.commit(); + } catch ( Exception ex ) { + db.rollback(); + throw ex; + } finally { + db.close(); + } + return container; + } + + private Derived loadDerived() throws Exception{ + Database db = jdo.getDatabase(); + db.begin(); + Derived derived; + try { + derived = (Derived) db.load( Derived.class, "100" ); + db.commit(); + } catch ( Exception ex ) { + db.rollback(); + throw ex; + } finally { + db.close(); + } + return derived; + } + + private Container loadContainerThenDerived() throws Exception { + Database db = jdo.getDatabase(); + db.begin(); + Container container = null; + try { + container = (Container) db.load( Container.class, "200" ); + Derived derived = (Derived) db.load( Derived.class, "100" ); + db.commit(); + } catch ( Exception ex ) { + db.rollback(); + throw ex; + } finally { + db.close(); + } + return container; + } + + public void testLoadContainer() { + try { + System.out.println( "First we load a Container in its own transaction" ); + System.out.flush(); + Object o = loadContainer(); + System.out.println( "loadContainer: " + o ); + } catch ( Exception ex ) { + ex.printStackTrace(); + } + } + + public void testLoadDerived() { + try { + System.out.println( "Second we load a Derived in its own transaction" ); + System.out.flush(); + Object o = loadDerived(); + System.out.println( "loadDerived: " + o ); + } catch ( Exception ex ) { + ex.printStackTrace(); + } + } + + public void testLoadContainerThenDerived() { + try { + System.out.println( "Third we load a Container and a Derived in one transaction" ); + System.out.flush(); + Object o = loadContainerThenDerived(); + System.out.println( "loadContainerThenDerived: " + o); + } catch ( Exception ex ) { + ex.printStackTrace(); + } + } + + public void testLoadContainerAndDerivedThreaded() { + int count = 0; + try { + System.out.println( "Forth we load Container and Derived in seperate " + + "threads in their own transactions" ); + System.out.flush(); + + Thread thread = new Thread ( new TreadedContainerLoader() ); + thread.start(); + + for ( int i = 0; i < REPS; i++ ) { + count = i; + loadDerived(); + } + System.out.println( "First thread successfully loaded " + (count + 1) + " Derived" ); + + thread.join(); + } catch ( Exception ex ) { + System.err.println( "Exception on first thread loading Derived on " + (count + 1) + "th try" ); + System.out.flush(); + ex.printStackTrace(); + } + } + + class TreadedContainerLoader implements Runnable { + public void run() { + int count = 0; + try { + for ( int i = 0; i < REPS; i++ ) { + count = i; + loadContainer(); + } + System.out.println( "Second thread successfully loaded " + (count + 1) + " Containers" ); + } catch ( Exception ex ) { + System.err.println( "Exception on second thread loading Container on " + (count + 1) + "th try" ); + System.out.flush(); + ex.printStackTrace(); + } + } + } +} Index: test1197/ddl.sql =================================================================== RCS file: test1197/ddl.sql diff -N test1197/ddl.sql --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/ddl.sql 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,34 @@ +/* +* Two objects, a Derived and a Container. +* Since Derived derives from Base there is a row in Base too. +* The Container id = 200 +* The Derived id = 100 +*/ +DROP TABLE IF EXISTS base; +CREATE TABLE base ( + id varchar(64) NOT NULL default '', + color varchar(64) default NULL, + PRIMARY KEY (ID) +) ; + +INSERT INTO base VALUES ('100','red'); + +DROP TABLE IF EXISTS derived; +CREATE TABLE derived ( + id varchar(64) NOT NULL default '', + scent varchar(64) default NULL, + PRIMARY KEY (ID) +) ; +INSERT INTO derived VALUES ('100','vanilla'); + + +DROP TABLE IF EXISTS container; +CREATE TABLE container ( + id varchar(64) NOT NULL default '', + reference varchar(64) default NULL, + PRIMARY KEY (ID) +) ; +INSERT INTO container VALUES ('200','100'); + + + Index: test1197/jdo-conf.xml =================================================================== RCS file: test1197/jdo-conf.xml diff -N test1197/jdo-conf.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/jdo-conf.xml 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + Index: test1197/mapping.xml =================================================================== RCS file: test1197/mapping.xml diff -N test1197/mapping.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test1197/mapping.xml 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +