Yes, you're right I'm getting a new database.
db.remove(totoBean); works finely.

In fact I'm trying to write a facade above Castor. What I want is to directly call bean.create(), bean.delete() or bean.update().
For example, bean.delete() calls the following method on a PersistenceManager:
public void delete(Bean aBean) throws PersistenceException {
Database db = null;
try {
db = getDatabase();
if (!db.isActive()) {
// no current transaction
db.begin();
db.remove(aBean);
db.commit();
} else {
db.remove(aBean);
}
<} ... catch statements>
} finally {
// close the database
try {
db.close();
} catch (Exception e) {
// do nothing
}
}
}
and I plan to use it this way:

UserTransaction ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
ut.begin();
bean1.create();
bean2.create();
bean3.create();
ut.commit();

The problem was that I retrieved the JDO instance from the JNDI directory and called getDatabase() each time I needed a Database object.

Now I've made the Database instance available via JNDI instead of the JDO one. ==> OK, I'm always using the same Database instance.
The problem I'm having now is with the "db.close()" statement. I don't really know when to call it. In the above example, the first call to create() works finely but the second one raises a
Moreover, as you mentionned, there's a concurrency problem which I've not yet worked on.

Could you please give me clues on how you implemented your facade?
Thanks in advance,

Cedrick

From: Micka�l Guessant <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: Re: [castor-dev] problem while querying then deleting an object
Date: Fri, 20 Dec 2002 10:32:02 +0100

Hi,

You probably get a new Database object in your delete() method,
right ? If so, the new db will not know about your totoBean =>
ObjectNotPersistent.

What happens if you just use db.remove(totoBean) ?

I had to implement a ThreadLocal object to keep the db objects
to avoid this problem in a facade above Castor I coded for one of my
clients.

Another (not so clean) solution would be to pass the db object to each method.

Cdrick Laballery wrote:
I'm meeting a problem with the following code:

<========== example ==========>
<assume ctx is a valid initial context>

UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
// begin the transaction
ut.begin();

JDO jdo = (JDO) ctx.lookup("java:comp/jdo/myJdo");
Database db = jdo.getDatabase();

OQLQuery oql = db.getOQLQuery("select b from test.TotoBean b");

QueryResults results = oql.execute();
while (results.hasMore()) {
totoBean = (TotoBean)results.next();
log.debug(totoBean.toString());
}
// delete the last found bean
totoBean.delete(); ==> raises an exception

ut.commit();
db.close();
<========== end of example ==========>

totoBean.delete() causes a ObjectNotPersistentException:
org.exolab.castor.jdo.ObjectNotPersistentException: The object of type test.TotoBean is not persistent -- it was not queried or created within this transaction
The fact is that this object has obviously been created within the transaction...
===> What's wrong with my code ?

===> One more thing: if I begin the transaction *after* getting the database, the call to oql.execute() raises a TransactionNotInProgressException. Is it normal ?

Thanks in advance for your help.

Cedrick

_________________________________________________________________
MSN Search, le moteur de recherche qui pense comme vous ! http://search.msn.fr/worldwide.asp

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev




--
Mickael Guessant
Technical Consultant
http://mguessan.free.fr
mailto:[EMAIL PROTECTED]

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

_________________________________________________________________
MSN Messenger : discutez en direct avec vos amis ! http://www.msn.fr/msger/default.asp

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

Reply via email to