Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manipulating Entity Beans: Strategy vs. Facade

 
James MacLean
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently attempted manipulating entity beans using the GoF Strategy Pattern, and it worked! Instead of hiding my entities behind a stateless session facade, I used a stateful session strategy. My question is: "How will this impact performance, and by how much?" Naturally, it will be much easier to add and/or remove entity beans; however, if there is a significant performance cut associated with this then I would not bother. I have posted the bean class below. Thanks in advance!

PersistenceManagerBean.java:
package ejb;
import framework.*;
import interfaces.*;
import vo.*;
import util.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;
import java.util.*;
import java.security.*;
import org.apache.log4j.*;
public class PersistenceManagerBean implements IPersistenceManager, SessionBean {

private SessionContext context;
private Logger log = Logger.getLogger(PersistenceManagerBean.class);

private IPersistenceAppService pas;
private String pasName;

public Object create(Object pk, IValue vo) throws PersistenceManagerException {
try {
this.log.info("Creating entity");
return this.pas.create(pk, vo);
} catch (CreateException e) {
this.log.warn("Unable to create entity");
throw new PersistenceManagerException(e);
}
}

public IValue read(Object pk) throws PersistenceManagerException {
try {
return this.pas.read(pk);
} catch (FinderException e) {
this.log.warn("Unable to read entity");
throw new PersistenceManagerException(e);
}
}

public void update(Object pk, IValue vo) throws PersistenceManagerException {
try {
this.pas.update(pk, vo);
} catch (FinderException e) {
this.log.warn("Unable to update entity");
throw new PersistenceManagerException(e);
}
}

public void delete(Object pk) throws PersistenceManagerException {
try {
this.pas.delete(pk);
} catch (RemoveException e) {
this.log.warn("Unable to delete entity");
throw new PersistenceManagerException(e);
}
}

public void ejbCreate(String pasName) throws CreateException {
try {
Class classDefinition = Class.forName(pasName);
this.pas = (IPersistenceAppService)classDefinition.newInstance();
this.pasName = pasName;
} catch (InstantiationException e) {
throw new CreateException();
} catch (IllegalAccessException e) {
throw new CreateException();
} catch (ClassNotFoundException e) {
throw new CreateException();
}
}

public void ejbActivate() throws EJBException {
try {
this.log = Logger.getLogger(PersistenceManagerBean.class);
Class classDefinition = Class.forName(this.pasName);
this.pas = (IPersistenceAppService)classDefinition.newInstance();
} catch (InstantiationException e) {
throw new EJBException(e);
} catch (IllegalAccessException e) {
throw new EJBException(e);
} catch (ClassNotFoundException e) {
throw new EJBException(e);
}
}

public void ejbPassivate() throws EJBException {
this.log = null;
this.pas = null;
}

public void ejbRemove() throws EJBException {
}

public void setSessionContext(SessionContext context) throws EJBException {
this.context = context;
}
}
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James MacLean:
"How will this impact performance, and by how much?"

Use a profiling tool to measure performance rather than guessing.
That said, I prefer the stateless session bean facade, since they hold no conversational state and are not tied to a particular client.
I am not averse to the design patterns... there is a time and place for everything.
Of course YMMV.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic