Package org.apache.ojb.otm.core

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


    private void createTestData() throws TransactionException, LockingException
    {
        for (int i = 0; i < COUNT; i++)
        {
            Transaction tx = _kit.getTransaction(_conn);
            tx.begin();
            Contract contract = new Contract();
            contract.setPk("C" + TIME);
            contract.setContractValue1("contractvalue1");
            contract.setContractValue2(1);
            contract.setContractValue3("contractvalue3");
            contract.setContractValue4(new Timestamp(TIME));
            _conn.makePersistent(contract);
            tx.commit();
            tx = _kit.getTransaction(_conn);
            tx.begin();
            Version version = new Version();
            version.setPk("V" + TIME);
            version.setVersionValue1("versionvalue1");
            version.setVersionValue2(1);
            version.setVersionValue3(new Timestamp(TIME));
            version.setContract(contract);
            _conn.makePersistent(version);
            tx.commit();
            tx = _kit.getTransaction(_conn);
            tx.begin();
            Effectiveness eff = new Effectiveness();
            eff.setPk("E" + TIME);
            eff.setEffValue1("effvalue1");
            eff.setEffValue2(1);
            eff.setEffValue3(new Timestamp(TIME));
            eff.setVersion(version);
            _conn.makePersistent(eff);
            tx.commit();
        }
    }
View Full Code Here


        Address address3 = new Address("Brasil", "Rio de Janeiro", "Rua Professor Azevedo Marques");
        Criteria emptyCriteria = new Criteria();
        Query q;
        Iterator it;

        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        // prepare tables for the test - empty them
        q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        q = QueryFactory.newQuery(Person.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        q = QueryFactory.newQuery(Address.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        tx.commit();

        person.setMainAddress(address1);
        person.addOtherAddress("work", address2);
        person.addOtherAddress("dream", address3);

        tx = _kit.getTransaction(_conn);
        tx.begin();
        // Cascade create
        _conn.makePersistent(person);
        tx.commit();

        Identity oid = _conn.getIdentity(person);

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        person = (Person) _conn.getObjectByIdentity(oid);
        assertTrue("person exists", (person != null));
        assertTrue("main Address exists", (person.getMainAddress() != null));
        assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
        assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
        AddressDesc desc1 = (AddressDesc) person.getOtherAddresses().get(0);
        assertEquals("1st other Address has correct description", "work", desc1.getDesc());
        assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
        AddressDesc desc2 = (AddressDesc) person.getOtherAddresses().get(1);
        assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
        assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());

        // Delete dependent
        person.setMainAddress(null);
        person.getOtherAddresses().remove(1);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        person = (Person) _conn.getObjectByIdentity(oid);
        assertTrue("main Address doesn't exist", (person.getMainAddress() == null));
        assertEquals("one other Address", 1, person.getOtherAddresses().size());
        desc2 = (AddressDesc) person.getOtherAddresses().get(0);
        assertEquals("the other Address has correct description", "work", desc1.getDesc());
        assertEquals("the other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());

        // Create dependent
        person.setMainAddress(address1);
        person.addOtherAddress("dream", address3);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        person = (Person) _conn.getObjectByIdentity(oid);
        assertTrue("main Address exists", (person.getMainAddress() != null));
        assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
        assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
        desc1 = (AddressDesc) person.getOtherAddresses().get(0);
        assertEquals("1st other Address has correct description", "work", desc1.getDesc());
        assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
        desc2 = (AddressDesc) person.getOtherAddresses().get(1);
        assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
        assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());

        // Cascade delete
        _conn.deletePersistent(person);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        person = (Person) _conn.getObjectByIdentity(oid);
        assertTrue("person doesn't exist", (person == null));
        q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
        it = _conn.getIteratorByQuery(q);
        assertTrue("address descriptions don't exist", !it.hasNext());
        q = QueryFactory.newQuery(Address.class, emptyCriteria);
        it = _conn.getIteratorByQuery(q);
        assertTrue("addresses don't exist", !it.hasNext());
        tx.commit();
    }
