Package org.odmg

Examples of org.odmg.Transaction


    public void testStoreFKinPK() throws Exception
    {
        final long timestamp = System.currentTimeMillis();
        try
        {
            Transaction tx = odmg.newTransaction();
            tx.begin();

            Master master_1 = populatedMasterFKinPK(tx, 5, timestamp);
            Master master_2 = populatedMasterFKinPK(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


    {
        long timestamp = System.currentTimeMillis();
        try
        {
            // 2. Get a list of all Masters
            Transaction tx = odmg.newTransaction();
            tx.begin();
            // 1. Insert some objects into the database, some of them with
            // details in DetailFKinPK, some of them in DetailFKnoPK
            Master master_1 = populatedMasterFKinPK(tx, 7, timestamp);
            Master master_2 = populatedMasterFKinPK(tx, 6, timestamp);
            Master master_3 = populatedMasterFKnoPK(tx, 7, timestamp);
            Master master_4 = populatedMasterFKnoPK(tx, 6, timestamp);

            tx.lock(master_1, Transaction.WRITE);
            tx.lock(master_2, Transaction.WRITE);
            tx.lock(master_3, Transaction.WRITE);
            tx.lock(master_4, Transaction.WRITE);
            tx.commit();

            tx.begin();
            OQLQuery query = odmg.newOQLQuery();
            query.create("select masters from " + Master.class.getName() + " where masterText like $1");
            query.bind("%" + timestamp);
            DList allMasters = (DList) query.execute();

            // Iterator over all Master objects
            Iterator it = allMasters.iterator();
            int counter = 0;
            while (it.hasNext())
            {
                ++counter;
                Master aMaster = (Master) it.next();
                Iterator it2 = aMaster.collDetailFKinPK.iterator();
                while (it2.hasNext())
                    database.deletePersistent(it2.next());
                it2 = aMaster.collDetailFKnoPK.iterator();
                while (it2.hasNext())
                    database.deletePersistent(it2.next());
                database.deletePersistent(aMaster);
            }
            tx.commit();
            assertEquals("Wrong count of Master objects found", 4, counter);

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

        obj2.setPk(contractPk);
        obj2.setContractValue2(2);
        try
        {
            // 1. Insert object
            Transaction tx = odmg.newTransaction();
            tx.begin();
            /*
            arminw:
            seems to have problems when within a tx a object
            was stored/deleted.
            Without obj1 test pass
            TODO: fix this
            */
            //database.makePersistent(obj1);
            database.makePersistent(obj2);
            //database.deletePersistent(obj1);
            /*
             thma: I checked this, and don't see a problem here.
            obj1 and obj2 have the same Identity. Thus the
            calls database.makePersistent(obj1); and database.makePersistent(obj2);
            will only register one instance to the transaction.
            The second call does not add a second instance, but just marks the
            existing instance as dirty a second time.
            So it's no wonder why after deletePersistent(obj1); no contract is found.
            Works as designed.
            The Lesson to learn: never let business objects have the same primary key values!
             * */
            tx.commit();
            Collection result = getContract(contractPk, odmg);
            assertEquals("We should found exact one contract", 1, result.size());
            obj2 = (Contract) result.iterator().next();
            if (obj2 == null)
            {
                fail("Contract not found");
            }
            else if (obj2.getContractValue2() != 2)
            {
                fail("Wrong contract found");
            }

            // 2. Delete, then insert object with the same identity
            tx.begin();
            database.deletePersistent(obj2);
            database.makePersistent(obj1);
            tx.commit();
            result = getContract(contractPk, odmg);
            assertEquals("We should found exact one contract", 1, result.size());
            obj1 = (Contract) result.iterator().next();
            if (obj1 == null)
            {
                fail("Contract not found");
            }
            else if (obj1.getContractValue2() != 1)
            {
                fail("Wrong contract found");
            }

            // 3. Delete
            tx.begin();
            database.deletePersistent(obj1);
            tx.commit();

            result = getContract(contractPk, odmg);
            if (result.size() > 0)
            {
                fail("Contract was not deleted");
View Full Code Here

    {
        if(log.isDebugEnabled()) log.debug("storeObjects");

        /* One possibility of storing objects is to use the current transaction
         associated with the container */
        Transaction tx = odmg.currentTransaction();
        for (Iterator iterator = objects.iterator(); iterator.hasNext();)
        {
            tx.lock(iterator.next(), Transaction.WRITE);
        }
        return objects;
    }
View Full Code Here

    {
        try
        {
            /* One possibility of storing objects is to use the current transaction
             associated with the container */
            Transaction tx = odmg.currentTransaction();
            for (Iterator iterator = objects.iterator(); iterator.hasNext();)
            {
                tx.lock(iterator.next(), Transaction.WRITE);
            }
        }
        catch (LockNotGrantedException e)
        {
            log.error("Failure while storing objects " + objects, e);
View Full Code Here

        }
        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
        {
View Full Code Here

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

        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        // perform transaction
        tx.begin();

        OQLQuery query = odmg.newOQLQuery();
        // use 'like' instead of '=' when perform query with wildcards
        query.create(
                "select anArticle from " + Article.class.getName() + " where productGroup.groupName like \"Fruit*\"");
        List results = (List) query.execute();

        // crosscheck
        query = odmg.newOQLQuery();
        query.create("select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
        List check = (List) query.execute();
        if (check.size() < 1)
            fail("Could not found ProductGroup's for: " +
                    "select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
        ProductGroup pg = (ProductGroup) check.get(0);

        assertEquals(pg.getAllArticlesInGroup().size(), results.size());
        assertTrue((results.size() > 0));

        tx.commit();


        // close database

        db.close();
View Full Code Here

        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

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

            OQLQuery query = odmg.newOQLQuery();
            query.create("select x from " + Article.class.getName() + " where productGroupId = 7");
            DList results = (DList) query.execute();

            int originalSize = results.size();
            assertTrue("result count have to be > 0", originalSize > 0);

//            OJB.getLogger().debug(results);

            String name = "gimme fruits_" + System.currentTimeMillis();

            db.bind(results, name);
            tx.commit();

            tx = odmg.newTransaction();
            tx.begin();

            ((TransactionImpl) tx).getBroker().clearCache();

            // look it up again
            DList newResults = (DList) db.lookup(name);

            assertEquals(originalSize, newResults.size());
            Article art = (Article) newResults.get(0);
            assertNotNull(art);
//            OJB.getLogger().info(results);

            tx.commit();

        }
        catch (Exception e)

        {
            tx.abort();
            throw e;
        }

        // close database
        try
View Full Code Here

        }
        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 = $678");
            query.bind(new Integer(30));

            List results = (List) query.execute();

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

            //crosscheck with PersistenceBroker lookup
            // 1. get OID
            Article example = new Article();
            example.setArticleId(30);
            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
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.