Olá,
peguei alguns exemplos na internet que na hora de fazer insert,update,delete,select,etc
não utiliza o metodo close() para fechar EntityManager o correto é fechar ou não ???
Ou existe uma forma dele fazer esse controle ???
Se alguém puder me ajudar agradeceria …
abs
-- inclusão
try {
DATAS.BeginTransaction();
dao.persist(user);
DATAS.CommitTransaction();
} catch (DaoException e) {
DATAS.RollBackTransaction();
Long id = e.getId();
e.printStackTrace();
}
finally{
DATAS.Close();
}
----alteração
try {
DATAS.BeginTransaction();
dao.merge(user);
DATAS.CommitTransaction();
} catch (DaoException e) {
DATAS.RollBackTransaction();
Long id = e.getId();
e.printStackTrace();
}
finally{
DATAS.Close();
}
-- consulta
--consulta
try {
DATAS.BeginTransaction();
StringBuffer jhl = new StringBuffer();
jhl.append("SELECT distinct(us) FROM usuario us ");
usuariosdoctos = DATAS.getManager().createQuery(jhl.toString()).setFirstResult (inicio).setMaxResults(fim).getResultList();
DATAS.CommitTransaction();
} catch (DaoException e) {
DATAS.RollBackTransaction();
Long id = e.getId();
e.printStackTrace();
}
finally{
DATAS.Close();
}
public class DATAS {
protected EntityManagerFactory factory = null;
protected EntityManager manager = null;
private EntityTransaction transaction = null;
protected String nDatabase = "banco";
private static DATAS instance = new DATAS();
public static DATAS getInstance() {
return instance;
}
public void initialize(boolean in_pool) {
factory = Persistence.createEntityManagerFactory(nDatabase);
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 void Close() {
manager.close();
}
}