db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an...@apache.org
Subject svn commit: r581220 - in /db/jdo/site/xdocs/tutorials: ./ replication.xml
Date Tue, 02 Oct 2007 13:02:49 GMT
Author: andyj
Date: Tue Oct  2 06:02:49 2007
New Revision: 581220

URL: http://svn.apache.org/viewvc?rev=581220&view=rev
Log:
JDO-537 Upgrade site to use Maven1 "site". Start process of merging in JPOX JDO docs

Added:
    db/jdo/site/xdocs/tutorials/
    db/jdo/site/xdocs/tutorials/replication.xml

Added: db/jdo/site/xdocs/tutorials/replication.xml
URL: http://svn.apache.org/viewvc/db/jdo/site/xdocs/tutorials/replication.xml?rev=581220&view=auto
==============================================================================
--- db/jdo/site/xdocs/tutorials/replication.xml (added)
+++ db/jdo/site/xdocs/tutorials/replication.xml Tue Oct  2 06:02:49 2007
@@ -0,0 +1,148 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<document>
+    <properties>
+        <title>Tutorial : Datastore Replication</title>
+    </properties>
+
+    <body>
+        <section name="Data Replication">
+            <p>
+                There are times when you need to replicate data between datastores. In many
cases datastores themselves provide a means of doing this, however if you want to avoid using
datastore-specific functionality you can utilise JDO to perform this task.
+                JDO2 allows replication by use of detach/attach functionality.
+                We demonstrate this with an example
+            </p>
+            <source>
+public class ElementHolder
+{
+    long id;
+    private Set elements = new HashSet();
+
+    ...
+}
+
+public class Element
+{
+    String name;
+
+    ...
+}
+
+public class SubElement extends Element
+{
+    double value;
+
+    ...
+}</source>
+            <p>
+                so we have a 1-N unidirectional (Set) relation, and we define the metadata
like this
+            </p>
+            <source><![CDATA[
+<jdo>
+    <package name="org.apache.jdo.test">
+        <class name="ElementHolder" identity-type="application" detachable="true">
+            <inheritance strategy="new-table"/>
+            <field name="id" primary-key="true"/>
+            <field name="elements" persistence-modifier="persistent">
+                <collection element-type="Element"/>
+                <join/>
+            </field>
+        </class>
+
+        <class name="Element" identity-type="application" detachable="true">
+            <inheritance strategy="new-table"/>
+            <field name="name" primary-key="true"/>
+        </class>
+
+        <class name="SubElement">
+            <inheritance strategy="new-table"/>
+            <field name="value"/> 
+        </class>
+    </package>
+</jdo>]]></source>
+            <p>
+                and so in our application we create some objects in <i>datastore1</i>,
like this
+            </p>
+            <source>
+PersistenceManagerFactory pmf1 = JDOHelper.getPersistenceManagerFactory("jdo.1.properties");
+PersistenceManager pm1 = pmf1.getPersistenceManager();
+Transaction tx1 = pm1.currentTransaction();
+Object holderId = null;
+try
+{
+    tx1.begin();
+
+    ElementHolder holder = new ElementHolder(101);
+    holder.addElement(new Element("First Element"));
+    holder.addElement(new Element("Second Element"));
+    holder.addElement(new SubElement("First Inherited Element"));
+    holder.addElement(new SubElement("Second Inherited Element"));
+    pm1.makePersistent(holder);
+
+    tx1.commit();
+    holderId = JDOHelper.getObjectId(holder);
+}
+finally
+{
+    if (tx1.isActive())
+    {
+        tx1.rollback();
+    }
+    pm1.close();
+}</source>
+            <p>
+                and now we want to replicate these objects into <i>datastore2</i>,
so we detach them from <i>datastore1</i>
+                and attach them to <i>datastore2</i>, like this
+            </p>
+            <source>
+// Detach the objects from "datastore1"
+ElementHolder detachedHolder = null;
+pm1 = pmf1.getPersistenceManager();
+tx1 = pm1.currentTransaction();
+try
+{
+    pm1.getFetchPlan().setGroups(new String[] {FetchPlan.DEFAULT, FetchPlan.ALL});
+    pm1.getFetchPlan().setMaxFetchDepth(-1);
+
+    tx1.begin();
+
+    ElementHolder holder = (ElementHolder) pm1.getObjectById(holderID);
+    detachedHolder = (ElementHolder) pm1.detachCopy(holder);
+
+    tx1.commit();
+}
+finally
+{
+    if (tx1.isActive())
+    {
+        tx1.rollback();
+    }
+    pm1.close();
+}
+
+// Attach the objects to datastore2
+PersistenceManagerFactory pmf2 = JDOHelper.getPersistenceManagerFactory("jdo.2.properties");
+PersistenceManager pm2 = pmf2.getPersistenceManager();
+Transaction tx2 = pm2.currentTransaction();
+try
+{
+    tx2.begin();
+
+    pm2.makePersistent(detachedHolder);
+
+    tx2.commit();
+}
+finally
+{
+    if (tx2.isActive())
+    {
+        tx2.rollback();
+    }
+    pm2.close();
+}</source>
+            <p>
+                These objects are now replicated into <i>datastore2</i>.
+                Clearly you can extend this basic idea and replicate large amounts of data.
+            </p>
+        </section>
+    </body>
+</document>



Mime
View raw message