Package org.apache.ojb.otm.core

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


     */
    public Iterator findByCriteriaWithLock(Query query, int lock)
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;
        try
        {
            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
            tx   = kit.getTransaction(conn);

            tx.begin();

            Iterator results = conn.getIteratorByQuery(query, lock);

            tx.commit();

            return results;
        }
        finally
        {
View Full Code Here


     */
    public Iterator findByOQL(String query, Object[] queryParams) throws Exception
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;

        try
        {
            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
            tx   = kit.getTransaction(conn);

            OQLQuery oql = conn.newOQLQuery();

            oql.create(query);

            if (queryParams != null)
            {
                for (int idx = 0; idx < queryParams.length; ++idx)
                {
                    oql.bind(queryParams[idx]);
                }
            }

            tx.begin();

            Iterator results = conn.getIteratorByOQLQuery(oql);

            tx.commit();

            return results;
        }
        catch (QueryInvalidException ex)
        {
            if (tx.isInProgress())
            {
                tx.rollback();
            }
            throw new Exception("Invalid OQl expression given");
        }
        catch (QueryParameterCountInvalidException ex)
        {
            if (tx.isInProgress())
            {
                tx.rollback();
            }
            throw new Exception("Incorrect number of bindings given");
        }
        catch (QueryParameterTypeInvalidException ex)
        {
            if (tx.isInProgress())
            {
                tx.rollback();
            }
            throw new Exception("Incorrect type of object given as binding");
        }
        finally
        {
View Full Code Here

     */
    public Iterator moreRealisticQueryByCriteria(Query query, int lock)
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;
        try
        {
            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
            tx   = kit.getTransaction(conn);

            boolean auto = !tx.isInProgress();

            if (auto)
            {
                tx.begin();
            }

            Iterator results = conn.getIteratorByQuery(query, lock);

            if (auto)
            {
                tx.commit();
            }

            return results;
        }
        finally
View Full Code Here

     */
    public void renameWidgetExample()
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;

        try
        {
            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
            tx   = kit.getTransaction(conn);

            tx.begin();

            Product sample = new Product();

            sample.setName("Wonder Widget");

            Query    query         = QueryFactory.newQueryByExample(sample);
            Iterator wonderWidgets = moreRealisticQueryByCriteria(query, LockType.WRITE_LOCK);

            while (wonderWidgets.hasNext())
            {
                Product widget = (Product)wonderWidgets.next();

                widget.setName("Improved Wonder Widget");
            }

            tx.commit();
        }
        finally
        {
            conn.close();
        }
View Full Code Here

        _conn = null;
    }

    public void testOtmSession() throws Throwable
    {
        Transaction tx = null;
        Criteria crit;
        Query q;
        EnhancedOQLQuery oql;
        Iterator it;
        Article example;

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

            example = (Article) _conn.getObjectByIdentity(
                    new Identity(Article.class, Article.class,
                                 new Object[] {new Integer(77777)}));
            if (example == null)
            {
                example = Article.createInstance();
                example.setArticleId(77777);
            }
            example.setProductGroupId(7);
            example.setStock(333);
            example.setArticleName("333");
            _conn.makePersistent(example);

            tx.commit();

            Identity oid = _conn.getIdentity(example);

            // get from the cache
            tx = _kit.getTransaction(_conn);
            tx.begin();
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", 7, example.getProductGroupId());
            assertEquals("should be equal", 333, example.getStock());
            assertEquals("should be equal", "333", example.getArticleName());
            tx.commit();

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", 7, example.getProductGroupId());
            assertEquals("should be equal", "333", example.getArticleName());
            example.setArticleName("334"); // test update
            tx.commit();

            // get from the database via Query
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            crit = new Criteria();
            crit.addEqualTo("articleId", new Integer(77777));
            crit.addEqualTo("articleName", "334");
            q = QueryFactory.newQuery(Article.class, crit);
            it = _conn.getIteratorByQuery(q);
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId());
                assertEquals("should be equal", "334", article.getArticleName());
                article.setArticleName("335"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database via OQLQuery Iterator
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            oql = _conn.newOQLQuery();
            oql.create("select a from " + Article.class.getName()
                + " where articleId=$1 and articleName=$2");
            oql.bind(new Integer(77777));
            oql.bind("335");
            it = _conn.getIteratorByOQLQuery(oql);
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId());
                assertEquals("should be equal", "335", article.getArticleName());
                article.setArticleName("336"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database via OQLQuery Collection
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            oql.bind(new Integer(77777));
            oql.bind("336");
            it = ((Collection) oql.execute()).iterator();
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId());
                assertEquals("should be equal", "336", article.getArticleName());
                article.setArticleName("337"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", "337", example.getArticleName());
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
                tx.begin();
                example = (Article) _conn.getObjectByIdentity(oid);
                _conn.deletePersistent(example);
                tx.commit();
            }
            catch (Throwable ex)
            {
                ex.printStackTrace();
                tx.rollback();
            }
        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
View Full Code Here

        }
    }

    public void testCollectionProxy() throws Throwable
    {
        Transaction tx = null;

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

            ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy();
            pg.setId(77777);
            pg.setName("1");
            _conn.makePersistent(pg);

            tx.commit();

            Identity oid = _conn.getIdentity(pg);

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
            assertTrue("CollectionProxy isn't loaded",
                    !((CollectionProxyDefaultImpl) pg.getAllArticlesInGroup()).isLoaded());
            Article article = Article.createInstance();
            article.setArticleId(77777);
            article.setProductGroup(pg);
            article.setStock(333);
            article.setArticleName("333");
            pg.getAllArticlesInGroup().add(article);
            _conn.makePersistent(article);
            tx.commit();

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
            assertEquals("CollectionProxy size", 1, pg.getAllArticlesInGroup().size());
            ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("444");
            tx.commit();

            // test isolation of the cache
            ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("555");

            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
            assertEquals("Article name", "444",
                    ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).getArticleName());
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
                tx.begin();
                pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
                _conn.deletePersistent(pg.getAllArticlesInGroup().get(0));
                _conn.deletePersistent(pg);
                tx.commit();
            }
            catch (Throwable ex)
            {
                ex.printStackTrace();
                tx.rollback();
            }
        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
