Persistence

Using Persistence

Code Example

Code examples make it easy to comprehend the package.


import org.javagroups.persistence.*;

// getting default factor
PersistenceFactory factory = PersistenceFactory();
 
//get reference to PersistenceManager (handle Exception as required by application)
// if user defines own properties
 try
{
    String filepath = "/home/user/userdefined.properties"
    PersistenceManager manager = factory.createPersistenceManager(filepath);
   
    //if user used existing properties by filling the values (default properties)
    // PersistenceManager manager = factory.createPersistenceManager();
}catch (Exception e)
{
    //handle Exception
}


//once the persistencemanager reference is obtained, use available
// to save a NV
HashMap map = new HashMap();
map.put("bela", "bela");
map.put("mandar", "mandar");
try
{
    manager.saveAll(map);                      // will store all values from map into storage
}catch (CannotPersistException cpe)
{
    //handle exception
}


//to update a value;
try
{
    map.put("bela", "geek");
    manager.save("bela", "geek");              // will update { {bela,bela}, {mandar,mandar})
                                               // to          { {bela,geek}, {mandar,mandar})
}catch (CannotPersistException cpe)
{
    //handle exception
}


// to remove an entry
try
{
    map.remove("mandar");
    String value = (String) manager.remove("mandar");// will update {{bela,geek}, {mandar, mandar}}
    //use value if required                           // to          {bela,geek}
}catch (CannotRemoveException cre)
{
    // handle exception
}

//**************************************************************************

// say, at start of application , need to get back stored state
//use above example to get back reference to "manager"
try
{
    HashMap map = (HashMap) manager.retrieveAll();    // map will get {{bela,geek}}
}catch (CannotRetrieveException cree)
{
    // handle exception
}

Entering Properties (persist.properties)

Currently, the properties reflect only the DB related, as the FILE based implementation is not in place.
 
Name Description Typical Value
persist Used to decide between DB or FILE. DB/FILE (No default assumed)
jdbc.table Lets the user dictate the schema used to storage purposes on the database. create table replhashmap(key varchar(100), keybin blob, valbin blob) 
jdbc.Driver Classname for driver that needs to be loaded for DB storage. oracle.jdbc.driver.OracleDriver
jdbc.Conn Connection string required for user to connect to Db instance jdbc:oracle:oci8:@instance
jdbc.User User name required to connect to provided DB instance user
jdbc.Pass Password wrt to User provided pass



Using org.javagroups.blocks.DistributedHashtable with PersistenceManager

// Example of how to setup and use DistributedHashtable