Persistence.createEntityManagerFactory()需要很长时间才能返回 [英] Persistence.createEntityManagerFactory() takes very long time to return

查看:655
本文介绍了Persistence.createEntityManagerFactory()需要很长时间才能返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 4.2,JPA 2.0& Postgres 9.2



代码被卡在 Persistence.createEntityManagerFactory(peristence_unit_name);



在进一步调查中,我发现Hibernate调用 getTypeInfo() 的方法java.sql.DatabaseMetaData class。此方法尝试加载有关每个数据库对象的元数据

$ $ $ $ $ $ $ $
*执行提取
*
* @param metaData JDBC元数据
*
* @return提取的元数据
* /
public static LinkedHashSet< TypeInfo> extractTypeInfo(DatabaseMetaData metaData){
LinkedHashSet< TypeInfo> typeInfoSet = new LinkedHashSet< TypeInfo>();
尝试{
ResultSet resultSet = metaData.getTypeInfo(); //<<得到卡在这里。
try {
......

代码 getTypeInfo()是Postgers的JDBC驱动程序的一部分,它确实是需要时间来执行该方法的驱动程序(我加载了驱动程序源并尝试跟踪)。但是由于这个问题在Hibernate 3.3(我之前使用过)中没有发生过,所以我认为Hibernate 3.3并没有调用这个方法,但是Hibernate 4.2是。



是有办法解决这个问题吗?请注意,与Hibernate 3.3相同的设置工作正常。



额外信息:
这些驱动程序一直在做什么:



它首先获取数据库对象列表,然后遍历结果集以获取每个对象的元数据。

数据库中有大约3000个对象(我有一个非常大的数据集需要很多分区表,加上PostGIS有很多自己的对象)



每个对象查找大约需要1秒,因此完成调用需要大约3000秒。

解决方案

我有同样的问题,出于某种原因,为了提取hibernate的JDBC元数据需要很长时间。
您可以使用jsbc元数据关闭休眠功能,并且它会变得非常快。但请记住,hibernate不使用jdbc元数据,而是使用一些默认值。



试试这个集: hibernate.temp.use_jdbc_metadata_defaults 为false。

 < prop key =hibernate.temp.use_jdbc_metadata_defaults> false< / prop> 


I am using Hibernate 4.2, JPA 2.0 & Postgres 9.2

The code gets stuck at Persistence.createEntityManagerFactory("peristence_unit_name");

On further investigation I found that Hibernate makes a call to getTypeInfo() method of java.sql.DatabaseMetaData class. This method tries to load metadata about every database object

/**
 * Perform the extraction
 *
 * @param metaData The JDBC metadata
 *
 * @return The extracted metadata
 */
public static LinkedHashSet<TypeInfo> extractTypeInfo(DatabaseMetaData metaData) {
    LinkedHashSet<TypeInfo> typeInfoSet = new LinkedHashSet<TypeInfo>();
    try {
        ResultSet resultSet = metaData.getTypeInfo(); // << Gets stuck here.
        try {
             ......

The code for getTypeInfo() is part of Postgers' JDBC drivers and it is indeed the driver thats taking time to execute the method (I loaded the driver source and tried following the trail). But since this problem did not occur in Hibernate 3.3 (which I was using earlier), I think, Hibernate 3.3 was not calling this method, but Hibernate 4.2 is.

Is there a way to solve this problem? Note that the same setup with Hibernate 3.3 was working fine.

Extra Info : What the driver is doing all this time:

It first gets the list of database objects and then loops through the resultset to get metadata of each object.

There are around 3000 objects in the DB (I have a very large data-set requiring many partition tables. Plus PostGIS has many objects of its own)

It takes about 1 second per object lookup, so it takes about 3000 seconds to finish the call.

解决方案

I have the same problem and for some reason it takes long time to extract JDBC metadata for hibernate. You can just turn off using jsbc metadata for hibernate and it will become very fast. But remember that hibernate is not using jdbc metadata and will use some defaults instead.

To try this out set hibernate.temp.use_jdbc_metadata_defaults to false.

 <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>

这篇关于Persistence.createEntityManagerFactory()需要很长时间才能返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