Hello there.

I'm fairly new to OJB.  I've read several of the tutorials and I'm working
on creating an application from scratch.

Below are three classes that I wrote.  One is my DAO class, the second class
is an inversion-of-control interface that I'm using to populate the object
after locking it in the DAO class, and the last class is a Struts action
which using the DAO class and provides an implementation of the
UserConfigurator interface.

I was wondering if anyone could provide some feedback and opinions about the
code.  I'm mainly curious about the following:

1) The exception handling code is omitted from the examples for the sake of
brevity.  Is the manner in which I'm handling exceptions correct?

2) I've written my retrieval routine to use a persistence broker query.
Using persistence broker queries in this manner is apparently faster.  In a
reply in another thread, I read...

"Philippe Hacquin" <[EMAIL PROTECTED]> wrote in message
news:<[EMAIL PROTECTED]>...
> ... and they are much more efficient, too, because they use the cache.

This confuses me somewhat.  Does this mean that the ODMG personality of OJB
doesn't use caching?  Or do OQL queries bypass caching?

3) I've chose to perform object deletion using the persistence broker
instead of database.deletePersistent.  What is the consequence of using one
over the other?

4) The PB transaction mechanism uses database locks.  The ODMG personality
uses object locks.  Yet, the ODMG personality is less performant than the PB
personality (according to the performance page on the web site.)  Last time
I checked, memory operations were less expensive than database I/O--so I
would have expected ODMG to out-perform PB.  Can anyone explain this
apparent contradiction?

5) I've seen an application that uses OJB that performs updates directly
against the database using JDBC.  I suspect that the implication is that
cached objects can become out of sync with data in the database.  Does OJB
have a means by which to detect that a row has been changed by another user?

6) The auto-retrieve attribute must (apparently) be set to true for ODMG.
This seems a little evil in that I will always be retrieving a graph of
objects instead of the single object in which I'm interested.  The
application mentioned in question 5 has severe performance problems related
to database access; I suspect that the root of the performance problems are
being caused by having auto-retrieve set to true for several key entities in
the application.  What happens to the behaviour of the ODMG implementation
if auto-retrieve is set to false?  (It suggests that this is a no-no on the
web site, but doesn't elaborate on what problems will arise.)  Should I be
switching to the PB personality of OJB to avoid problems?

7) Why would I choose to use the ODMG personality over the PB personality?
I've seen some (rather unhelpful) replies saying "it depends on your needs"
and I'm looking for something a little more insightful.  What does using the
ODMG personality get me?  (I'm not sure what "real Object Transactions" are
supposed to be, but that is one of the reasons cited on the web site.)

Thanks very much for your precious time.  It is greatly appreciated.

UserDao.java:
package catalyst.dao;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryFactory;
import org.apache.ojb.odmg.HasBroker;
import org.apache.ojb.odmg.OJB;
import org.odmg.Implementation;
import org.odmg.Transaction;
import catalyst.domain.User;
public class UserDao {
private static UserDao instance = new UserDao();
private UserDao() {
}
public static UserDao getInstance() {
return instance;
}
private User findUser(Integer userId) throws DaoException {
Implementation odmg = OJB.getInstance();
Transaction tx = odmg.newTransaction();
tx.begin();
try {
PersistenceBroker broker = ((HasBroker) tx).getBroker();
Criteria criteria = new Criteria();
criteria.addEqualTo("userId", userId);
Query query = QueryFactory.newQuery(User.class, criteria);
User user = (User) broker.getObjectByQuery(query);
tx.commit();
return user;
}
catch (Throwable t) {
tx.abort();
throw new DaoException(t);
}
}
private void storeUser(User user, UserConfigurator configurator) throws
DaoException {
Implementation implementation = OJB.getInstance();
Transaction tx = implementation.newTransaction();
tx.begin();
try {
tx.lock(user, Transaction.WRITE);
configurator.configure(user);
tx.commit();
}
catch (Throwable t) {
tx.abort();
throw new DaoException(t);
}
}
public void createUser(UserConfigurator configurator) throws DaoException {
storeUser(new User(), configurator);
}
public void updateUser(Integer userId, UserConfigurator configurator) throws
DaoException {
storeUser(findUser(userId), configurator);
}
public void deleteUser(Integer userId) throws DaoException {
Implementation odmg = OJB.getInstance();
Transaction tx = odmg.newTransaction();
tx.begin();
try {
PersistenceBroker broker = ((HasBroker) tx).getBroker();
Criteria criteria = new Criteria();
criteria.addEqualTo("userId", userId);
Query query = QueryFactory.newQuery(User.class, criteria);
broker.deleteByQuery(query);
tx.commit();
}
catch (Throwable t) {
tx.abort();
throw new DaoException(t);
}
}
}

UserConfigurator.java
package catalyst.dao;
import catalyst.domain.User;
public interface UserConfigurator {
public void configure(User user);
}

UpdateUserAction.java:
package catalyst.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import catalyst.dao.UserConfigurator;
import catalyst.dao.UserDao;
import catalyst.domain.User;
public class UpdateUserAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
final String _username = BeanUtils.getProperty(form, "username");
final String _password = BeanUtils.getProperty(form, "password");
final String _firstName = BeanUtils.getProperty(form, "firstName");
final String _lastName = BeanUtils.getProperty(form, "lastName");
final String _language = BeanUtils.getProperty(form, "language");
Integer userId = Integer.valueOf(BeanUtils.getProperty(form, "userId"));
UserDao.getInstance().updateUser(userId, new UserConfigurator() {
public void configure(User user) {
user.setUsername(_username);
user.setPassword(_password);
user.setFirstName(_firstName);
user.setLastName(_lastName);
user.setLanguage(_language);
}
});
return mapping.findForward("success");
}
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to