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 Farnea Massimiliano <ma...@atscom.it> on 2003/03/06 12:58:04 UTC
NEWBIE: cannot update object with ODMG
I'm testing the ODMG layer with this example:
/**
* Update the Traente instance on the database
*/
public void update() {
Database db = odmg.newDatabase(); // the current DB
Transaction tx = null;
//open database
try {
db.open("default", Database.OPEN_READ_WRITE);
} catch (ODMGException ex) {
ex.printStackTrace();
}
tx = odmg.newTransaction();
String oqlQuery = "select edit from " +
traente.className +
" where id = " + traente.getId();
try {
tx = odmg.newTransaction();
tx.begin();
OQLQuery query = odmg.newOQLQuery();
query.create(oqlQuery);
DList result = (DList) query.execute();
Traente toBeEdited = (Traente) result.get(0);
tx.lock(toBeEdited, Transaction.UPGRADE);
toBeEdited = traente;
tx.commit();
} catch (Throwable t) {
// rollback in case of errors
tx.abort();
t.printStackTrace();
}finally {
try {
db.close();
System.out.println("DB closed!!!!");
} catch (ODMGException ex) {
ex.printStackTrace();
}
}
}
where 'traente' is a class attribute that rapresents the updated object that
i would like to persist.
After the method invocation .... the database still contains the old object,
while retrieving again the reference 'toBeEdited' it seems to be updated.
What do I miss? I'm using a test class on JBuilder, without any particular
deployement enviroment.
Thanks in advance
Massimiliano Farnea
Re: NEWBIE: cannot update object with ODMG
Posted by Guido Beutler <gu...@hrs.de>.
Hi,
----- Original Message -----
From: "Farnea Massimiliano" <ma...@atscom.it>
To: <oj...@db.apache.org>
Sent: Thursday, March 06, 2003 12:58 PM
Subject: NEWBIE: cannot update object with ODMG
> I'm testing the ODMG layer with this example:
>
> /**
> * Update the Traente instance on the database
> */
>
> public void update() {
>
> Database db = odmg.newDatabase(); // the current DB
> Transaction tx = null;
> //open database
> try {
> db.open("default", Database.OPEN_READ_WRITE);
> } catch (ODMGException ex) {
> ex.printStackTrace();
> }
>
> tx = odmg.newTransaction();
> String oqlQuery = "select edit from " +
> traente.className +
> " where id = " + traente.getId();
> try {
> tx = odmg.newTransaction();
you got 2 transactions now. I think you need only one.
> tx.begin();
>
> OQLQuery query = odmg.newOQLQuery();
> query.create(oqlQuery);
> DList result = (DList) query.execute();
> Traente toBeEdited = (Traente) result.get(0);
>
> tx.lock(toBeEdited, Transaction.UPGRADE);
At the tutorial page the OJB crew uses
tx.lock(toBeEdited, Transaction.WRITE);
for updating values.
>
> toBeEdited = traente;
what's traente at that point?
> tx.commit();
second transaction is commited but the first one is still in use.
cheers,
Guido
> } catch (Throwable t) {
> // rollback in case of errors
> tx.abort();
> t.printStackTrace();
> }finally {
> try {
> db.close();
> System.out.println("DB closed!!!!");
> } catch (ODMGException ex) {
> ex.printStackTrace();
> }
> }
> }
>
>
> where 'traente' is a class attribute that rapresents the updated object
that
> i would like to persist.
>
> After the method invocation .... the database still contains the old
object,
> while retrieving again the reference 'toBeEdited' it seems to be updated.
> What do I miss? I'm using a test class on JBuilder, without any particular
> deployement enviroment.
>
> Thanks in advance
>
> Massimiliano Farnea
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>