Persistence.createEntityManagerFactory() takes ver

2020-03-02 00:11发布

问题:

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.

回答1:

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>