View Full Code Here

        }
    }

    public void testOtmCache() throws Throwable
    {
        Transaction tx = null;

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

            ProductGroup pg = new ProductGroup();
            pg.setId(77777);
            pg.setName("1");
            _conn.makePersistent(pg);
            Article article = Article.createInstance();
            article.setArticleId(77777);
            article.setStock(373);
            pg.add(article);
            article.setProductGroup(pg);
            _conn.makePersistent(article);
            tx.commit();

            Identity aOid = _conn.getIdentity(article);
            Identity pgOid = _conn.getIdentity(pg);

            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            pg.setName("2");
            _conn.makePersistent(pg);
            tx.rollback();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            article = (Article) _conn.getObjectByIdentity(aOid);
            assertEquals("should be equal", "1", article.getProductGroup().getName());
            tx.commit();

            // test checkpoint
            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            pg.setName("2");
            _conn.makePersistent(pg);
            tx.checkpoint();
            tx.rollback();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            article = (Article) _conn.getObjectByIdentity(aOid);
            assertEquals("should be equal", "1", article.getProductGroup().getName());
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
                tx.begin();
                article = (Article) _conn.getObjectByIdentity(aOid);
                _conn.deletePersistent(article);
                _conn.deletePersistent(article.getProductGroup());
                tx.commit();
            }
            catch (Throwable ex)
            {
                ex.printStackTrace();
                tx.rollback();
            }
        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
