Package org.odmg

Examples of org.odmg.Transaction


     * @param product The product to store
     */
    public static void storeProduct(Product product)
    {
        Implementation impl = OJB.getInstance();
        Transaction    tx   = impl.newTransaction();

        tx.begin();
        tx.lock(product, Transaction.WRITE);
        tx.commit();
    }
View Full Code Here


     * @return The product if found
     */
    public static Product findProductByName(String name) throws Exception
    {
        Implementation impl = OJB.getInstance();
        Transaction    tx   = impl.newTransaction();

        tx.begin();

        OQLQuery query = impl.newOQLQuery();

        query.create("select products from " + Product.class.getName() + " where name = $1");
        query.bind(name);

        DList   results = (DList)query.execute();
        Product product = (Product)results.iterator().next();

        tx.commit();
        return product;
    }
View Full Code Here

     * @param number  The number of items to sell
     */
    public static void sellProduct(Product product, int number)
    {
        Implementation impl = OJB.getInstance();
        Transaction    tx   = impl.newTransaction();

        tx.begin();

        tx.lock(product, Transaction.WRITE);
        product.setStock(product.getStock() -  number);

        tx.commit();
    }
View Full Code Here

     * @param product The product to delete
     */
    public static void deleteProduct(Product product)
    {
        Implementation impl = OJB.getInstance();
        Transaction    tx = impl.newTransaction();

        tx.begin();

        Database db = impl.getDatabase(product);

        db.deletePersistent(product);

        tx.commit();
    }
View Full Code Here

        System.out.println("The list of available products:");

        try
        {
            // 1. open a transaction
            Transaction tx = odmg.newTransaction();

            tx.begin();

            // 2. get an OQLQuery object from the ODMG facade
            OQLQuery query = odmg.newOQLQuery();

            // 3. set the OQL select statement
            query.create("select allproducts from " + Product.class.getName());

            // 4. perform the query and store the result in a persistent Collection
            DList allProducts = (DList) query.execute();

            tx.commit();

            // 5. now iterate over the result to print each product
            for (Iterator iter = allProducts.iterator(); iter.hasNext();)
            {
                System.out.println(iter.next());
View Full Code Here

        // 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());
        }
View Full Code Here

        // 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

        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());
        }
View Full Code Here

        }

        // 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

    public void testStoreFKnoPK() throws Exception
    {
        long timestamp = System.currentTimeMillis();
        try
        {
            Transaction tx = odmg.newTransaction();
            tx.begin();
            Master master_1 = populatedMasterFKnoPK(tx, 5, timestamp);
            Master master_2 = populatedMasterFKnoPK(tx, 5, timestamp);

            database.makePersistent(master_1);
            database.makePersistent(master_2);
            tx.commit();

            // Check stored objects
            OQLQuery query = odmg.newOQLQuery();
            query.create("select masters from " + Master.class.getName() + " where masterText like $1");
            query.bind("%" + timestamp);
View Full Code Here

TOP

Related Classes of org.odmg.Transaction

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.