Package javax.jdo

Examples of javax.jdo.Transaction


        insertPCPoints(pm, 5);
    }

    /** */
    protected void insertPCPoints(PersistenceManager pm, int numInsert) {
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            for(int i = 0; i<numInsert; i++) {
                Object pc = new PCPoint(i, i);
                pm.makePersistent(pc);
                inserted.add(pc);
            }
            tx.commit();
            tx = null;
            if (debug) logger.debug("Total objects inserted : " + numInsert);
        }
        finally {
            if ((tx != null) && tx.isActive())
                tx.rollback();
        }
    }
View Full Code Here


     * Persists the given pc instances.
     * @param pm the PersistenceManager
     * @param pcInstances the pc instances to persist
     */
    private void makePersistentAll(PersistenceManager pm, List pcInstances) {
        Transaction tx = pm.currentTransaction();
        tx.begin();
        try {
            pm.makePersistentAll(pcInstances);
            if (debug) logger.debug("inserted " + pcInstances);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    }
View Full Code Here

        boolean bFlag = false;
        String strValue = "";
        char charValue = '\u0000';
        int numInsert = 10;
       
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            for (int i = 1; i <= numInsert; i++ ) {
                if (i%2 == 1) {
                    bFlag = true;
                    strValue = "Odd" + i;
                    charValue = 'O';
                }
                else {
                    bFlag = false;
                    strValue = "Even" + i;
                    charValue = 'E';
                }
                PrimitiveTypes primitiveObject = new PrimitiveTypes(
                    (long)i, bFlag, new Boolean(bFlag), (byte)i, new Byte((byte)i),
                    (short)i, new Short((short)i), (int) i, new Integer(i),
                    (long)i, new Long(i), (float)i, new Float(i),
                    (double)i, new Double(i), charValue, new Character(charValue),
                    Calendar.getInstance().getTime(), strValue,
                    new BigDecimal(String.valueOf(i)),
                    new BigInteger(String.valueOf(i)),
                    new Long(i));
                pm.makePersistent(primitiveObject);
            }
            tx.commit();
            tx = null;
            if (debug) logger.debug("Total objects inserted : " + numInsert);
        }
        finally {
            if ((tx != null) && tx.isActive())
                tx.rollback();
        }
    }
