0

I have a spring boot project. I put in my resource folder hibernate.cfg.xml file. this is my hibernate.cfg.xml file:

<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">***</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/exportbatch?serverTimezone=UTC</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
</session-factory>

I have a hibernateUtil class used to instantiate a hibernate session.

public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory()
{
    try
    {
        // Create the SessionFactory from hibernate.cfg.xml
        // return new Configuration().configure().buildSessionFactory();
        return new AnnotationConfiguration().configure().buildSessionFactory();
    }
    catch (Throwable ex)
    {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory()
{
    return sessionFactory;
}

public static void shutdown()
{
    // Close caches and connection pools
    getSessionFactory().close();
}
}

In my application controller I have an endpoint in which I am requesting my mysql database.

@RequestMapping("/getbuildinginfo/{buildingid}")
public void getbuildinginfo(String buildingid)
            Session session = null;
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String query = "select * from exportbatch.exportbatch";
        SQLQuery sqlQuery = session.createSQLQuery(query);

        List<Object[]> rows = sqlQuery.list();
        HibernateUtil.shutdown();

This works fine the first time I run the project, but if I call the endpoint a second time, I am getting this exception: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

Could someone help me with this ? why it works the first time? Is it a spring boot context problem? if yes how could I resolve it ?

Aymen Ragoubi
  • 298
  • 5
  • 22
  • Which is nothing to do with the JPA API. That is the Hibernate API. –  Aug 30 '18 at 17:04
  • Why do you close your sessionfactory? Close the session instead. Don't forget to create a Transaction and then commit or rollback. – Ademir Constantino Aug 31 '18 at 03:58
  • You are using Spring Boot so why do you have a hibernate.cfg.xml ? – Simon Martinelli Aug 31 '18 at 14:33
  • yes now i changed and I am using springboot jpa – Aymen Ragoubi Aug 31 '18 at 14:56
  • Maybe [this](https://stackoverflow.com/questions/30096416/where-to-open-and-where-to-close-sessionfactory-in-my-application) can help you. But why don't you use the spring [repositories](https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#repositories) which will manage sessions, etc. for you? – Tom Aug 31 '18 at 15:12
  • Yes I am doing this way now, thanks – Aymen Ragoubi Aug 31 '18 at 15:18

0 Answers0