I am relatively fresh using OJB and the ODMG API, and I am not sure to understand your problem, but as far as I have understood it:
- you may not have more than one transaction active per thread (well, one Transaction object bound to one thread)
- every object modified within a thread, starting from the beginning of the transaction and ending with the commit, known as persistent, will be saved to the database
So, if you do not want to persist _all_ of the objects that have been modified, you could:
- retrieve all the needed objects and make clones of them in a first transaction,
- do your work with the clones,
- and at the end open a second transaction, retrieve all objects that you have chosen to be persisted with their changes, apply the changes taking the clones as reference, and commit the changes.
Anyway, I guess you could make it with spawning a thread per object, and acquiring a Transaction object for each thread (that is the way it is supposed to work in a web or ejb container), but that would make your app more complex than the above (I think...). More than that, I do not know how long a transaction may remain opened.
If I did wrote something wrong, you are welcome to correct me.
HTH
-- Philippe Hacquin [EMAIL PROTECTED] 06 82 79 93 65
Adam Griffiths wrote:
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:[EMAIL PROTECTED]
Do not email the above address or remove these two lines
-----Original Message-----
From: Adam Griffiths [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
