JPA..Iniciante duvida no uso AbstractBaseDao alguém já utilizou

1 resposta
P

Olá,

alguem já utilizou esse AbstractBaseDao pois estou com problema na hora de rodar no servidor
Jboss depois de meia hora utilizando estoura o numero de conexão …com o banco
Não sei se a forma de implementação da classe DataSource está correta …

Se alguém puder me ajudar …

abs

--persistence.xml


<persistence-unit name="banco">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <!-- Only scan and detect annotated entities -->
         <property name="hibernate.archive.autodetection" value="class"/>

         <!-- property name="hibernate.hbm2ddl.auto" value="create"/ -->

         
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost/banco"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="root"/>
         <property name="hibernate.show_sql" value="true" />  
         
         <property name="hibernate.c3p0.min_size" value="0"/>
         <property name="hibernate.c3p0.max_size" value="10"/>
         <property name="hibernate.c3p0.timeout" value="1000"/>
         <property name="hibernate.c3p0.max_statements" value="50"/>
         <property name="hibernate.c3p0.idle_test_period" value="3000"/>
      </properties>
-----
public class DataSource {
    protected EntityManagerFactory factory = null;
    protected EntityManager manager = null;
    private EntityTransaction transaction = null;

    protected String bDatabase = "banco";

    private static DataSource instance = new DataSource();   
    public static DataSource getInstance() {   
    	return instance;   
    }   
    
    public void initialize(boolean in_pool) {
        factory = Persistence.createEntityManagerFactory(bDatabase);
        manager = factory.createEntityManager();
    }

    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 abstract class AbstractBaseDao<T extends Entity, ID extends Serializable> extends JpaDaoSupport implements BaseDao<T, ID> {
	
	protected DataSource conn = null;

	public AbstractBaseDao() {
		conn = DataSource.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 = conn.getManager().find(domainClass, id);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_ID), "Can not find by ID", e);
    	}
    }

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

	/**
	 * @see BaseDao#persist(ID) BaseDao.persist(T t)
	 */
    public void persist(T t) throws DaoException {
    	try {
    		conn.getManager().persist(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_PERSIST), "Can not insert", e);
    	}
    }
	/**
	 * @see BaseDao#remove(ID) BaseDao.remove(T t)
	 */
    public void remove(T t) throws DaoException {
    	try {
    		conn.getManager().remove(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_REMOVE), "Can not remove", e);
    	}
    }

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

    
	/**
	 * @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.conn.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);
    	}
        return l_list;
    }

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

	
}

---
public interface BaseDao<T extends Entity, ID extends Serializable> {
	
    T find(ID id) throws DaoException;
    
    T find(T entity) throws DaoException;

    void persist(T entity) throws DaoException;

    void merge(T entity) throws DaoException;

    void remove(T entity) throws DaoException;
    
    void removeById(ID id) throws DaoException;
    
    List<T> getList() throws DaoException;
    
    int count() throws DaoException;
}

1 Resposta

JonasZzZz

public abstract class AbstractBaseDao<T extends Entity, ID extends Serializable> extends JpaDaoSupport implements BaseDao<T, ID> { . . .
não sei a resposta da sua pergunta… mas que sintaxe é essa? nunca vi =/…

quem herda de quem aí?

Criado 6 de janeiro de 2009
Ultima resposta 6 de jan. de 2009
Respostas 1
Participantes 2