JPA....erro java.lang.IllegalStateException: EntityManager is closed

estou fazendo um teste para uso do metodo close()


public void Close() {
		manager.close();
		factory.close();
	}    

qdo executo o teste abaixo o primeiro executa e grava no banco o segundo da erro

java.lang.IllegalStateException: EntityManager is closed

O que pode ser o que devo estar fazendo de errado …

alguém pode me ajudar …

abs

teste


   for (int w=1;w<50;w++){

  ClienteDao dao = new ClienteDao();
  Cliente cliente = new Cliente();
  cliente.setNome("Nome "+w);
  dao.persist(cliente); 


 }


  public void persist(T t) throws DaoException {
    	try {
    		
    		System.out.println("Conteudo AbstractBaseDao = "+gerenciador);
    		gerenciador.BeginTransaction();
    		gerenciador.getManager().persist(t);
    		gerenciador.CommitTransaction();
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_PERSIST), "Can not insert", e);
    	}finally{
    		gerenciador.Close();
    	}
    }

public class DATAS {
    
    protected String dDatabase = "banco";	
    protected EntityManagerFactory factory     = Persistence.createEntityManagerFactory(dDatabase);
    protected EntityManager        manager     = factory.createEntityManager();
    private   EntityTransaction    transaction = null;

    private static DATAS instance = new DATAS();   

    private DATAS(){
    }
    
    public static DATAS getInstance() {   
    	return instance;   
    }   
    

    public void finalize() {
    	
    }

    public EntityManager getManager() {
    	return this.manager;
    }
    
    public void BeginTransaction()
    {
    	transaction = manager.getTransaction();
    	transaction.begin();
    }
    
    public void CommitTransaction()
    {
    	transaction.commit();
    }

	public void RollBackTransaction() {
		transaction.rollback();
	}    
	
	public void Close() {
		manager.close();
		factory.close();
	}    
}
-------------------------------------------------------------------------


/**
 * Classe abstrata que define um DAO base com as funcionalidades básicas utilizadas por um sistema CRUD
 * (Create, Retrieve, Update e Delete).
 *  
 * @see BaseDao
 */
public abstract class AbstractBaseDao<T extends Entity, ID extends Serializable> extends JpaDaoSupport implements BaseDao<T, ID> {
	protected final static Long METHOD_FIND_BY_ID 		= 0L;
	protected final static Long METHOD_FIND_BY_CLASS 	= 1L;
	protected final static Long METHOD_PERSIST 			= 2L;
	protected final static Long METHOD_MERGE 			= 3L;
	protected final static Long METHOD_REMOVE 			= 4L;
	protected final static Long METHOD_REMOVE_BY_ID 	= 5L;
	protected final static Long METHOD_GET_LIST 		= 6L;
	protected final static Long METHOD_COUNT 			= 7L;
	
	protected DATAS gerenciador = null;

	public AbstractBaseDao() {
		gerenciador = DATAS.getInstance();
	}

	/* Classe que representa o objeto de domínio gerenciado pelo DAO. */
    protected Class<T> domainClass = getDomainClass();

    /* Retorna a classe do objeto de domínio gerenciado pelo DAO. */
    @SuppressWarnings("unchecked")
	protected Class<T> getDomainClass() {
        if (domainClass == null) {
        	ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();
            domainClass = (Class<T>) thisType.getActualTypeArguments()[0];
        }
        return domainClass;
    }

	/**
	 * @see BaseDao#find(ID) BaseDao.find(ID id)
	 */
    public T find(ID id) throws DaoException {
    	T t = null;
    	try {
    		t = gerenciador.getManager().find(domainClass, id);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_ID), "Can not find by ID", e);
    	}finally{
    		gerenciador.Close();
    	}
    }

	/**
	 * @see BaseDao#find(ID) BaseDao.find(T entity)
	 */
    public T find(T entity) throws DaoException {
    	T t = null;
    	try {
    		t = gerenciador.getManager().find(domainClass, entity);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_CLASS), "Can not find by Class", e);
    	}finally{
    		gerenciador.Close();
    	}
    }
    
	/**
	 * @see BaseDao#persist(ID) BaseDao.persist(T t)
	 */
    public void persist(T t) throws DaoException {
    	try {
    		
    		System.out.println("Conteudo AbstractBaseDao = "+gerenciador);
    		gerenciador.BeginTransaction();
    		gerenciador.getManager().persist(t);
    		gerenciador.CommitTransaction();
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_PERSIST), "Can not insert", e);
    	}finally{
    		gerenciador.Close();
    	}
    }
	/**
	 * @see BaseDao#remove(ID) BaseDao.remove(T t)
	 */
    public void remove(T t) throws DaoException {
    	try {
    		gerenciador.getManager().remove(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_REMOVE), "Can not remove", e);
    	}finally{
    		gerenciador.Close();
    	}
    }

	/**
	 * @see BaseDao#remove(ID) BaseDao.merge(T t)
	 */
    public void merge(T t) throws DaoException {
    	try {
    		gerenciador.getManager().merge(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_MERGE), "Can not update", e);
    	}finally{
    		gerenciador.Close();
    	}
    }

    
	/**
	 * @see BaseDao#getList() List<T> BaseDao.getList()
	 */
    @SuppressWarnings("unchecked")
    public List<T> getList() throws DaoException {
       	List<T> l_list = new ArrayList<T>();
        try {              
        	l_list = this.gerenciador.getManager().createQuery("select x from " + domainClass.getName() + " x").getResultList();
        	
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_GET_LIST), "Can not get the List of", e);
    	}finally{
    		gerenciador.Close();
    	}
        return l_list;
    }

	/**
	 * @see BaseDao#removeById(ID) BaseDao.removeById(ID id)
	 */
    public void removeById(ID id) throws DaoException {
    	try {
	        Object obj = find(id);
	        gerenciador.getManager().remove(obj);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_REMOVE_BY_ID), "Can not remove by ID", e);
    	}finally{
    		gerenciador.Close();
    	}
    }

	/**
	 * @see BaseDao#count() BaseDao.count()
	 */
    @SuppressWarnings("unchecked")
	public int count() throws DaoException {
        
    	Long resultado = new Long(0L);
    	Query query = gerenciador.getManager().createQuery("select count(e) from " + domainClass.getName()+" e" );
    	resultado = (Long) query.getSingleResult();
    	gerenciador.Close();
    	return resultado.intValue();
    }

}

Veja: http://www.guj.com.br/posts/list/114384.java

Seria demais pedir que o paribe parasse de floodar o fórum?

Não. Já entrei em contato com ele.

Não. Já entrei em contato com ele.[/quote]

Obrigado! :smiley: