Hibernate Session Errors - getCurrentSession ()

0

Please, if possible, please comment on this code. If this is consistent or needs to be improved because I constantly have errors of org.hibernate.TransactionException: rollback failed or

org.hibernate.TransactionException: Transaction not successfully started
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:202).

Following codes:

    private Session session = null;
    private SessionFactory sessionFactory = null;

    public UsuarioDAO() {
        sessionFactory = HibernateUtil.getFactory();
    }

    public Usuarios autenticarUsuario(Usuarios usu) {
        Usuarios usuario = Singleton.getUsuarios();
        String hql = "FROM Usuarios WHERE emausu = :emausu AND senusu = :senusu";

        try {
            session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            usuario = (Usuarios) session.createQuery(hql)
                    .setParameter("emausu", usu.getEmausu())
                    .setParameter("senusu", usu.getSenusu())
                    .uniqueResult();

            if (!session.getTransaction().wasCommitted()) {
                session.getTransaction().commit();
            }
        } catch (RuntimeException erro) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            throw erro;
        }
        return usuario;
    }

Below is my hibernate.cfg.xml:

<hibernate-configuration>
    <session-factory>
        <!--  Configurações de Conexão ao Banco de Dados  -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/meuBanco?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">minhaSenha</property>


        <!--  **** Propriedades do C3P0 - Pool de Conexão ***  -->
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">50</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.c3p0.max_statements">50</property>


        <!--  ****SQL Dialect ****  -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <!--  ****Gerenciamento do Contexto da Sessão****  -->
        <property name="current_session_context_class">thread</property>


        <!--  ****Desabilitando Cache de segundo nível****  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>


        <!--  Mapeamento das Entidades  -->
        Minhas entidades

    </session-factory>
</hibernate-configuration>

Follow HibernateUtil.java:

private static SessionFactory sessionFactory;

    public static SessionFactory getFactory() {
        if(sessionFactory == null){
             sessionFactory = HibernateUtil.buildSessionFactory();
        }

        return sessionFactory;
    }

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());

            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            SessionFactory session = configuration.buildSessionFactory(serviceRegistry);
            return session;

        } catch (HibernateException ex) {
            System.err.println("Criação da SessionFactory failed." + ex);

            throw new ExceptionInInitializerError(ex);
        }
    }

In addition to the errors listed above, I also have max_user_connections errors.

The method shown above is only one of several methods, in this same format, in my application. So the improvement made here will be doubled for the entire application.

I am extensively trying to figure out what happens, but unfortunately with no success.

If anyone can help me.

Thank you very much

    
asked by anonymous 18.01.2018 / 12:35

1 answer

0

Try to sim:

session = sessionFactory.openSession();
session.beginTransaction(); 
    
18.01.2018 / 13:54