Migration from Hibernate 3 to 4 slows down startup

we are trying to transfer our project from sleep mode 3 to sleep mode 4. Everything is working fine, but the problem is starting up.

We do not use JPA, we use direct sleep mode with xml file and mapping files.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.informix.jdbc.IfxDriver</property> <property name="hibernate.connection.url">jdbc:informix-sqli://xxx:xxx/xxx:INFORMIXSERVER=xxx</property> <property name="hibernate.connection.username">xxx</property> <property name="hibernate.connection.password">xxx</property> <property name="hibernate.dialect">org.hibernate.dialect.InformixDialect</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.show_sql">false</property> <property name="generated.mappingFile">dev.xml</property> </session-factory> </hibernate-configuration> 

The created.mappingFile property is its own property. At startup, the file will be loaded (dev.xml). This file is as follows:

  <mapping resource="de/cargosoft/edi/cargoservice/entities/aart/Aart_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/abteilung/Abteilung_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/adr/Adr_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/adraesort/Adraesort_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/adrakte/Adrakte_DEV.hbm.xml" /> ... <mapping resource="de/cargosoft/edi/cargoservice/entities/zollanmtxt/Zollanmtxt_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/sstbasis/Sstbasis_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/sststruktur/Sststruktur_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/ssthandler/Ssthandler_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/sstproperty/Sstproperty_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/sstprophandler/Sstprophandler_DEV.hbm.xml" /> <mapping resource="de/cargosoft/edi/cargoservice/entities/sstneustart/Sstneustart_DEV.hbm.xml" /> 

We have reduced the number of displays in this post. we have over 500 mappings at the moment.

With hibernate 3, it took 2 seconds to load all the mappings. With hibernate 4, it takes more than 2 minutes .

Here is the log file from hibernate 3.2.GA:

  07:36:21,293 INFO [HibernateManager ] | Verwende Mapping-Collection Datei : /com/cargosoft/csedi/data/mappings_dev.xml 07:36:21,347 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/aart/Aart_DEV.hbm.xml 07:36:21,443 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/abteilung/Abteilung_DEV.hbm.xml 07:36:21,458 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/adr/Adr_DEV.hbm.xml 07:36:21,495 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/adraesort/Adraesort_DEV.hbm.xml 07:36:21,523 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/adrakte/Adrakte_DEV.hbm.xml ... 07:36:23,475 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/zollanmtxt/Zollanmtxt_DEV.hbm.xml 07:36:23,477 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/sstbasis/Sstbasis_DEV.hbm.xml 07:36:23,479 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/sststruktur/Sststruktur_DEV.hbm.xml 07:36:23,481 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/ssthandler/Ssthandler_DEV.hbm.xml 07:36:23,482 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/sstproperty/Sstproperty_DEV.hbm.xml 07:36:23,484 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/sstprophandler/Sstprophandler_DEV.hbm.xml 07:36:23,486 INFO [HibernateManager ] | Adding this resource to hibernate now : com/cargosoft/csedi/data/sstneustart/Sstneustart_DEV.hbm.xml 07:36:23,488 INFO [HibernateManager ] | Create new SessionFactory for: jdbc:informix-sqli://... 

With sleeping 4.3.8-Final:

  07:38:04,749 INFO [HibernateManager ] | Verwende Mapping-Collection Datei : /de/cargosoft/edi/cargoservice/entities/mappings_dev.xml 07:38:04,824 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/aart/Aart_DEV.hbm.xml 07:38:05,249 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/abteilung/Abteilung_DEV.hbm.xml 07:38:05,527 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/adr/Adr_DEV.hbm.xml 07:38:05,792 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/adraesort/Adraesort_DEV.hbm.xml 07:38:06,077 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/adrakte/Adrakte_DEV.hbm.xml ... 07:40:14,119 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/zollanmtxt/Zollanmtxt_DEV.hbm.xml 07:40:14,499 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/sstbasis/Sstbasis_DEV.hbm.xml 07:40:14,746 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/sststruktur/Sststruktur_DEV.hbm.xml 07:40:14,972 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/ssthandler/Ssthandler_DEV.hbm.xml 07:40:15,211 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/sstproperty/Sstproperty_DEV.hbm.xml 07:40:15,434 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/sstprophandler/Sstprophandler_DEV.hbm.xml 07:40:15,657 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/sstneustart/Sstneustart_DEV.hbm.xml 07:40:15,878 INFO [HibernateManager ] | Create new SessionFactory for: jdbc:informix-sqli://... 

