'Transaction is not active' after receiving error for not being able to delete

Asked

Viewed 171 times

1

Guys tried to mount a crud for study and I came across the following situation.

I’m using JPA and hibernate.

When trying to delete a record that has reference in another table is launched an Exception saying that you can not delete pq has reference, until ai ok.

After this Exception appears if I try to add another item appears the same exception that showed when I tried to delete and just below 'Transaction is not active' what may be happening?

DAO

public T inserir(T entity) throws Exception {
    try {
        manager.getTransaction().begin();
        manager.persist(entity);
        manager.getTransaction().commit();          
    } catch (Exception e) {
        manager.getTransaction().rollback();
    }
    return entity;
}`

public void excluir(T entity) {
    try {
        manager.getTransaction().begin();
        manager.remove(entity);
        manager.getTransaction().commit();
    } catch (Exception e) {
        manager.getTransaction().rollback();
    }
}

RN

public Item inserir(Item item){
    try {
        return geralDAO.inserir(item);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }       
}

public void excluir(Item item){
    try {
        geralDAO.excluir(item);

    } catch (Exception e) {
        System.err.println("Erro ao deletar", e.getMessage());          
    }       
}
  • Present the implementation, it’s easier to guess.

  • the way I’m using now put in the question above @Matheus

1 answer

1

You’re probably creating the factory and the manager in the construtor, but every operation must be closed to conexão. The "correct" implementation would be to create Factory and manager in each method, and close after operation.

Ex:

public void setProdutos(ProdutosVO produto) throws Exception{
    try {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("model");
        EntityManager manager = factory.createEntityManager();

        manager.getTransaction().begin();    
        manager.persist(produto);
        manager.getTransaction().commit();  

    } catch (Exception e) { 
        throw new Exception(e);
    }finally {
        manager.close();
        factory.close();
    }
}
  • Got it, man I’m gonna take a test here to see what happens

  • So @Mattheus if I create Factory and the manager in each method will get fucked, it can generate a grotesque slowness on the server. I thought it might be because I wasn’t closing the transactions that was causing the error. But I’m still the same

  • Errors received: A Pooledconnection that has already signalled a Connection error is still in use!.

  • does . close() tb when falling in catch , or uses Finally.

  • tried already bro and nothing.

  • Brother, as I could not solve otherwise I did the following: Before deleting I check if the item has fk in the other table. thanks. I’m going to go deeper into jpa/Hibernate.. thanks mano @Matheus

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.