我有一个运行正常的DBUnit测试,但是需要花费一些时间(4-5分钟)来创建实体管理器工厂。我使用JPA与hibernate和SQL服务。如果有人能解释一下,那就太好了。我的机器似乎更快,归咎于Sql server:)这是我的设置代码。
@BeforeClass
public static void initEntityManager() throws Exception {
emf = Persistence.createEntityManagerFactory("primary");
em = emf.createEntityManager();
tx = em.getTransaction();
connection = new DatabaseConnection(((EntityManagerImpl) em).getSession().connection());
dataset = getDataSet();
}
这里是我的persistence。xml
<persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.prototype.database.Customer</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.connection.username" value="testuser" />
<property name="hibernate.connection.password" value="testuser" />
<property name="hibernate.connection.url" value="jdbc:sqlserver://localhost:1433;DatabaseName=testdb"/>
</properties>
</persistence-unit>
最终设法将EntityManagerFactory操作时间从大约255秒降低到平均约3秒。从3.4.0升级hibernate-entitymanager。GA到3.6.3。最后,瞧!单元测试现在像单元测试一样运行,只需不到6秒。
我建议您尝试通过以下方式找到瓶颈所在:
1-使用终端连接SQLServer。
2-修改连接到不同的数据库(MySQL, PostgreSQL, H2,…)
如果两者都运行顺利,那么问题出在你的配置上,我不能再帮助你了,只要我对JPA不太有经验。