You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Adam Griffiths <pu...@adam-griffiths.co.uk> on 2003/08/19 10:58:07 UTC
Editing multiple objects. Can it be done?
Hi,
I'm sure this is a common requirement, how does one have multiple objects
open for editing in the same process and be able to choose which should be
committed?
I am writing a application which can be used to open and edit multiple
Objects concurrently, using ojb's odmg api as a persistence layer. I would
like to give the user the option to 'Save changes' or 'Drop changes' which
I'm sure is a common requirement. My problem arises with how I should use
ojb/odmg transactions. My initial idea was that I should associate a
transaction with each object being concurrently edited, for example:
Transaction trx1 = odmg.newTransaction();
Transaction trx2 = odmg.newTransaction();
trx1.begin();
trx2.begin();
trx1.lock(obj1, tx.WRITE);
trx2.lock(obj2, tx.WRITE);
//...code to allow the user edit obj1 and obj2
//...code to allow user to persist either obj1 or obj2, or both or
neither.
But if I commit one transaction it is not possible to commit the other
without causing an error: "Calling method needed transaction, but no
transaction found for current thread :-(". It seems that it is possible to
persist both objects using checkpoint rather than commit:
trx1.checkpoint();
trx2.checkpoint();
But there is still a problem if the user chooses to save one of the objects
but not the other. It is desirable to release the lock on the object which
is not being saved:
trx1.abort();
[note that: trx1.lock(obj1, tx.READ) does not downgrade the lock here, it
says in the API documentation that locks can only be increased. Which is why
I use trx1.abort() to remove the lock]
However a subsequent call:
trx2.commit();
OR
trx2.checkpoint();
Fails with the same error above.
Is concurrent editing of different objects, in which you can commit (or
otherwise persist) some objects but not others, supported with ojb/odmg? I
would like to use ojb/odmg to lock and commit my objects so that my
application has the ability to warn the user when they request the lock to
edit a object and that object is already locked by another process running
my application (i.e. open for editing by another user).
Can anyone recommend a way to achieve this?
Any help or suggestions would be greatly appreciated.
Adam
P.S.
I assume my problems above are all because odmg Transactions are using a
shared org.apache.ojb.broker.PersistenceBroker... But I'm not sure if I'm
right or how to get arround the problem.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
RE: Editing multiple objects. Can it be done?
Posted by Adam Griffiths <pu...@adam-griffiths.co.uk>.
Hi again,
I have an idea to solve my problem bellow and would very grateful for any
suggestions or comments. It seems that odmg / jdo don't have great
provisions for locking and multiple transactions so here is my work around:
I believe that it's not possible to have multiple transactions open at once,
with a view to committing some of them and not others. So my solution would
be to begin a transaction, retrieve the objects I wish to view/edit and then
close the transaction before any changes are made to the objects. I can then
edit the retrieved objects and optionally save them by: Beginning a new
transaction, setting the object to dirty, locking it for writing and finally
committing the transaction
//To retrieve ojb
tx0.begin();
OQLQuery query = odmg.newOQLQuery();
query.create(oql);
allProducts = (DList) query.execute();
tx0.commit();
ojb = allProducts.get(0);
//To save ojb
tx1.begin();
((org.apache.ojb.odmg.TransactionImpl) tx1).markDirty(obj);
tx1.lock(obj, tx.WRITE);
tx1.commit();
The problem with this method is the need to call:
((org.apache.ojb.odmg.TransactionImpl) tx1).markDirty(obj);
And that objects can't be locked for writing while they are open for
editing.
Is what I'm doing a good idea or is there a better way to achieve this
functionality.
Kind regards and my thanks in advance
Adam
________________________________________________________
s_p_a_m_t_r_a_p from:ignoreme@adam-griffiths.co.uk
Do not email the above address or remove these two lines
-----Original Message-----
From: Adam Griffiths [mailto:public@adam-griffiths.co.uk]
Sent: 19 August 2003 09:58
To: 'OJB Users List'
Subject: Editing multiple objects. Can it be done?
Hi,
I'm sure this is a common requirement, how does one have multiple objects
open for editing in the same process and be able to choose which should be
committed?
I am writing a application which can be used to open and edit multiple
Objects concurrently, using ojb's odmg api as a persistence layer. I would
like to give the user the option to 'Save changes' or 'Drop changes' which
I'm sure is a common requirement. My problem arises with how I should use
ojb/odmg transactions. My initial idea was that I should associate a
transaction with each object being concurrently edited, for example:
Transaction trx1 = odmg.newTransaction();
Transaction trx2 = odmg.newTransaction();
trx1.begin();
trx2.begin();
trx1.lock(obj1, tx.WRITE);
trx2.lock(obj2, tx.WRITE);
//...code to allow the user edit obj1 and obj2
//...code to allow user to persist either obj1 or obj2, or both or
neither.
But if I commit one transaction it is not possible to commit the other
without causing an error: "Calling method needed transaction, but no
transaction found for current thread :-(". It seems that it is possible to
persist both objects using checkpoint rather than commit:
trx1.checkpoint();
trx2.checkpoint();
But there is still a problem if the user chooses to save one of the objects
but not the other. It is desirable to release the lock on the object which
is not being saved:
trx1.abort();
[note that: trx1.lock(obj1, tx.READ) does not downgrade the lock here, it
says in the API documentation that locks can only be increased. Which is why
I use trx1.abort() to remove the lock]
However a subsequent call:
trx2.commit();
OR
trx2.checkpoint();
Fails with the same error above.
Is concurrent editing of different objects, in which you can commit (or
otherwise persist) some objects but not others, supported with ojb/odmg? I
would like to use ojb/odmg to lock and commit my objects so that my
application has the ability to warn the user when they request the lock to
edit a object and that object is already locked by another process running
my application (i.e. open for editing by another user).
Can anyone recommend a way to achieve this?
Any help or suggestions would be greatly appreciated.
Adam
P.S.
I assume my problems above are all because odmg Transactions are using a
shared org.apache.ojb.broker.PersistenceBroker... But I'm not sure if I'm
right or how to get arround the problem.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org