2014-03-24 45 views
1

我有一個通過Hibernate連接到MySQL數據庫的Java應用程序。休眠測試沒有完成

我已經寫了一個測試用於測試與數據庫的連接,但它永遠不會結束。兩個線程正在運行,但是我在測試中寫入的所有代碼都已執行。這兩個線程是:

Thread [pool-1-thread-1] (Running) 
Thread [DestroyJavaVM] (Running)  

以下是我的testfile的:

Vote v1 = new Vote(0, "abc"); 
Vote v2 = new Vote(1, "def"); 
Candidate c1 = new Candidate(0, "First Candidate"); 
Timestamp t = new Timestamp(0, 123456l); 

EntityManager entMgr = EntityManagerUtil.getEntityManagerFactory().createEntityManager(); 
EntityTransaction transaction = entMgr.getTransaction(); 
transaction.begin(); 

VoteRepository vr = new VoteRepository(entMgr); 
entMgr.persist(v1); 
entMgr.persist(v2); 

CandidateRepository cr = new CandidateRepository(entMgr); 
entMgr.persist(c1); 

TimestampRepository tr = new TimestampRepository(entMgr); 
entMgr.persist(t); 

transaction.commit(); 
entMgr.close(); 

我的persistence.xml看起來是這樣的:

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <!-- This is where we tell JPA/Hibernate about our @Entity objects --> 
    <class>org.evoting.database.entities.Candidate</class> 
    <class>org.evoting.database.entities.Timestamp</class> 
    <class>org.evoting.database.entities.Vote</class> 

<properties> 
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://url" /> 
    <property name="javax.persistence.jdbc.user" value="***" /> 
    <property name="javax.persistence.jdbc.password" value="***" /> 
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 

    <!-- Update the database schema on startup: "update"; "validate" to only check --> 
    <property name="hibernate.hbm2ddl.auto" value="update" /> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="hibernate.show_sql" value="true" /> 

    <!-- Disable the second-level cache --> 
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvicer" /> 

    <property name="hibernate.format_sql" value="true" /> 
    <property name="hibernate.transaction.flush_before_completion" value="true" /> 
    <property name="hibernate.connection.autocommit" value="false" /> 
    <property name="hibernate.connection.release_mode" value="after_transaction" /> 
</properties> 

有什麼可以導致測試永遠不會完成?

+0

後的測試方法和類的完整代碼。你是否聲明測試是回滾事務處理?請參閱http://stackoverflow.com/questions/4166983/how-to-rollback-a-database-transaction-when-testing-services-with-spring-in-juni – emeraldjava

+0

它是完整的代碼。這只是一個普通的老式主要方法。 – Prechtig

+0

嘗試添加一些System.out.println(「test」);在你的方法中,你可以找出到底哪一行執行停止。 – dave823

回答

0

我發現我必須關閉EntityManagerFactory才能完成測試。因此,而不是

entMgr.close(); 

我應該寫

entMgr.getEntityManagerFactory.close(); 

有了這個新的生產線測試結束後,大家都開心:)