-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Session.replicate() usage
PostPosted: Mon Jan 05, 2004 4:35 am 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Can someone provide some insight on how to use Session.replicate and what exactly it does?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 7:42 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Well, the API javadoc for Hibernate says "Persist all reachable transient objects, reusing the current identifier values." For exactly what it does, see the source. :) (net.sf.hibernate.impl.SessionImpl)

I haven't seen any code examples, but I can show you how I'm using it. Here's an example of a stateless session EJB call that replicates objects from one server to another.

Server code:
Code:
public List synchSiteDataObjects(List replicants)
                          throws RemoteException {

    List modifiedSDOs = null;


    Session session = getSession();

    try {
   
        SynchManager synchManager = SynchManager.getInstance();

        Synch lastSynch = synchManager.getLast(session);

        try {
            Transaction trans = session.beginTransaction();

            for(Iterator i = replicants.iterator(); i.hasNext();) {
                session.replicate(i.next(), ReplicationMode.LATEST_VERSION);
            }

            trans.commit();
        } catch(HibernateException he) {
            trans.rollback();
            LOG.error("replicate failed!", he);
            throw he;
        }

        //One of the objects replicated should have been a new Synch object.
        Synch newSynch = synchManager.getLast(session);

        modifiedSDOs = synchManager.getModified(
                                                lastSynch,
                                                newSynch,
                                                session
                                               );

    } catch(HibernateException he) {
        LOG.fatal("synchSiteDataObjects failed!", he);
        throw new RemoteException("synchSiteDataObjects failed!", he);
    }}

    return modifiedSDOs;
}


Client code:
Code:
public static final void synchSiteDataObjects(
                                              Site site,
                                              Session session
                                             )
                                       throws RemoteException,
                                              NamingException,
                                              CreateException,
                                              UnknownHostException,
                                              HibernateException {

    //Get remote interface first.  If this fails we want to
    //throw exceptions quickly.
    SynchRemote bean = getSynchRemote(site);

    SynchManager synchManager = SynchManager.getInstance();

    Synch lastSynch = synchManager.getLast(
                                           site,
                                           session
                                          );

    //Include a new Synch object to restrict objects replicated
    //and also be replicated to server. (Saves new Synch object to session.)
    Synch newSynch = SynchFactory.getInstance()
                                 .create(
                                         site,
                                         session
                                        );

    List modifiedSDOs = synchManager.getModified(
                                                 lastSynch,
                                                 newSynch,
                                                 session
                                                );

    List replicants = bean.synchSiteDataObjects(modifiedSDOs);

    try {
        Transaction trans = session.beginTransaction();

        for(Iterator i = replicants.iterator(); i.hasNext();) {
            session.replicate(i.next(), ReplicationMode.LATEST_VERSION);
        }

        trans.commit();
    } catch(HibernateException he) {
        trans.rollback();
        LOG.error("replicate failed!", he);
        throw he;
    }
}


In this example a Site object describes the location (ip address) of the server I want to replicate to. I use a Synch object who'se only attribute is a timestamp typed version field to limit the objects replicated. The SynchFactory creates a new Synch object. The SynchManager provides convenience methods for getting the latest Synch object (the one with the most recent timestamp) and gathering all objects which have been modified between the timestamps of two Synch objects. (All of my persistent objects have a timestamp version.) One of the objects in the replicants list is a Synch object generated by the client.

How well does this work? Don't know yet. All of my persistent objects are mapped using <joined-subclass> and there seems to be a problem with replicate() and joined-subclass. http://forum.hibernate.org/viewtopic.php?t=926835


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.