Package org.apache.ojb.otm.core

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


    {
      return m_zoo;
    }
    else
    {
      Transaction tx = _kit.getTransaction(_conn);
      tx.begin();
      Zoo zoo = new Zoo();
      zoo.setZooId(1234);
      Identity oid = _conn.getIdentity(zoo);
      zoo = (Zoo) _conn.getObjectByIdentity(oid);
      if (zoo == null)
      {
        zoo = new Zoo();
        zoo.setZooId(1234);
        _conn.makePersistent(zoo);
        Mammal mammal = new Mammal();
        mammal.setName("molly");
        mammal.setNumLegs(4);
        mammal.setAge(55);
        zoo.addAnimal(mammal);
        _conn.makePersistent(mammal);
        Reptile reptile = new Reptile();
        reptile.setColor("green");
        reptile.setName("hubert");
        reptile.setAge(51);
        zoo.addAnimal(reptile);
        _conn.makePersistent(reptile);
      }
      tx.commit();
      m_zoo = zoo;
      return m_zoo;
    }
  }
View Full Code Here


    {
      return m_tca;
    }
    else
    {
      Transaction tx = _kit.getTransaction(_conn);
      tx.begin();
      TestClassA tca = new TestClassA();
      tca.setOid("someoid");
      Identity oid = _conn.getIdentity(tca);
      tca = (TestClassA) _conn.getObjectByIdentity(oid);
      if (tca == null)
      {
        tca = new TestClassA();
        tca.setOid("someoid");
        tca.setValue1("abc");
        tca.setValue2("123");
        tca.setValue3(5);
        _conn.makePersistent(tca);
        TestClassB tcb = new TestClassB();
        tcb.setOid("boid");
        tcb.setValue1("hi there");
        _conn.makePersistent(tcb);
        tca.setB(tcb);
      }
      tx.commit();
      m_tca = tca;
      return m_tca;
    }
  }
View Full Code Here

     *
     * @throws Throwable
     */
    public void testJustAttachConnections() throws Throwable
    {
        Transaction tx = null;
        Article example;

        OTMConnection conn1 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
        OTMConnection conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
        try
        {
            tx = _kit.getTransaction(conn1);
            tx.begin();

            tx.registerConnection(conn2);

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

            EnhancedOQLQuery query = conn2.newOQLQuery();
            query.create("select obj from " + Article.class.getName()
                         + " where " + "articleId = " + example.getArticleId());
            Article same = (Article) conn2.getIteratorByOQLQuery(query).next();
            Assert.assertNotNull("Didn't find object in context of transaction", same);

            tx.commit();

        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
View Full Code Here

     */
    public static void storeProduct(Product product) throws LockingException
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;

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

            tx.begin();

            conn.makePersistent(product);

            tx.commit();
        }
        catch (LockingException ex)
        {
            if (tx.isInProgress())
            {
                tx.rollback();
            }
            throw ex;
        }
        finally
        {
View Full Code Here

     */
    public static void removeProduct(Product product) throws LockingException
    {
        OTMKit        kit  = SimpleKit.getInstance();
        OTMConnection conn = null;
        Transaction   tx   = null;

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

            tx.begin();

            conn.deletePersistent(product);

            tx.commit();
        }
        catch (LockingException ex)
        {
            if (tx.isInProgress())
            {
                tx.rollback();
            }
            throw ex;
        }
        finally
        {
View Full Code Here

     */
    public Iterator findByCriteria(Query query)
    {
        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);

            tx.commit();

            return results;
        }
        finally
        {
View Full Code Here

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

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.