Examples of PersistenceBroker


Examples of org.apache.ojb.broker.PersistenceBroker

  public void testExposeJdbcTransaction() throws LookupException, SQLException {
    MockControl dsControl = MockControl.createControl(DataSource.class);
    final DataSource ds = (DataSource) dsControl.getMock();
    MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
    final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
    MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
    final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
    final Object entity = new Object();
    MockControl conControl = MockControl.createControl(Connection.class);
    Connection con = (Connection) conControl.getMock();

    pb.serviceConnectionManager();
    pbControl.setReturnValue(cm, 2);
    cm.getConnection();
    cmControl.setReturnValue(con, 2);
    con.isReadOnly();
    conControl.setReturnValue(false, 1);
    pb.beginTransaction();
    pbControl.setVoidCallable(1);
    pb.delete(entity);
    pbControl.setVoidCallable(1);
    pb.commitTransaction();
    pbControl.setVoidCallable(1);
    pb.close();
    pbControl.setReturnValue(true, 1);

    dsControl.replay();
    pbControl.replay();
    cmControl.replay();
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

          "and configuration does not allow creation of non-transactional one here");
    }

    try {
      logger.debug("Opening OJB PersistenceBroker");
      PersistenceBroker pb = PersistenceBrokerFactory.createPersistenceBroker(pbKey);

      if (TransactionSynchronizationManager.isSynchronizationActive()) {
        logger.debug("Registering transaction synchronization for OJB PersistenceBroker");
        // Use same PersistenceBroker for further OJB actions within the transaction.
        // Thread object will get removed by synchronization at transaction completion.
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     *
     * @param product The product to store
     */
    public static void storeProduct(Product product)
    {
        PersistenceBroker broker = null;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            broker.beginTransaction();
            broker.store(product);
            broker.commitTransaction();
        }
        catch (PersistenceBrokerException ex)
        {
            if (broker != null)
            {
                broker.abortTransaction();
            }
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     *
     * @param products The products to store
     */
    public static void storeProducts(Product[] products)
    {
        PersistenceBroker broker = null;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            broker.beginTransaction();
            for (int idx = 0; idx < products.length; idx++)
            {
                broker.store(products[idx]);
            }
            broker.commitTransaction();
        }
        catch (PersistenceBrokerException ex)
        {
            if (broker != null)
            {
                broker.abortTransaction();
            }
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     * @param template The product whose equal shall be found
     * @return The found product if any
     */
    public static Product findByTemplate(Product template)
    {
        PersistenceBroker broker = null;
        Product           result = null;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            QueryByCriteria query = new QueryByCriteria(template);

            result = (Product)broker.getObjectByQuery(query);
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
        return result;
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     *
     * @return The products if products were found
     */
    public static Collection getExpensiveLowStockProducts()
    {
        PersistenceBroker broker  = null;
        Collection        results = null;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            Criteria criteria = new Criteria();

            criteria.addLessOrEqualThan("stock", new Integer(20));
            criteria.addGreaterOrEqualThan("price", new Double(100000.0));

            QueryByCriteria query = new QueryByCriteria(Product.class, criteria);

            results = broker.getCollectionByQuery(query);
        }
        catch (PersistenceBrokerException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
        return results;
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     * @param template The product to sell
     * @return Whether the product was sold
     */
    public static boolean sellOneProduct(Product template)
    {
        PersistenceBroker broker = null;
        boolean           isSold = false;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            QueryByCriteria query  = new QueryByCriteria(template);
            Product         result = (Product)broker.getObjectByQuery(query);

            if (result != null)
            {
                broker.beginTransaction();
                result.setStock(result.getStock() - 1);

                broker.store(result);
                // alternative, more performant:
                // broker.store(result, ObjectModificationDefaultImpl.UPDATE);

                broker.commitTransaction();
                isSold = true;
            }
        }
        catch (PersistenceBrokerException ex)
        {
            if (broker != null)
            {
                broker.abortTransaction();
            }
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
        return isSold;
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     * @param template The product whose equal shall be deleted from the database
     * @return Whether the product was deleted
     */
    public static boolean findAndDeleteProduct(Product template)
    {
        PersistenceBroker broker    = null;
        boolean           isDeleted = false;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            QueryByCriteria query  = new QueryByCriteria(template);
            Product         result = (Product)broker.getObjectByQuery(query);

            if (result != null)
            {
                broker.beginTransaction();
                broker.delete(result);
                broker.commitTransaction();
                isDeleted = true;
            }
        }
        catch (PersistenceBrokerException ex)
        {
            if (broker != null)
            {
                broker.abortTransaction();
            }
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
        return isDeleted;
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     *
     * @param product The product to delete
     */
    public static void deleteProduct(Product product)
    {
        PersistenceBroker broker = null;

        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            broker.beginTransaction();
            broker.delete(product);
            broker.commitTransaction();
        }
        catch (PersistenceBrokerException ex)
        {
            if (broker != null)
            {
                broker.abortTransaction();
            }
            ex.printStackTrace();
        }
        finally
        {
            if (broker != null)
            {
                broker.close();
            }
        }
    }
View Full Code Here

Examples of org.apache.ojb.broker.PersistenceBroker

     */
    private void internalCleanup()
    {
        if(hasBroker())
        {
            PersistenceBroker broker = getBroker();
            if(log.isDebugEnabled())
            {
                log.debug("Do internal cleanup and close the internal used connection without" +
                        " closing the used broker");
            }
            ConnectionManagerIF cm = broker.serviceConnectionManager();
            if(cm.isInLocalTransaction())
            {
                /*
                arminw:
                in managed environment this call will be ignored because, the JTA transaction
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.