0
Please, I would like your opinion on that code if possible.
If this coherent or needs to be improved because I constantly have errors org.hibernate.TransactionException: rollback failed
or
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:202).
Follows 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 follows my xml Hibernate.cfg.:
<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>
It follows 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 errors of max_user_connections
.
The method shown above is only one of several methods, in this same formatting, in my application. Therefore, the improvement that is made here will be duplicated for the entire application.
I am exhaustively trying to find out what happens, but unfortunately without success.
If anyone can help me.
Thank you very much
Hibernateutil.java as it is ?
– LR10
@LR10, follows up the Hibernateutil.java. Thank you very much
– EduOK
Have you considered the possibility of using Spring to control your transactions? Regardless, have you tried closing your sessionFactory after the transaction commit? This might solve Too’s problem.
– StatelessDev