Olá pessoal, estou com um intermitente problema com uma bendita sessionFactory, que onde tudo o que eu faço resulta numa excessão: org.hibernate.HibernateException: No CurrentSessionContext configured!
SessionFactory:
package br.com.agets.util;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class SessionFactoryManager implements ServletContextListener {
private static Log log = LogFactory.getLog(SessionFactoryManager.class);
public void contextInitialized(ServletContextEvent sce) {
try {
Configuration cfg = new AnnotationConfiguration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
HibernateUtil.setSessionFactory(sf);
log.info("SessionFactory inicializada");
} catch (HibernateException hbe) {
log.fatal("Impossivel abrir SessionFactory", hbe);
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
SessionFactory sf = HibernateUtil.getSessionFactory();
if (!sf.isClosed())
sf.close();
} catch (HibernateException hbe) {
log.error("Impossivel fechar SessionFactory", hbe);
}
}
}
HibernateUtil
package br.com.agets.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setSessionFactory(SessionFactory aSessionFactory) {
sessionFactory = aSessionFactory;
}
public static Session getManagedSession(boolean forceTransaction) {
Session s = sessionFactory.getCurrentSession();
if (forceTransaction && !s.getTransaction().isActive()) {
s.beginTransaction();
}
return s;
}
public static Session getManagedSession() {
return getManagedSession(true);
}
public static Transaction getTransaction() {
return getManagedSession(false).getTransaction();
}
}
Filtro para transações:
package br.com.agets.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;
import br.com.agets.util.HibernateUtil;
public class HibernateRequestFilter implements Filter {
private static Log log = LogFactory.getLog(HibernateRequestFilter.class);
public void init(FilterConfig config) throws ServletException {
log.info("HibernateRequestFilter inicializado");
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
boolean tudoOk = false;
try {
chain.doFilter(req, resp);
tudoOk = true;
} catch (ServletException e) {
log.warn("abortando transacao", e.getRootCause() == null ? e : e.getRootCause());
throw e;
} catch (IOException e) {
log.warn("abortando transacao", e);
throw e;
} catch (RuntimeException e) {
log.warn("abortando transacao", e);
throw e;
} finally {
Transaction tx = HibernateUtil.getTransaction();
if (tx.isActive()) {
if (tudoOk)
tx.commit();
else
tx.rollback();
}
}
}
public void destroy() {
}
}
Esta classe leva(pelo menos tenta) as sessões às classes:
package br.com.agets.dao.hibernate;
import org.hibernate.Session;
import br.com.agets.dao.AtendimentoDAO;
import br.com.agets.dao.DAOFactory;
import br.com.agets.dao.DoadorDAO;
import br.com.agets.dao.ReceptorDAO;
import br.com.agets.util.HibernateUtil;
public class HibernateDAOFactory extends DAOFactory {
private Session session = HibernateUtil.getSessionFactory().openSession();
@Override
public DoadorDAO getDoadorDAO() {
return new HibernateDoadorDAO(session);
}
@Override
public AtendimentoDAO getAtendimentoDAO() {
return new HibernateAtendimentoDAO(session);
}
@Override
public ReceptorDAO getReceptorDAO() {
return new HibernateReceptorDAO(session);
}
}
Todos os DAO’S Extendem o DAO Abaixo, se ele estiver errado, nada vai persistir:
package br.com.agets.dao.hibernate;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Session;
import br.com.agets.dao.GenericDAO;
public abstract class HibernateGenericDAO<T> implements GenericDAO<T> {
protected Session session;
private Class<T> persistentClass;
@SuppressWarnings("unchecked")
public HibernateGenericDAO(Session session) {
this.session = session;
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Session getSession() {
return this.session;
}
@SuppressWarnings("unchecked")
public T pesquisarPorId(Serializable id) {
return (T) this.session.get(this.persistentClass, id);
}
@SuppressWarnings("unchecked")
public List<T> listarTodos() {
return this.session.createCriteria(this.persistentClass).list();
}
@SuppressWarnings("unchecked")
public T salvar(T entidade) {
return (T) this.session.merge(entidade);
}
public void excluir(T entidade) {
this.session.delete(entidade);
}
}
Preciso muito de uma mão aqui, seria muito grato se houvesse alguma alma caridosa disposta a ajudar.