0

Via a Junit test, I am calling a method that calls currentSession() for getting a session object.

public final ThreadLocal session = new ThreadLocal();
    public synchronized Session currentSession() {
        Session s = (Session) session.get();
        // Open a new Session, if this thread has none yet

        if (s == null || !s.isOpen()) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

The code hangs at s = sessionFactory.openSession() ;. Below is my hibernate.properties and initialization of sessionFactory code. What am I missing ?

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.c3p0.acquire_increment=1 
hibernate.c3p0.idle_test_period=100 
hibernate.c3p0.max_size=100 
hibernate.c3p0.max_statements=0 
hibernate.c3p0.min_size=10 
hibernate.c3p0.timeout=1800
hibernate.c3p0.preferredTestQuery=select 1
hibernate.c3p0.testConnectionOnCheckout=true
hibernate.c3p0.testConnectionOnCheckout=true

initialize sessionFactory code

synchronized (this) {
            if (sessionFactory == null) {
                try {
                    String connection = "jdbc:mysql://"
                            + Globals.DBSERVER.trim()
                            + "/mydb?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
                    log.debug("Connection URL " + connection);
                    Configuration configuration = new Configuration();
                    configuration                               
                            .setProperty("hibernate.connection.username",
                                    Globals.DB_USER_NAME.trim())
                            .setProperty("hibernate.connection.password",
                                    Globals.DB_PASSWORD.trim())    
                    ;
                    configuration.configure();

                    sessionFactory = configuration
                            .buildSessionFactory(new ServiceRegistryBuilder()
                                    .applySettings(
                                            configuration.getProperties())
                                    .buildServiceRegistry());

                } catch (Exception e) {
                    log.fatal("Unable to create SessionFactory for Hibernate");
                    log.fatal(e.getMessage());
                    log.fatal(e);
                    e.printStackTrace();
                }
            }

            if (sessionFactory == null) {
                log.fatal("Hibernate not configured.");
                System.exit(0);
            }
            log.info("Hibernate Configured Successfully!!!");
        }
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • Where exactly does the code hang? Have you tried to analyze a thread dump? http://stackoverflow.com/questions/4876274/kill-3-to-get-java-thread-dump – user3707125 Jun 17 '15 at 14:32
  • The code hangs at `s = sessionFactory.openSession() ;` – Siddharth Jun 17 '15 at 16:41
  • Not the line in your code, the line in the libraries' code. In order to analyze why thread hangs you need to understand where it hangs in the underlying code - full stack trace of the thread that hangs can easily show the reason. – user3707125 Jun 17 '15 at 16:44
  • now how do i find that out ? – Siddharth Jun 17 '15 at 16:52
  • Run your test, wait until the program hangs, and then use debugger of your IDE if possible, or use suggestions from the link I mentioned earlier (e.g. kill -3 $JavaAppPID), or just google "java thread dump" and find an approach that suits your setup. – user3707125 Jun 17 '15 at 17:00
  • how do i find the pid, cant google that.. sorry – Siddharth Jun 17 '15 at 17:01
  • 1
    Provide more details: 1. Does it work for your application and fails only for the JUnits? 2. Does it fail for all JUnits or a particular JUnit? 3. Post the whole JUnit code, if it fails for a particular one. Not sure why you are synchronizing currentSession() since a thread safe variable is by definition thread safe and openSession() is thread safe 4. Post the thread dump of the JUnit – codedabbler Jun 17 '15 at 18:22
  • @ddalton you gave me the hint i needed, please add an answer – Siddharth Jun 18 '15 at 02:35

1 Answers1

0

I was calling a background async thread to do the database write. Junit was exiting before the background thread could complete the write into the database.

I put a timeUnit, wait after the flushData, which queues the task for writing, and it worked.

@Test
    public void test() {
        logWrapper.flushData();
        System.out.println("GeneralLogReadWriteTest test()");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            System.out.println("InterruptedException e");
        }
    }
Siddharth
  • 9,349
  • 16
  • 86
  • 148