2015-06-17 104 views
0

通過Junit測試,我打電話給調用currentSession()獲取會話對象的方法。Hibernate,sessionFactory.openSession掛起

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; 
    } 

代碼在s = sessionFactory.openSession() ;處掛起。下面是我的hibernate.properties和sessionFactory代碼的初始化。我錯過了什麼?

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 

初始化SessionFactory的代碼

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!!!"); 
     } 
+0

代碼在哪裏掛起?你有沒有試圖分析一個線程轉儲? http://stackoverflow.com/questions/4876274/kill-3-to-get-java-thread-dump – user3707125

+0

代碼掛在's = sessionFactory.openSession();' – Siddharth

+0

不是代碼中的行,行在圖書館的代碼中。爲了分析線程掛起的原因,您需要了解它在底層代碼中的掛起位置 - 掛起線程的完整堆棧跟蹤可以輕鬆顯示原因。 – user3707125

回答

0

我打電話後臺異步線程來執行數據庫寫入。 Junit在後臺線程完成寫入數據庫之前退出。

我放了一個timeUnit,在flushData後面等待寫入的任務,它工作。

@Test 
    public void test() { 
     logWrapper.flushData(); 
     System.out.println("GeneralLogReadWriteTest test()"); 
     try { 
      TimeUnit.SECONDS.sleep(5); 
     } catch (InterruptedException e) { 
      System.out.println("InterruptedException e"); 
     } 
    }