Hello,
I need some help. I am trying to perform a transaction in hibernate.
The problem is that when giving a close, the data is modified in the database, but this warning occurs
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 resolve this problem?
Follow 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();
}
}
}