Persistence.createEntityManagerFactory 需要很长时间
我有一个运行正常的 DBUnit 测试,但需要很长时间(4-5 分钟)才能创建实体管理器工厂。我将 JPA 与 hibernate 和 SQL 服务结合使用。如果有人能对此有所了解,将会有很大帮助。我的机器似乎更快地把它归咎于 Sql 服务器:) 这是我的设置代码。
@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>
I have a DBUnit test which runs OK, but is taking ages (4-5 mins) to create the entity manager factory. I am using JPA with hibernate and SQL serve. Would be of great help if anybody could throw some light on this. My machine seems faster to blame it on Sql server :) Here is my setup code.
@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();
}
And here is my 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终成功将 EntityManagerFactory 操作时间从平均 255 秒左右缩短至 3 秒左右。将 hibernate-entitymanager 从 3.4.0.GA 升级到 3.6.3.Final,瞧!单元测试现在就像单元测试一样运行,不到 6 秒。将尝试根据我的知识寻求这种改进的答案。
Finally managed to bring down the EntityManagerFactory operation time from around 255 seconds to about 3 seconds on an average. Upgraded the hibernate-entitymanager from 3.4.0.GA to 3.6.3.Final and voila! Unit test runs like a unit test now, just under 6 secs. Will try to seeek an answer for this improvement for my knowledge.
我建议您尝试通过以下方式找到瓶颈所在:
1-使用终端连接到 SQLServer。
2-修改连接以链接到不同的数据库(MySQL、PostgreSQL、H2...)。
如果两者都运行顺利,那么问题出在您的配置中,只要我对 JPA 不太有经验,我就无法为您提供进一步的帮助。
I would recommend you trying to find where the bottleneck is by:
1-Connecting to the SQLServer using a terminal.
2-Modifying the connection to link to a different database (MySQL, PostgreSQL, H2,...).
If both run smoothly, then the problem is in your configuration, and I can't help you any further, as long as I am not quite experienced with JPA.