falaei galera, tudo bem!?!?
desculpe minha ignorância, eu sou novo no java e jsf e minhas dúvidas persistem mesmo após ler o post, espero que possam me ajudar.
na parte de cadastrar e listar ta tudo OK, mas sempre que tento deletar ou update estoura erro de duas conexões abertas e se eu a fecho, estoura erro de sessão fechada . nisso, eu encontrei esse post e acho que me serve.
eu não estou conseguindo visualizar onde que é feita a chamada do Filtro ou aonde que ele é chamado. alguém pode me ajudar?? to postando meu HibernateUtil e meu GenericDao.
o filtro que eu copiei com base no que li aqui no post.
public class FiltroDao implements Filter{
private SessionFactory sf;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
try {
sf.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sf.getCurrentSession().getTransaction().commit();
} catch (StaleObjectStateException staleEx) {
// TODO: handle exception
staleEx.printStackTrace();
throw staleEx;
} catch (Throwable ex){
ex.printStackTrace();
try {
if(sf.getCurrentSession().getTransaction().isActive())
sf.getCurrentSession().getTransaction().rollback();
} catch (Throwable rbEx) {
// TODO: handle exception
rbEx.printStackTrace();
}
throw new ServletException(ex);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
sf = HibernateUtil.getSessionFactory();
}
}
meu HibernateUtil
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure("config/postgresql_hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
meu GenericDao
public class GenericDao {
Session s;
Transaction t;
Query query;
public void create(Object o)throws Exception{
s = HibernateUtil.getSessionFactory().openSession();
t = s.beginTransaction();
s.save(o);
t.commit();
s.close();
}
public void excluir(Object o)throws Exception{
s = HibernateUtil.getSessionFactory().openSession();
t = s.beginTransaction();
s.delete(o);
t.commit();
s.close();
}
public void update(Object o)throws Exception{
s = HibernateUtil.getSessionFactory().openSession();
t = s.beginTransaction();
s.update(o);
t.commit();
s.close();
}
public List<Cliente> findByName(String nome) throws Exception{
s = HibernateUtil.getSessionFactory().openSession();
Criteria cc = s.createCriteria(Cliente.class);
cc.add(Expression.like("nome", "%" + nome + "%"));
List<Cliente> lc = cc.list();
s.close();
return lc;
}
public Cliente findByCod(Integer cod)throws Exception {
// TODO Auto-generated method stub
s = HibernateUtil.getSessionFactory().openSession();
Cliente c = (Cliente) s.get(Cliente.class, cod);
/*query = s.createQuery("from Cliente where idcliente=:cod");
query.setInteger("cod", new Integer(cod));
Cliente c = (Cliente) query.uniqueResult();
*/
// s.close();
return c;
}
meu hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/*</property>
<property name="hibernate.connection.username">*******</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="entity.Cliente" />
<mapping class="entity.Contatos" />
<mapping class="entity.Endereco" />
</session-factory>
</hibernate-configuration>