View Full Code Here

     * @param positive
     */
    protected void compile(String assertion,
            Query query, String queryText, boolean positive) {
        PersistenceManager pm = getPM();
        Transaction tx = pm.currentTransaction();
        tx.begin();
        try {
            query.compile();
            if (!positive) {
                fail(assertion,
                        "Query compilation must throw JDOUserException: " +
                        queryText);
            }
        } catch (JDOUserException e) {
            if (positive) {
                fail(assertion, "Query '" + queryText +
                        "' must be compilable. The exception message is: " +
                        e.getMessage());
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    }
View Full Code Here

    private Object execute(String assertion, Query query,
            String singleStringQuery, boolean hasOrdering,
            Object parameters, Object expectedResult, boolean positive) {
        Object result = null;
        PersistenceManager pm = getPM();
        Transaction tx = pm.currentTransaction();
        tx.begin();
        try {
            try {
                if (parameters == null) {
                    result = query.execute();
                } else if (parameters instanceof Object[]) {
                    result = query.executeWithArray((Object[])parameters);
                } else if (parameters instanceof Map) {
                    result = query.executeWithMap((Map)parameters);
                } else if (parameters instanceof List) {
                    List list = (List) parameters;
                    switch (list.size()) {
                        case 1:
                            result = query.execute(list.get(0));
                            break;
                        case 2:
                            result = query.execute(list.get(0), list.get(1));
                            break;
                        case 3:
                            result = query.execute(
                                    list.get(0), list.get(1), list.get(2));
                            break;
                        default:
                            throw new IllegalArgumentException(
                                "Argument parameters is a list " +
                                "and must have 1, 2, or 3 elements.");
                    }
                } else {
                    throw new IllegalArgumentException("Argument parameters " +
                            "must be instance of List, Map, Object[], or null.");
                }
               
                if (logger.isDebugEnabled()) {
                    logger.debug("Query result: " + ConversionHelper.
                        convertObjectArrayElements(result));
                }
   
                if (positive) {
                    if (hasOrdering) {
                        checkQueryResultWithOrder(assertion, singleStringQuery, result,
                                expectedResult);
                    } else {
                        checkQueryResultWithoutOrder(assertion, singleStringQuery, result,
                                expectedResult);
                    }
                } else {
                    fail(assertion, "Query must throw JDOUserException: " +
                            singleStringQuery);
                }
            } finally {
                query.close(result);
            }
        } catch (JDOUserException e) {
            if (positive) {
                String msg = "JDOUserException thrown while executing query:\n" +
                        singleStringQuery;
                throw new JDOException(msg, e);
            }
        } catch (JDOException e) {
            String msg = "JDOException thrown while executing query:\n" +
                    singleStringQuery;
            throw new JDOException(msg, e);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        return result;
    }
View Full Code Here

    private void delete(String assertion, Query query,
            String singleStringQuery, Object parameters,
            long expectedNrOfDeletedObjects) {
        boolean positive = expectedNrOfDeletedObjects >= 0;
        PersistenceManager pm = getPM();
        Transaction tx = pm.currentTransaction();
        tx.begin();
        try {
            try {
                long nr;
                if (parameters == null) {
                    nr = query.deletePersistentAll();
                } else if (parameters instanceof Object[]) {
                    nr = query.deletePersistentAll((Object[])parameters);
                } else if (parameters instanceof Map) {
                    nr = query.deletePersistentAll((Map)parameters);
                } else {
                    throw new IllegalArgumentException("Argument parameters " +
                            "must be instance of Object[], Map, or null.");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug(nr + " objects deleted.");
                }
               
                if (positive) {
                    if (nr != expectedNrOfDeletedObjects) {
                        fail(assertion, "deletePersistentAll returned " + nr +
                                ", expected is " + expectedNrOfDeletedObjects +
                                ". Query: " + singleStringQuery);
                    }
                } else {
                    fail(assertion, "deletePersistentAll must throw JDOUserException: " +
                            singleStringQuery);
                }
            } finally {
                query.closeAll();
            }
            tx.commit();
        } catch (JDOUserException e) {
            if (positive) {
                String msg = "JDOUserException thrown while executing query:\n" +
                        singleStringQuery;
                throw new JDOException(msg, e);
            }
        } catch (JDOException e) {
            String msg = "JDOException thrown while executing query:\n" +
                    singleStringQuery;
            throw new JDOException(msg, e);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    }
View Full Code Here

    void runTest(PersistenceManager pm, CompanyModelReader reader) {
        Query query;
        Collection expected;
        Object result;
           
        Transaction tx = pm.currentTransaction();
        tx.begin();
        Department dept1 = reader.getDepartment("dept1");
        expected = new HashSet();
        expected.add(reader.getFullTimeEmployee("emp1"));
        expected.add(reader.getFullTimeEmployee("emp2"));
        expected.add(reader.getPartTimeEmployee("emp3"));
           
        // Import Department twice
        query = pm.newQuery(Employee.class);
        query.declareImports("import org.apache.jdo.tck.pc.company.Department; import org.apache.jdo.tck.pc.company.Department;");
        query.declareParameters("Department d");
        query.setFilter("department == d");
        result = query.execute(dept1);
        checkQueryResultWithoutOrder(ASSERTION_FAILED, "department == d",
                result, expected);

        // Import Department explictly and per type-import-on-demand
        query = pm.newQuery(Employee.class);
        query.declareImports("import org.apache.jdo.tck.pc.company.Department; import org.apache.jdo.tck.pc.company.*");
        query.declareParameters("Department d");
        query.setFilter("department == d");
        result = query.execute(dept1);
        checkQueryResultWithoutOrder(ASSERTION_FAILED, "department == d",
                result, expected);

        // type-import-on-demand twice
        query = pm.newQuery(Employee.class);
        query.declareImports("import org.apache.jdo.tck.pc.company.*; import org.apache.jdo.tck.pc.company.*");
        query.declareParameters("Department d");
        query.setFilter("department == d");
        result = query.execute(dept1);
        checkQueryResultWithoutOrder(ASSERTION_FAILED, "department == d",
                result, expected);

        tx.commit();
        tx = null;
    }
View Full Code Here

    }

    /** */
    private void runTestRetrieve(PersistenceManager pm) {
        createObjects(pm);
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.retrieve(p1);
            pm.retrieve(p3);
            pm.retrieve(rect);
            pm.makeTransient(p1);
            pm.makeTransient(p3);
            pm.makeTransient(rect);
            tx.commit();
            tx = null;
            checkP1();
            checkP3();
            checkRectP1();
            checkRectId();
        }
        finally {
            if ((tx != null) && tx.isActive())
                tx.rollback();
        }
    }
View Full Code Here

    }

    /** */
    private void runTestRetrieveAllWithCollection(PersistenceManager pm) {
        createObjects(pm);
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Collection coll = new ArrayList();
            coll.add(p1);
            coll.add(p3);
            coll.add(rect);
            pm.retrieveAll(coll);
            pm.makeTransientAll(coll);
            tx.commit();
            tx = null;
            checkP1();
            checkP3();
            checkRectP1();
            checkRectId();
        }
        finally {
            if ((tx != null) && tx.isActive())
                tx.rollback();
        }
    }
View Full Code Here

    }

    /** */
    private void runTestRetrieveAllWithCollectionDFGtrue(PersistenceManager pm) {
        createObjects(pm);
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Collection coll = new ArrayList();
            coll.add(p1);
            coll.add(p3);
            coll.add(rect);
            pm.retrieveAll(coll, true);
            pm.makeTransientAll(coll);
            tx.commit();
            tx = null;
            checkP1();
            checkP3();
            // checkRectP1(); p1 is not in the default fetch group by default
            checkRectId();
        }
        finally {
            if ((tx != null) && tx.isActive())
                tx.rollback();
        }
    }
View Full Code Here

TOP

Related Classes of javax.jdo.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.