2

I'm writing simple app using Hibernate. I have hibernate.cfg.xml file as follows:

<!DOCTYPE hibernate-configuration SYSTEM
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.url">
        jdbc:mysql://localhost:3306/demo
    </property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">1234</property>
    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>



<!-- Use the C3P0 connection pool provider -->
    <property name="hibernate.c3p0.min_size">5</property> 
    <property name="hibernate.c3p0.max_size">20</property> 
    <property name="hibernate.c3p0.timeout">300</property> 
    <property name="hibernate.c3p0.max_statements">50</property> 
    <property name="hibernate.c3p0.idle_test_period">3000</property>

    <!--List of XML mapping files -->
    <mapping class="www.Message"/>
</session-factory>
</hibernate-configuration>

and retrieve SessionFactory object as follows:

//in HibernateUtil.class
  SessionFactory sessionFactory;
  Configuration configuration=new 
                             Configuration().configure("hibernate.cfg.xml");
   sessionFactory=configuration.buildSessionFactory();`

And everything works fine until I add C3P0 jars:

  • c3p0-0.9.5.2.jar
  • hibernate-c3p0-5.2.14.Final.jar
  • mchange-commons-java-0.2.11.jar

After adding jars above, the app throws StackOverflowError.

Here's my main App class:

package www;

import org.hibernate.Session;
import utils.HibernateUtil;
import java.util.List;

public class HelloWorld {
    public static void main(String[] args) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(new Message("Hello Denmark!"));
            session.getTransaction().commit();
            session.close();
            System.out.println("Message saved...");
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            List messages = session.createQuery("from Message m order by m.text 
asc").list();
            System.out.println("Messages found:"+messages.size());
            for (Object mes: messages) {
                Message message = (Message) mes;
                System.out.println(message.getText());
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) session.close();
        }
    }
}

Any help would be appreciated.

alexrnov
  • 2,346
  • 3
  • 18
  • 34
  • Does it throw error on application startup ? – soulcoder Mar 28 '18 at 12:30
  • Post the exception and anything else pertinent. – AlexC Mar 28 '18 at 13:53
  • Maybe you already have some c3p0 jars already on classpath. If you use Maven you can try to check if some jar versions are not duplicated. When few jars of same library with different version exist on classpath, the first (randomly) is taken. You can use https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html to show libraries tree. – soulcoder Mar 28 '18 at 14:01
  • @soulcoder I'm not using Maven in this project.And SOFError dissapeared after i removed c3p0-0.9.5.2.jar.Then i had another problem,which said that SLF4JLoggerContext cannot be cast to log4j's LoggerContext.I removed all slf4j jars as stated here:https://stackoverflow.com/questions/25891737/getting-exception-org-apache-logging-slf4j-slf4jloggercontext-cannot-be-cast-to but now it throws an error saying: ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. But i do have log4j.properties file... – Mukhamedali Zhadigerov Mar 29 '18 at 05:20

0 Answers0