-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: problem with TransactionException
PostPosted: Tue Nov 21, 2017 6:11 pm 
Newbie

Joined: Tue Nov 21, 2017 5:48 pm
Posts: 5
Hi there,
hi have a problem with my app.
if I search for a user, that exists in my database, it works fine, returns the user.
If I search for a user who is not present in dbms, the search does not produce results, and I'm an exception

Code:
Exception in thread "AWT-EventQueue-0" org.hibernate.TransactionException: Transaction not successfully started
   at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:200)



this is the code of my call at the DAOUtente
Code:
Utente utente = null;
        SessionFactory sf = DAOUtilHibernate.getSessionFactory();
        try {
            sf.getCurrentSession().beginTransaction();
            utente = daoUtente.findByNomeUtente(userId);
            logger.debug("utente : " + utente.getNome());
            if (sf.getCurrentSession().getTransaction().wasCommitted()) {
                sf.getCurrentSession().getTransaction().commit();
            }
            logger.debug("utente : " + utente.getNome());
            if (utente == null) {
                pannello.getJLaberErrore().setText("UTENTE NON ESISTENTE");
            } else {
                if (!utente.getPassword().equals(pass)) {
                    pannello.getJLaberErrore().setText("USER ID E PASSWORD NON RICONOSCIUTE");
                } else {
                    this.controllo.getModello().setUtente(utente);
                    if (utente.getValoreRuolo() < 2) {
                        this.controllo.getModello().setModificaPosizioneTavoli(true);
                    }
                    vista.cambiaPannello((JPanel) vista.getMappaViste().get(costanti.Costanti.PANNELLO_120_INIZIALE));
                }
            }
        } catch (Exception ex) {
            if (sf.getCurrentSession().getTransaction().isActive()) {
                logger.debug("Trying to rollback database transaction after exception");
                sf.getCurrentSession().getTransaction().rollback();
            }
            sf.getCurrentSession().getTransaction().rollback();
            pannello.getJLaberErrore().setText("DATABASE NON DISPONIBILE O NON AVVIATO");
            logger.error("Database non disponibile" + ex);
        }
    }

This is DAOUtente code with its hierarchy
Code:
    public Utente findByNomeUtente(String nomeUtente) throws DAOException {
        List<Utente> lista = super.findByCriteria(Restrictions.eq("nome", nomeUtente));
        if (lista.size() != 0) {
            return lista.get(0);
        }
        Utente utente = null;
        return utente;
    }


what is my error ??
help me ;) thanks a lot ;)
Marco


Top
 Profile  
 
 Post subject: Re: problem with TransactionException
PostPosted: Wed Nov 22, 2017 2:14 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I guess this:

Code:
if (sf.getCurrentSession().getTransaction().wasCommitted()) {
   sf.getCurrentSession().getTransaction().commit();
}


should be this instead:

Code:
if (!sf.getCurrentSession().getTransaction().wasCommitted()) {
   sf.getCurrentSession().getTransaction().commit();
}


Also, this ill end up rolling back twice:

Code:
if (sf.getCurrentSession().getTransaction().isActive()) {
   logger.debug("Trying to rollback database transaction after exception");
   sf.getCurrentSession().getTransaction().rollback();
}
sf.getCurrentSession().getTransaction().rollback();


You want to change it to:

Code:
if (sf.getCurrentSession().getTransaction().isActive()) {
   logger.debug("Trying to rollback database transaction after exception");
   sf.getCurrentSession().getTransaction().rollback();
}


Top
 Profile  
 
 Post subject: Re: problem with TransactionException
PostPosted: Wed Nov 22, 2017 4:14 am 
Newbie

Joined: Tue Nov 21, 2017 5:48 pm
Posts: 5
oh my god ..
in first time i write only row with comment..
in second time i added Condition but i did not remove the row..
i'm stupid and.. not good solution write code at 23:00 o clock :(

Code:
if (sf.getCurrentSession().getTransaction().isActive()) {
                logger.debug("Trying to rollback database transaction after exception");
                sf.getCurrentSession().getTransaction().rollback();
            }
//sf.getCurrentSession().getTransaction().rollback();


Thanks a lot :)


Top
 Profile  
 
 Post subject: Re: problem with TransactionException
PostPosted: Wed Nov 22, 2017 8:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You're welcome.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.