Close in Transaction with Ibernate

Asked

Viewed 438 times

2

Hello,

I need some help. I’m trying to make a transaction on Ibernate.

The problem is that by giving a close, the data are modified in the database, but this occurs WARN:

WARN [Commonannotationbeanpostprocessor] Invocation of Destroy method failed on bean with name 'sessionCreator': org.hibernate.Sessionexception: Session was already closed

The code I’m using is this:

Transaction transacao = null;
    try {
        transacao = session.beginTransaction();
        contaDao.atualiza(conta);
        transacao.commit();
        session.close();    
    } catch (Exception err) {  
        if (transacao != null) {
            transacao.rollback();
        }
    }

How can I solve this problem?

Follows the whole method:

 public void atualizarSaldo(LancamentoDados dados, double novoSaldo) {
    ContaBancaria conta = new ContaBancaria();
    HistoricoConta historicoSaldoConta = new HistoricoConta(this.historicoConta);
    conta.setCodContaBancaria(dados.getConta().getCodContaBancaria());
    conta.setNumeroContaBancaria(dados.getConta().getNumeroContaBancaria());
    conta.setSaldoContaBancaria(novoSaldo);
    conta.setAnotacaoContaBancaria("Sem conta");
    Transaction transacao = null;
    try {
        transacao = session.beginTransaction();
        System.out.println("Estou no try em atualiza");
        contaDao.atualiza(conta);
        historicoSaldoConta.inserirSaldoConta(conta);
        transacao.commit();
    } catch (Exception err) {
        System.out.println("Erro meu : " + err);
        if (transacao != null) {
            transacao.rollback();
        }
    }
}
  • you take your Session so Session Session = sessionFactory.openSession();?

  • Rafael, I’m not wearing it like this

  • @Ivanalves Can you put your entire method? You’re opening the session somewhere?

  • @Diegoaugusto put the whole method

  • @Ivanalves Where you are opening the session?

3 answers

0

I got through that code:

Transaction transacao = null;
    Session sess = Sessao.getSessao();      
    try {
        transacao = sess.beginTransaction();
        contaDao.atualiza(conta);
        transacao.commit();
    } catch (Exception err) {
        System.out.println("Erro meu : " + err);
        if (transacao != null) {
            transacao.rollback();
        }
    } finally {          
        sess.close();              
    }

0

Exception says your Session is already closed:

Session was already closed

I recommend doing a validation before closing Session, to Prevent it from trying to close one Session already closed:

if (this.session.isOpen()) {
      session.close();

    }
  • I did the if, as above, even if I only close if the connection has opened, it seems that at some other time it will try to close again. because it appears the excessao. if I do not give a close, this does not occur, but he Uga, not letting me return anything to the view.

  • In all its methods that make connection with the bank this being treated that ?

  • For this may be occurring in another way

  • This is occurring in this method when using the commit, if I comment the commit line of the method I’m using, nothing wrong happens in the system.

  • your connection to the database is set to setAutoCommit(false); ?

  • My bank is here

Show 1 more comment

0

You have already finished the session, ideally you do a check to see if the session has been opened and one to check if it is closed... you put the 2 if , you can control what happens in your code transactions.

if(session.Open())
{
}
if(session.Close())
{}

Browser other questions tagged

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