JPA Hibernate with Postgres - 2 transactions

Asked

Viewed 85 times

1

I started using Hibernate 1 month ago. I am developing a simple application from bank to college. I have an account class, which has the Withdraw Value method :

public boolean SacarValor(long numConta, double valor) {
    EntityManager em = FabricaEM.getEntityManager();
    Conta c = null;

    try {
        c = em.find(Conta.class, numConta);
        em.getTransaction().begin();
        if ((c.getSaldo() - valor) < 0) {//testar
            return false;
        } else {
            c.setSaldo(c.getSaldo() - valor);
            em.getTransaction().commit();
            em.getTransaction().begin();//nova transacao para registrar o movimento
            MovimentoDAO mdao = new MovimentoDAO();
            Movimento m = new Movimento();
            m.setTipoMovimento(TipoMovimento.SAQUE);
            m.setDataMovimento(Calendar.getInstance());
            m.setHoraMovimento(Time.valueOf(LocalTime.now()));
            m.setValorMovimento(5);
            m.setConta(em.find(Conta.class, numConta));
            mdao.save(m, numConta);
            em.getTransaction().commit();
        }
        return true;//removido
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        em.close();
    }
    return false;
}

I’m hoping that when the method(Withdraw value) was executed, a movement would be registered soon after, but I’m getting the following return.

 java.lang.IllegalStateException: Transaction already active

  Exception in thread "main" java.lang.IllegalStateException: EntityManager is closed

  at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:132)

  at modelo.dao.ContaDAO.SacarValor(ContaDAO.java:153)

  at modelo.tests.ContaTeste.main(ContaTeste.java:94)

  Java Result: 1

How can I perform two transactions then using the same transaction?

OBS : The subtraction in the Account table is done in the BD, but no new movement is recorded.

2 answers

0

Problem solved!

Resolution : 1st part : (Annotation in Arraylist movements)

@OneToMany(mappedBy = "conta", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)

private List<Movimento> movimentos = new ArrayList<>();

2nd part : (Method for adding movements)

public void adicionarMovimento(Movimento m){
    m.setConta(this);
    this.movimentos.add(m);
}

3rd part : (in the Sacarvalor method, when it persists) In addition to setting the balance, I called the add method);

c.setSaldo(c.getSaldo() - valor);
            c.adicionarMovimento(m);
            em.getTransaction().commit();

More details : I got information here

0

I don’t think you need that line:

 em.getTransaction().begin();//nova transacao para registrar o movimento,

Your transaction is already open before you enter Try, and when you enter it, it’s still open...

Then you try to start a new transaction again.

  • thanks, but I tried without this line... returns the following : java.lang.Illegalstateexception: Transaction not active

Browser other questions tagged

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