The method that adds the mapping files is as follows:

  for (Node node : nodes) { Element element = (Element) node; String resource = element.attributeValue("resource"); logger.info("Adding this resource to hibernate now : " + resource); configuration.addResource(resource); } 

Missing addResource time.

We also tried it by moving the display element directly to the hibernate.cfg.xml file, but it takes the same time to run.

We believe that sleep mode checks for things that were not in sleep mode 3.

Anyone have an idea to fix this problem? We cannot wait 2 minutes for each test run.

Thank you very much and many congratulations, Hauke

UPDATE

I changed loglevel to "DEBUG" and now this happens:

I changed to loglevel for debugging, and now it comes out:

  11:29:22,781 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/aart/Aart_DEV.hbm.xml 11:29:22,782 INFO [Configuration ] | HHH000221: Reading mappings from resource: de/cargosoft/edi/cargoservice/entities/aart/Aart_DEV.hbm.xml 11:29:22,804 DEBUG [DTDEntityResolver ] | Trying to resolve system-id [http://hibernate.org/dtd/hibernate-mapping-3.0.dtd] 11:29:23,149 INFO [HibernateManager ] | Adding this resource to hibernate now : de/cargosoft/edi/cargoservice/entities/abteilung/Abteilung_DEV.hbm.xml ... 

So, it seems that DTDEntityResolver takes about 200 ms - 400 ms for each object. With 500 objects, this will take stock.

So the question is how to disable this?

+6
source share
2 answers

You need to change the DTD of all HBM configuration files to:

 http://hibernate.org/dtd/hibernate-mapping-3.0.dtd 

in

 http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd 

This is how DTDEntityResolver tries to find DTO:

 public class DTDEntityResolver implements EntityResolver, Serializable { private static final String HIBERNATE_NAMESPACE = "http://www.hibernate.org/dtd/"; private static final String OLD_HIBERNATE_NAMESPACE = "http://hibernate.sourceforge.net/"; private static final String USER_NAMESPACE = "classpath://"; public InputSource resolveEntity(String publicId, String systemId) { InputSource source = null; // returning null triggers default behavior if ( systemId != null ) { LOG.debugf( "Trying to resolve system-id [%s]", systemId ); if ( systemId.startsWith( HIBERNATE_NAMESPACE ) ) { LOG.debug( "Recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/" ); source = resolveOnClassPath( publicId, systemId, HIBERNATE_NAMESPACE ); } else if ( systemId.startsWith( OLD_HIBERNATE_NAMESPACE ) ) { LOG.recognizedObsoleteHibernateNamespace( OLD_HIBERNATE_NAMESPACE, HIBERNATE_NAMESPACE ); LOG.debug( "Attempting to resolve on classpath under org/hibernate/" ); source = resolveOnClassPath( publicId, systemId, OLD_HIBERNATE_NAMESPACE ); } else if ( systemId.startsWith( USER_NAMESPACE ) ) { LOG.debug( "Recognized local namespace; attempting to resolve on classpath" ); String path = systemId.substring( USER_NAMESPACE.length() ); InputStream stream = resolveInLocalNamespace( path ); if ( stream == null ) { LOG.debugf( "Unable to locate [%s] on classpath", systemId ); } else { LOG.debugf( "Located [%s] in classpath", systemId ); source = new InputSource( stream ); source.setPublicId( publicId ); source.setSystemId( systemId ); } } } return source; } ... } 

Hibernate cannot find the hibernate-mapping-3.0.dtd configuration file in hibernate-core-4.3.8.Final.jar , and therefore it will go through your entire class path, and since you have a huge project, this explains why it starts so slowly.

+6
source

I once used a trick to skip DTD validation when parsing XML files using the code below. Your case is more complicated since you need to connect it to the Hibernate configuration parser. But it can make you start.

 private boolean skipDtd = true; // TODO make configurable private EntityResolver dummyDtdResolver = new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (skipDtd && systemId.toLowerCase().endsWith(".dtd")) return new InputSource(new StringReader("")); return null; } }; 

Several ways to use:

  • From DocumentBuilder

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setEntityResolver(dummyDtdResolver); ... parser.parse(new File(xmlName)); 
  • From XmlReader

     SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader(); xmlReader.setEntityResolver(dummyDtdResolver); ... new SAXSource(xmlReader, inputSource); 
+1
source

Source: https://habr.com/ru/post/982768/


All Articles