Package org.apache.ojb.otm.core

Examples of org.apache.ojb.otm.core.Transaction


        }
    }

    public void testSwizzling() throws Throwable
    {
        Transaction tx = null;
        ProductGroup pg;
        Article article;
        Article article2;

        try
        {
            tx = _kit.getTransaction(_conn);
            tx.begin();

            pg = new ProductGroup();
            pg.setId(77777);
            _conn.makePersistent(pg);
            article = Article.createInstance();
            article.setArticleId(77777);
            article.setStock(333);
            pg.add(article);
            article.setProductGroup(pg);
            _conn.makePersistent(article);
            article2 = Article.createInstance();
            article2.setArticleId(article.getArticleId());
            article2.setStock(334);
            article2.setProductGroup(pg);
            _conn.makePersistent(article2);
            article = (Article) pg.getAllArticles().get(0);
            assertEquals("should be equal", 334, article.getStock());
        }
        finally
        {
            if (tx != null)
            {
                try
                {
                    tx.rollback();
                }
                catch (Throwable ex)
                {
                    ex.printStackTrace();
                }
View Full Code Here


    }

    private void failIfLockForWrite(OTMConnection conn2, Identity oid)
            throws Exception
    {
        Transaction tx2 = null;

        tx2 = _kit.getTransaction(conn2);
        tx2.begin();
        try {
            conn2.getObjectByIdentity(oid, LockType.WRITE_LOCK);
            fail("LockingException was not thrown");
        } catch (LockingException ex) {
            // ok: we cannot write lock from another tx
            tx2.rollback();
        }
    }
View Full Code Here

   public void testSwizzle4() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
    {
        clearTestData();
        TestClassA a = generateTestData();
    TestClassB b = a.getB();
        Transaction tx = _kit.getTransaction(_conn);

        tx.begin();
        _conn.makePersistent(b);
        _conn.makePersistent(a);
        b.setA(a);
        tx.commit();
        /**
        * clear to start test
        */
        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        /**
     * load B
     */
    Identity oidb = _conn.getIdentity(b);
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
    /**
     * load A
      */
    Identity oida = _conn.getIdentity(a);
    TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
    assertTrue(a1 != null);
    assertTrue(a1.getB().equals(b1));
    assertTrue(b1.getA().equals(a1));
        /**
     * update B
     */
        a.setValue1("a");
        _conn.makePersistent(a);

    /**
     * B, as navigated from A, should be the same as B gotten directly.
     */
    assertTrue(a1.getValue1().equals(a.getValue1()));
        tx.commit();

    /**
     * clear
     */
        clearTestData();
View Full Code Here

     * Cache data must be independent of any objects available to used,
     * otherwise modification of user objects outside transaction will
     * damage cache data
     */
    public void testCacheIndependence() throws Throwable {
        Transaction tx = null;
        Collection addresses = this.getAddresses();
        deleteAddresses(addresses);
        Identity oid;
        Address address = new Address("oldCountry", "oldCity", "oldStreet");

        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.makePersistent(address);
            oid = _conn.getIdentity(address);
            tx.commit();

            address.setStreet("newStreet");

            tx = _kit.getTransaction(_conn);
            tx.begin();
            address = (Address) _conn.getObjectByIdentity(oid);
            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
            tx.commit();

            address.setStreet("newStreet");

            tx = _kit.getTransaction(_conn);
            tx.begin();
            address = (Address) _conn.getObjectByIdentity(oid, LockType.WRITE_LOCK);
            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
            tx.commit();

            address.setStreet("newStreet");

            tx = _kit.getTransaction(_conn);
            tx.begin();
            address = (Address) _conn.getObjectByIdentity(oid);
            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
View Full Code Here

                                                address.getStreet());
        }
    }

    private Collection getAddresses() throws Throwable {
        Transaction tx = null;
        Collection addresses;
        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidateAll();
            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
            addresses = _conn.getCollectionByQuery(q);
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
View Full Code Here

        return addresses;
    }

    private Collection updateAddresses(Collection newAddresses)
            throws Throwable {
        Transaction tx = null;
        Collection oldAddresses;
        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();

            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
            oldAddresses = _conn.getCollectionByQuery(q);

            Iterator oldAddressesIterator = oldAddresses.iterator();
            while (oldAddressesIterator.hasNext()) {
                Address oldAddress = (Address)oldAddressesIterator.next();
                if (!newAddresses.contains(oldAddress)) {
                    _conn.deletePersistent(oldAddress);
                }
            }

            Iterator newAddressesIterator = newAddresses.iterator();
            while (newAddressesIterator.hasNext()) {
                Address newAddress = (Address)newAddressesIterator.next();
                _conn.makePersistent(newAddress);
            }
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
View Full Code Here

        return newAddresses;
    }

    private Collection deleteAddresses(Collection oldAddresses)
            throws Throwable {
        Transaction tx = null;
        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();

            Iterator oldAddressesIterator = oldAddresses.iterator();
            while (oldAddressesIterator.hasNext()) {
                Address oldAddress = (Address)oldAddressesIterator.next();
                _conn.deletePersistent(oldAddress);
                oldAddressesIterator.remove();
            }
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
View Full Code Here

    private void clearTestData() throws LockingException
    {
        TestClassA a = generateTestData();
        TestClassB b2 = generateAnotherB();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity oid = _conn.getIdentity(a);
        Identity oidb = _conn.getIdentity(a.getB());
        Identity oidb2 = _conn.getIdentity(b2);
        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
        if (a1 != null)
        {
            _conn.deletePersistent(a1);
        }
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
        if (b1 != null)
        {
            _conn.deletePersistent(b1);
        }
        b2 = (TestClassB) _conn.getObjectByIdentity(oidb2);
        if (b2 != null)
        {
            _conn.deletePersistent(b2);
        }

        Article article = Article.createInstance();
        article.setArticleId(77777);
        ProductGroup pg = new ProductGroup();
        pg.setId(77777);
        Identity oidArt = _conn.getIdentity(article);
        Identity oidPG = _conn.getIdentity(pg);
        article = (Article) _conn.getObjectByIdentity(oidArt);
        if (article != null)
        {
            _conn.deletePersistent(article);
        }
        pg = (ProductGroup) _conn.getObjectByIdentity(oidPG);
        if (pg != null)
        {
            _conn.deletePersistent(pg);
        }
        tx.commit();
    }
View Full Code Here

        createTestData();
        /**
        * first get the contract object.
        */
        PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        Criteria crit = new Criteria();
        crit.addEqualTo("pk", "C" + TIME);
        Query q = QueryFactory.newQuery(Contract.class, crit);
        Iterator it = _conn.getIteratorByQuery(q, LockType.WRITE_LOCK);
        Object retval = null;
        RelatedToContract r2c = new RelatedToContract();
        r2c.setPk("R2C" + TIME);
        r2c.setRelatedValue1("matt");
        r2c.setRelatedValue2(34);
        r2c.setRelatedValue3(new Timestamp(TIME));
        _conn.makePersistent(r2c);
        while (it.hasNext())
        {
            retval = it.next();
            ((Contract) retval).setRelatedToContract(r2c);
        }
        tx.commit();
        r2c = null;
        tx = _kit.getTransaction(_conn);
        tx.begin();
        crit = new Criteria();
        crit.addEqualTo("pk", "E" + TIME);
        q = QueryFactory.newQuery(Effectiveness.class, crit);
        it = _conn.getIteratorByQuery(q);
        retval = null;
        while (it.hasNext())
        {
            retval = it.next();
        }
        tx.commit();
        assertTrue("contract object should have a RelatedToContract instance attached", ((Effectiveness) retval).getVersion().getContract().getRelatedToContract() != null);
    }
View Full Code Here

   public void testSwizzle3() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
    {
        clearTestData();
        TestClassA a = generateTestData();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(a.getB());
        _conn.makePersistent(a);
    TestClassB b = a.getB();
        tx.commit();
        /**
        * clear to start test
        */
        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        /**
     * load B
     */
    Identity oidb = _conn.getIdentity(b);
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
        assertTrue(b1 != null);
    /**
     * load A
      */
    Identity oida = _conn.getIdentity(a);
    TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);

    /**
     * B, as navigated from A, should be the same as B gotten directly.
     */
    assertTrue(a1.getB().equals(b1));
        tx.commit();

    /**
     * clear
     */
        clearTestData();
View Full Code Here

TOP

Related Classes of org.apache.ojb.otm.core.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.