2015-06-17 106 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"); 
     } 
    }