Package org.odmg

Examples of org.odmg.Database


        Implementation impl = OJB.getInstance();
        Transaction    tx = impl.newTransaction();

        tx.begin();

        Database db = impl.getDatabase(product);

        db.deletePersistent(product);

        tx.commit();
    }
View Full Code Here


     * @param args The commandline arguments
     */
    public static void main(String[] args) throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database       db   = odmg.newDatabase();

        db.open("default", Database.OPEN_READ_WRITE);

        Product product = new Product();

        product.setName("Widget");
        product.setPrice(9.99);
        product.setStock(11);

        storeProduct(product);

        Product same = findProductByName(product.getName());

        System.out.println(same.getId() + " = " + product.getId());

        sellProduct(same, 1);

        db.close();
    }
View Full Code Here

     */
    public Application()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database       db   = odmg.newDatabase();

        // open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            ex.printStackTrace();
        }
View Full Code Here

    {
        String bindingName = "binding_for_testBind";

        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        // clean up roots map
        clearNRM();

        Article example = createArticle();

        Transaction tx = odmg.newTransaction();
        tx.begin();
        try
        {
            db.bind(example, bindingName);
            Article value = (Article) db.lookup(bindingName);
            assertTrue("Could not lookup object for binding name: "+bindingName, value != null);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            // tx.abort();
            fail("name " + bindingName + " should not be unknown. "+ex.getMessage());
        }
        catch (ObjectNameNotUniqueException e)
        {
            // tx.abort();
            fail("should not have happened: " + e.getMessage());
        }

        try
        {
            tx.begin();
            db.bind(example, bindingName);
            tx.commit();
            fail("We expected a ObjectNameNotUniqueException, but was not thrown");
        }
        catch (ObjectNameNotUniqueException ex)
        {
            // we wait for this exception
            assertTrue(true);
            tx.abort();
        }

        try
        {
            tx.begin();
            db.unbind(bindingName);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Can't unbind " + bindingName + ": "+ex.getMessage());
        }

        db.close();
    }
View Full Code Here

        String bindingName = "binding_for_testDoubleBindInOneTx";

        DList foundList = null;
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();
        tx.begin();
        db.bind(odmg.newDList(), bindingName);

        foundList = (DList)db.lookup(bindingName);
        assertTrue("Could not found bound DList", foundList != null);
        foundList = null;

        db.unbind(bindingName);
        try
        {
            foundList = (DList) db.lookup(bindingName);
            fail("Found unbound DList");
        }
        catch (ObjectNameNotFoundException ex)
        {
        }

        db.bind(odmg.newDList(), bindingName);
        try
        {
            foundList = (DList) db.lookup(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Could not found bound DList, binding name was: "+bindingName);
        }
        foundList = null;
        tx.commit();

        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            DList newList = (DList)db.lookup(bindingName);
            db.unbind(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Could not found bound DList in new tx");
        }
        tx.commit();

        db.close();
    }
View Full Code Here

    public void testLookup() throws Exception
    {
        String bindingName = "binding_for_testLookup";
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);

        // clear named roots.
        clearNRM();
        Transaction tx = odmg.newTransaction();
        //bind object to name
        tx.begin();
        Article example = createArticle();
        Identity oid = new Identity(example, ((HasBroker)tx).getBroker());

        try
        {
            db.bind(example, bindingName);
            tx.commit();
        }
        catch (ObjectNameNotUniqueException ex)
        {
            tx.abort();
            fail(ex.getMessage());
        }

        // TestThreadsNLocks look up
        Article lookedUp1 = null;
        tx = odmg.newTransaction();
        tx.begin();

        try
        {
            // lookup by name binding
            lookedUp1 = (Article) db.lookup(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("lookup by name: " + bindingName + " should not be unknown");
        }

        tx.commit();

        // looking up object by OID should return same Object as by name
        assertEquals("lookups should return identical object", example, lookedUp1);

        try
        {
            tx.begin();
            db.unbind(bindingName);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Can't unbind " + bindingName + ": "+ex.getMessage());
        }

        db.close();
    }
View Full Code Here

    public void testUnBind() throws Exception
    {
        String name = "binding_for_testUnBind";
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();

        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }

        // clear named roots.
        clearNRM();

        Transaction tx = odmg.newTransaction();
        // 1. perform binding
        tx.begin();

        Article example = createArticle();

        try
        {
            db.bind(example, name);
            tx.commit();
        }
        catch (ObjectNameNotUniqueException ex)
        {
            tx.abort();
            fail(ex.getMessage());
        }

        // 2. perform unbind
        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            db.unbind(name);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            tx.abort();
            fail("name " + name + "should be known");
        }

        // 3. check if name is really unknown now
        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            Article value = (Article) db.lookup(name);
        }
        catch (ObjectNameNotFoundException ex)
        {
            // OK !
            tx.commit();
            // close database
            try
            {
                db.close();
            }
            catch (ODMGException e)
            {
                fail("ODMGException: " + e.getMessage());
            }

            return;
        }

        // NOT OK!
        tx.abort();
        db.close();
        fail("name " + name + " should not be known after unbind");

    }
View Full Code Here

    /**TestThreadsNLocks state transition of modification states*/
    public void testModificationStates()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        ModificationState oldState = StateNewClean.getInstance();
        ModificationState newState = oldState.markDirty();
        assertEquals(StateNewDirty.getInstance(), newState);

        oldState = newState;
        newState = oldState.markDirty();
        assertEquals(oldState, newState);

        // close database
        try
        {
            db.close();
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

    public void testOdmgSession()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        //perform transaction
        try
        {
            tx.begin();

            Article example = new Article();
            example.setArticleId(777);
            example.setProductGroupId(7);
            db.makePersistent(example);

            // modify Object
            example.setStock(333);
            example.addToStock(47);
            example.addToStock(7);
            example.addToStock(4);

            //System.out.println("now commit all changes...");
            tx.commit();
        }
        catch (Exception ex)
        {
            tx.abort();
        }

        // close database
        try
        {
            db.close();
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

    public void testOQLQuery()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        //perform transaction
        try
        {
            tx.begin();

            OQLQuery query = odmg.newOQLQuery();
            query.create("select anArticle from " + Article.class.getName() + " where articleId = 60");
            List results = (List) query.execute();

            Article a = (Article) results.get(0);

            // cross check with PersistenceBroker lookup
            // 1. get OID
            Article example = new Article();
            example.setArticleId(60);
            Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker());
            // 2. lookup object by OID
            PersistenceBroker broker = ((TransactionImpl) tx).getBroker();
            broker.clearCache();
            Article b = (Article) broker.getObjectByIdentity(oid);

            assertEquals("should be same object", a, b);

            //System.out.println("now commit all changes...");
            tx.commit();
        }
        catch (Exception ex)
        {
            tx.abort();
            fail("ODMGException: " + ex.getMessage());
        }

        // close database
        try
        {
            db.close();
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

TOP

Related Classes of org.odmg.Database

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.