View Full Code Here

        Address2 address = new Address2();
        Criteria emptyCriteria = new Criteria();
        Query q;
        Iterator it;

        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        // prepare tables for the test - empty them
        q = QueryFactory.newQuery(Debitor.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        q = QueryFactory.newQuery(Address2.class, emptyCriteria);
        for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
            _conn.deletePersistent(it.next());
        }
        tx.commit();

        person.getAddresses().add(address);
        debitor.setAbstractPerson(person);

        tx = _kit.getTransaction(_conn);
        tx.begin();
        // Cascade create
        _conn.makePersistent(debitor);
        tx.commit();

        Identity debitorOid = _conn.getIdentity(debitor);
        Identity personOid = _conn.getIdentity(person);
        int addrId = address.getId();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
        assertNotNull("debitor does not exist", debitor);
        person = debitor.getAbstractPerson();
        assertNotNull("person does not exist", person);
        assertTrue("person has not the expected type", (person instanceof LegalPerson));
        assertEquals("address does not exist", 1, person.getAddresses().size());
        address = (Address2) person.getAddresses().iterator().next();
        assertEquals("addressid is not correct", addrId, address.getId());

        // Delete dependent
        person.getAddresses().clear();
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
        person = (LegalPerson) debitor.getAbstractPerson();
        assertEquals("address was not deleted", person.getAddresses().size(), 0);

        // Create dependent
        person.getAddresses().add(address);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
        person = (LegalPerson) debitor.getAbstractPerson();
        assertEquals("address does not exist", person.getAddresses().size(), 1);
        tx.commit();

        // Change dependent reference, should delete old dependant
        person = new NaturalPerson();
        person.setName("before");
        debitor.setAbstractPerson(person);

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(debitor);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        assertTrue("old person has not been deleted", (_conn.getObjectByIdentity(personOid) == null));
        q = QueryFactory.newQuery(Address2.class, emptyCriteria);
        it = _conn.getIteratorByQuery(q);
        assertTrue("old address has not been deleted", !it.hasNext());
        person = debitor.getAbstractPerson();
        assertTrue("new person has unexpected type", (person instanceof NaturalPerson));
        assertTrue("person does not have correct name", "before".equals(person.getName()));
        tx.commit();

        person.setName("after");
        assertTrue("name of person was not saved", "after".equals(debitor.getAbstractPerson().getName()));

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(debitor);
        tx.commit();
        assertTrue("name of person was not saved: " + debitor.getAbstractPerson().getName(),
                   "after".equals(debitor.getAbstractPerson().getName()));

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
        person = debitor.getAbstractPerson();
        assertTrue("name of person was not saved: " + debitor.getAbstractPerson().getName(),
                   "after".equals(debitor.getAbstractPerson().getName()));
        // Cascade delete
        _conn.deletePersistent(debitor);
        tx.commit();

        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
        assertNull("debitor still exists", debitor);
        q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
        it = _conn.getIteratorByQuery(q);
        assertTrue("persons still exist", !it.hasNext());
        tx.commit();
    }
View Full Code Here

     * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
     */
    protected void deleteArticles() throws Exception
    {
        long start = System.currentTimeMillis();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        for (int i = 0; i < articleCount; i++)
        {
            _conn.deletePersistent(arr[i]);
        }
        tx.commit();
        long stop = System.currentTimeMillis();
        logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
    }
View Full Code Here

     */
    protected void insertNewArticles() throws Exception
    {
        long start = System.currentTimeMillis();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        for (int i = 0; i < articleCount; i++)
        {
            _conn.makePersistent(arr[i]);
        }
        tx.commit();
        long stop = System.currentTimeMillis();
        logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
    }
View Full Code Here

     * The lookup is done one by one, that is: a primary key based lookup is used.
     */
    protected void readArticles() throws Exception
    {
        long start = System.currentTimeMillis();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        for (int i = 0; i < articleCount; i++)
        {
            Object[] pks = {new Integer(offsetId + i)};
            Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks);
            PerformanceArticle a = (PerformanceArticle) _conn.getObjectByIdentity(oid, LockType.NO_LOCK);
        }
        tx.commit();
        long stop = System.currentTimeMillis();
        logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
    }
View Full Code Here

    protected void readArticlesByCursor() throws Exception
    {
        // clear the cache
        _conn.invalidateAll();

        Transaction tx = _kit.getTransaction(_conn);
        Criteria c = new Criteria();
        c.addBetween("articleId", new Integer(offsetId), new Integer(offsetId + articleCount));
        Query q = new QueryByCriteria(PerformanceArticle.class, c);
        long start = System.currentTimeMillis();
    tx.begin();
        Iterator iter = _conn.getIteratorByQuery(q, LockType.NO_LOCK);
        int fetchCount = 0;
        while (iter.hasNext())
        {
            fetchCount++;
            PerformanceArticle a = (PerformanceArticle) iter.next();
        }
    tx.commit();
        long stop = System.currentTimeMillis();
        logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
    }
View Full Code Here

     * All objects are modified and changes are written to the RDBMS with an UPDATE.
     */
    protected void updateExistingArticles() throws Exception
    {
        long start = System.currentTimeMillis();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        // update all objects
        for (int i = 0; i < articleCount; i++)
        {
            _conn.lockForWrite(arr[i]);
            arr[i].setPrice(arr[i].getPrice() * 1.95583);
        }
        tx.commit();
        long stop = System.currentTimeMillis();
        logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
    }
View Full Code Here

        Query q;
        Iterator iter;
        /**
        * delete effectiveness first
        */
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Effectiveness.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
        /**
        * then version
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Version.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
        /**
        * the contract
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Contract.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
    }
View Full Code Here

    public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
    {
        clearTestData();
        TestClassA a = generateTestData();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(a.getB());
        _conn.makePersistent(a);
        tx.commit();
        /**
        * clear to start test
        */
        _conn.invalidateAll();
        /**
        * get A to make it and the related B in cache
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity oid = _conn.getIdentity(a);
        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a1.getB() != null);
        assertTrue(a1.getB().getValue1().equals("hi there"));
        /**
        * everything is good, update b
        */
        tx.commit();

        /**
        * now get B and update it, do NOT get it by traversing A
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity boid = _conn.getIdentity(a.getB());
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
        assertTrue(b1 != null);
        assertTrue(b1.getValue1().equals("hi there"));
        /**
        * everything is good, update b
        */
        _conn.lockForWrite(b1);
        b1.setValue1("goodbye there");
        tx.commit();
        /**
        * make sure b was updated
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        boid = _conn.getIdentity(a.getB());
        b1 = (TestClassB) _conn.getObjectByIdentity(boid);
        assertTrue(b1 != null);
        assertTrue(b1.getValue1().equals("goodbye there"));
        tx.commit();

        /**
        * now get A again and make sure the related B is updated to reflect
        * the new value.
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a2.getB() != null);
        assertTrue(a2.getB().getValue1().equals("goodbye there"));
        tx.commit();
        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.