PAiN DB Quick Start


Example of persistent object:

import net.sourceforge.pain.db.*; 
 
import java.util.*; 
 
/** Any subclass of DbObject is persistent object */ 
public class MyDataObject extends DbObject { 
 
    /** This constructor is used by PAiN DB during startup. 
     * No field access allowed from it. */ 
    public MyDataObject() { 
    } 
 
    /** This constructor is used by user for new objects creation 
     * object is atteched to given database. 
     * Object schema is automatically imported to db */ 
    public MyDataObject(PainDB db) throws RuntimeException { 
        super(db); 
    } 
 
    /** method called by database every time new class added to database 
     *  (one per class)*/ 
    protected DbClassSchema provideSchema() { 
        byte[] types = new byte[3]; 
        String[] names = new String[3]; 
 
        types[0] = DbType.INT; 
        names[0] = "my_int_field"; 
 
        types[1] = DbType.STRING; 
        names[1] = "my_string_field"; 
 
        types[2] = DbType.REFERENCE_SET; 
        names[2] = "my_references"; 
 
        return new DbClassSchema(types, names); 
    } 
 
    public int getMyIntField() { 
        return getInt(0); 
    } 
 
    public void setMyIntField(int value) { 
        setInt(0, value); 
    } 
 
    public String getMyStringField() { 
        return getString(1); 
    } 
 
    public void setMyStringField(String value) { 
        setString(1, value); 
    } 
 
    public Set getMyReferences() { 
        return getRefSet(2); 
    } 
} 

Code example of PAiN DB usage:

import net.sourceforge.pain.db.*; 
 
import java.util.*; 
 
public class PainDBQuickStart { 
 
    public static void main(String[] args) throws Exception { 
        final PainDB db = new PainDB("mydbfile"); 
 
        // Example 1: new object creation 
        db.beginTransaction(); 
        MyDataObject obj = new MyDataObject(db); 
        obj.setMyIntField(10); 
        obj.setMyStringField("Hello World!"); 
        db.commitTransaction(); 
 
        // Example 2: transparent collections support 
        db.beginTransaction(); 
        for (int i = 0; i < 100; i++) { 
            MyDataObject myReference = new MyDataObject(db); 
            myReference.setMyStringField("my_reference"); 
            obj.getMyReferences().add(myReference); 
        } 
        db.commitTransaction(); 
 
 
        //Example 3: rollback 
        db.beginTransaction(); 
        for (Iterator it = obj.getMyReferences().iterator(); it.hasNext();) { 
            MyDataObject myReference = (MyDataObject) it.next(); 
            it.remove(); 
            myReference.delete(); 
        } 
        db.rollbackTransaction(); // here all changes done in  last transaction will be rolled back 
 
 
        //Example 4: savepoints (subtransactions) 
        db.beginTransaction(); 
        obj.setMyStringField("new value 1"); 
        db.beginTransaction(); // internal transaction (savepoint)! 
        obj.setMyStringField("new value 2"); 
        db.rollbackTransaction(); // here we rolling back all changes done in internal transaction 
        db.commitTransaction(); // commiting all changes done in first level transaction 
 
        //Example 5: transaction wrapper, it's convinient to never write commit or rollback :) 
        DbTransaction t = new DbTransaction() { 
            public Object execute(Object[] params) throws Exception { 
                MyDataObject myDataObject = new MyDataObject(db); 
                myDataObject.setMyStringField("this transaction will be commited if no exception thrown!"); 
                return null; 
            } 
        }; 
        db.execute(t); 
 
 
        //Example 6: Subtransactions wrapping. 
        DbTransaction t1 = new DbTransaction() { 
            public Object execute(Object[] params) throws Exception { 
                final MyDataObject myDataObject = new MyDataObject(db); 
                myDataObject.setMyStringField("value1"); 
                DbTransaction t2 = new DbTransaction() { 
                    public Object execute(Object[] params) throws Exception { 
                        myDataObject.setMyStringField("value2"); 
                        return null; 
                    } 
                }; 
                db.execute(t2); 
                assert(myDataObject.getMyStringField().equals("value2")); 
                return null; 
            } 
        }; 
        db.execute(t1); 
 
        //example 7: automatic removal all references during object deletion 
        db.beginTransaction(); 
        MyDataObject obj7_1 = new MyDataObject(db); 
        MyDataObject obj7_2 = new MyDataObject(db); 
        obj7_1.getMyReferences().add(obj7_2); 
        assert (obj7_1.getMyReferences().contains(obj7_2)); 
        obj7_2.delete(); 
        assert (obj7_1.getMyReferences().isEmpty()); 
        db.commitTransaction(); 
 
        db.close(); 
    } 
} 


TODO...




<<back to the main page>>



Copyright c 2002-2003, Mike Fursov <fmike@mail.ru>


Vote for our project!


SourceForge.net Logo