View Full Code Here

        }
    }

    public void testOtmIsolation() throws Throwable
    {
        Transaction tx = null;
        Transaction tx2 = null;
        OTMConnection conn2;

        conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());

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

            ProductGroup pg = new ProductGroup();
            pg.setId(77777);
            pg.setName("1");
            _conn.makePersistent(pg);
            tx.commit();

            Identity pgOid = _conn.getIdentity(pg);

            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            pg.setName("2");

            tx2 = _kit.getTransaction(conn2);
            tx2.begin();
            pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
            assertEquals("should be equal", "1", pg.getName());
            tx2.commit();
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
View Full Code Here

        }
    }

    public void testOtmLocks() throws Throwable
    {
        Transaction tx = null;
        Transaction tx2 = null;
        OTMConnection conn2;
        ProductGroup pg = null;
        ProductGroup pg2 = null;
        Identity pOid = null;
        Identity pOid2 = null;

        conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());

        try
        {
            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = new ProductGroup();
            pg.setId(77777);
            pg.setName("1");
            pOid = _conn.getIdentity(pg);
            if (_conn.getObjectByIdentity(pOid) == null)
            {
                _conn.makePersistent(pg);
            }
            pg2 = new ProductGroup();
            pg2.setId(77778);
            pg2.setName("1");
            pOid2 = _conn.getIdentity(pg2);
            if (_conn.getObjectByIdentity(pOid2) == null)
            {
                _conn.makePersistent(pg2);
            }
            tx.commit();

            final Identity pgOid = _conn.getIdentity(pg);
            final Identity pgOid2 = _conn.getIdentity(pg2);


            final Transaction tx3 = _kit.getTransaction(_conn);
            tx3.begin();
            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
            // we can write lock twice from the same tx
            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);

            // test different LockWaitStrategies
            _kit.setLockWaitStrategy(new NoWaitStrategy());
            failIfLockForWrite(conn2, pgOid);
            _kit.setLockWaitStrategy(new TimeoutStrategy(1));
            failIfLockForWrite(conn2, pgOid);

            // Second test for the TimeoutStrategy:
            // let the second tx to lock
            _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
            tx2 = _kit.getTransaction(conn2);
            tx2.begin();
            (new Thread()
            {
                public void run()
                {
                    try
                    {
                        Thread.sleep(1000);
                        tx3.commit();
                    }
                    catch (InterruptedException ex)
                    {
                    }
                }
View Full Code Here

        }
    }

    public void testUpdateByReachability() throws Throwable
    {
        Transaction tx = null;
        ProductGroup pg;
        Article article;
        Article article2;
        org.apache.ojb.broker.Person person;
        org.apache.ojb.broker.Project project;
        Identity aOid = null;
        Identity aOid2 = null;
        Identity pgOid = null;
        Identity prsOid = null;
        Identity prjOid = null;

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

            pg = new ProductGroup();
            pg.setId(77777);
            pgOid = _conn.getIdentity(pg);
            pg.setName("1");
            _conn.makePersistent(pg);
            article = Article.createInstance();
            article.setArticleId(77777);
            aOid = _conn.getIdentity(article);
            article.setStock(333);
            pg.add(article);
            article.setProductGroup(pg);
            article2 = Article.createInstance();
            article2.setArticleId(77778);
            aOid2 = _conn.getIdentity(article2);
            article2.setStock(334);
            pg.add(article2);
            article2.setProductGroup(pg);
            _conn.makePersistent(article);
            _conn.makePersistent(article2);
            person = new org.apache.ojb.broker.Person(77777, "first", "last");
            prsOid = _conn.getIdentity(person);
            project = new org.apache.ojb.broker.Project(77777, "title", "desc");
            prjOid = _conn.getIdentity(project);
            _conn.makePersistent(person);
            _conn.makePersistent(project);
            tx.commit();


            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            article2 = (Article) pg.getAllArticles().get(0);
            article = (Article) pg.getAllArticles().get(1);
            if (!_conn.getIdentity(article2).equals(aOid2))
            {
                article = (Article) pg.getAllArticles().get(0);
                article2 = (Article) pg.getAllArticles().get(1);
                if (!_conn.getIdentity(article2).equals(aOid2))
                {
                    fail("Missing the second article");
                }
            }
            article.setStock(433);
            article2.setStock(434);
            pg.setName("2");
            tx.commit();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidateAll();
            article = (Article) _conn.getObjectByIdentity(aOid);
            article2 = (Article) _conn.getObjectByIdentity(aOid2);
            assertEquals("should be equal", "2", article.getProductGroup().getName());
            assertEquals("should be equal", 433, article.getStock());
            assertEquals("should be equal", 434, article2.getStock());
            tx.commit();

            // Test M:N relations
            tx = _kit.getTransaction(_conn);
            tx.begin();
            person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
            project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
            person.getProjects().add(project);
            tx.commit();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidateAll();
            person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
            project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
            assertEquals("should be equal", 1, person.getProjects().size());
            tx.commit();
        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
            throw ex;
        }
        finally
        {
            try
            {
                if (tx == null || !tx.isInProgress())
                {
                    tx = _kit.getTransaction(_conn);
                    tx.begin();
                }

                if (aOid != null)
                {
                    article = (Article) _conn.getObjectByIdentity(aOid);
                    if (article != null)
                    {
                        _conn.deletePersistent(article);
                    }
                }
                if (aOid2 != null)
                {
                    article2 = (Article) _conn.getObjectByIdentity(aOid2);
                    if (article2 != null)
                    {
                        _conn.deletePersistent(article2);
                    }
                }
                if (pgOid != null)
                {
                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
                    if (pg != null)
                    {
                        _conn.deletePersistent(pg);
                    }
                }
                if (prsOid != null)
                {
                    person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
                    if (person != null)
                    {
                        _conn.deletePersistent(person);
                    }
                }
                if (prjOid != null)
                {
                    project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
                    if (project != null)
                    {
                        _conn.deletePersistent(project);
                    }
                }
                tx.commit();
            }
            catch (Throwable ex)
            {
                ex.printStackTrace();
                tx.rollback();
            }
        }
    }
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.