Hi all,
I am trying to create a reusable persistence manager Session bean that I can use across my application. Naturally, I am happy to share the code here if it is of use to anyone.
However, I would like to ask everyone her for a bit of code review in terms of the reusability of this Class, robustness of Exception handling, potential problems etc.
1. Are there any direct problems with implementing a persistence manager as a Session bean? (i.e. storing session and factory references as class memebers etc)
2. Is exception handling sufficient or can improvements be done to avoid problems?
3. Performance? What can be done to better optimize the session bean's performance?
4. Opening and closing sessions...as it is set up right now, would there be significant problems for a high volume of transactions or concurrency?
ok, here is the Bean code:
Code:
/*
* Created on Feb 6, 2004
*
*/
package com.forisent.framework.productManager.store;
import java.io.File;
import java.net.URL;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import com.forisent.framework.common.exception.PersistenceException;
import com.forisent.framework.productManager.store.factory.PersistenceFactory;
/**
* @author
*
*/
public class PersistenceManagerBean implements SessionBean{
private PersistenceManagerBean _PersistanceManager = null;
private SessionFactory _SessionFactory = null;
private Session session = null;
private Log log = null;
private void initializeLog(){
if(log == null){
log = LogFactory.getLog(PersistenceManagerBean.class);
}
}
public Object persist(Object obj) throws PersistenceException{
Session session = null;
log.info("Persisting object: " + obj.getClass().toString() + "...");
try{
session = getSession();
session.saveOrUpdate(obj);
session.flush();
}catch(HibernateException e){
log.info("Rolling Back Persist Operation");
try{
session.evict(obj);
}catch(Exception ex){
throw new PersistenceException(ex.getMessage(), ex);
}
throw new PersistenceException(e.getMessage(), e);
}finally{
try{
closeSession();
}catch(HibernateException e){
throw new PersistenceException(e.getMessage(), e);
}
}
return obj;
}
public boolean delete(Object obj) throws PersistenceException {
Session session = null;
log.info("Deleting object: " + obj.getClass().toString() + "...");
try{
session = getSession();
session.delete(obj);
session.flush();
}catch(HibernateException e){
log.info("Rolling Back Delete Operation");
try{
session.evict(obj);
}catch(Exception ex){
throw new PersistenceException(ex.getMessage(), ex);
}
throw new PersistenceException(e.getMessage(), e);
}finally{
try{
closeSession();
}catch(HibernateException e){
throw new PersistenceException(e.getMessage(), e);
}
}
return true;
}
private Session getSession() throws HibernateException{
if(session == null){
session = _SessionFactory.openSession();
}else if(!session.isConnected()){
session.reconnect();
}
return session;
}
private void closeSession() throws HibernateException{
if(session != null){
session.disconnect();
}
}
//Other EJB lifecycle methods left out...
public void ejbCreate() throws EJBException{
initializeLog();
try{
_SessionFactory = PersistenceFactory.getFactory("META-INF/hibernate.cfg.xml");
}catch(Exception e){
e.printStackTrace();
}
log.info("Connection established...");
}
}
Would be very glad if I could get some critique. Thanx!