db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joerg Heinicke <joerg.heini...@gmx.de>
Subject [OTM] bug with most simple use case
Date Mon, 22 Mar 2004 14:37:07 GMT
Hello,

I have found another bug in OJB OTM when doing long transactions. What's strange
about it is that it is probably the most simple use case.

The symptom: I can create and delete persistent objects, but I can not update
them. First I retrieve the objects via getCollectionByQuery(). Later I iterate
over the old Collection and call deletePersistent() for no longer existing
objects, and iterate over the new Collection and call makePersistent() for new
and updated objects.

When doing remote debugging I see that the objects have the correct values
immediately before makePersistent() - and after it. But no update on the
database is done (through profileSql=true for MySQL I can see the fired SQL
statements). Even the next call to getCollectionByQuery() returns the objects
with the correct values that are not in the database though a SELECT on the
database is done.

Please find the test case below. If you remove the conn.invalidateAll() you will
see the expected behaviour, no error is reported. But when not getting the
objects from the cache you see that no update was done.

Thanks in advance,

Joerg

    public void testSomethingSimple() throws Throwable {
        Collection addresses = this.getAddresses();
        assertTrue("Collection of addresses is not empty. ",
                   addresses.isEmpty());

        addresses.add(new Address("oldCountry", "oldCity", "oldStreet"));

        addresses = this.updateAddresses(addresses);

        Iterator iter = addresses.iterator();
        while (iter.hasNext()) {
            Address address = (Address)iter.next();
            address.setStreet("newStreet");
        }

        addresses = this.updateAddresses(addresses);
        addresses = this.getAddresses();

        iter = addresses.iterator();
        while (iter.hasNext()) {
            Address address = (Address)iter.next();
            assertEquals("New street not set.", "newStreet",
                                                address.getStreet());
        }
    }

    private Collection getAddresses() throws Throwable {
        Transaction tx = null;
        Collection addresses;
        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidateAll();
            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
            addresses = _conn.getCollectionByQuery(q);
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
        }
        return addresses;
    }

    private Collection updateAddresses(Collection newAddresses)
    throws Throwable {
        Transaction tx = null;
        Collection oldAddresses;
        try {
            tx = _kit.getTransaction(_conn);
            tx.begin();
        
            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
            oldAddresses = _conn.getCollectionByQuery(q);
        
            Iterator oldAddressesIterator = oldAddresses.iterator();
            while (oldAddressesIterator.hasNext()) {
                Address oldAddress = (Address)oldAddressesIterator.next();
                if (!newAddresses.contains(oldAddress)) {
                    _conn.deletePersistent(oldAddress);
                }
            }
        
            Iterator newAddressesIterator = newAddresses.iterator();
            while (newAddressesIterator.hasNext()) {
                Address newAddress = (Address)newAddressesIterator.next();
                _conn.makePersistent(newAddress);
            }
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            throw e;
        }
        return newAddresses;
    }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message