Skip to content

Commit

Permalink
HHH-10065 - Slow mapping initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Sep 1, 2015
1 parent 74d2d33 commit 78fcfe2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Expand Up @@ -155,11 +155,10 @@ protected static boolean hasNamespace(StartElement startElement) {
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> T jaxb(XMLEventReader reader, Schema xsd, Class<T> modelClass, Origin origin) { protected <T> T jaxb(XMLEventReader reader, Schema xsd, JAXBContext jaxbContext, Origin origin) {
final ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler(); final ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();


try { try {
final JAXBContext jaxbContext = JAXBContext.newInstance( modelClass );
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if ( isValidationEnabled() ) { if ( isValidationEnabled() ) {
unmarshaller.setSchema( xsd ); unmarshaller.setSchema( xsd );
Expand All @@ -181,4 +180,6 @@ protected <T> T jaxb(XMLEventReader reader, Schema xsd, Class<T> modelClass, Ori
); );
} }
} }


} }
Expand Up @@ -6,6 +6,8 @@
*/ */
package org.hibernate.boot.jaxb.internal; package org.hibernate.boot.jaxb.internal;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
Expand All @@ -21,6 +23,7 @@
import org.hibernate.boot.jaxb.internal.stax.LocalSchema; import org.hibernate.boot.jaxb.internal.stax.LocalSchema;
import org.hibernate.boot.jaxb.spi.Binding; import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.config.ConfigurationException;


import org.jboss.logging.Logger; import org.jboss.logging.Logger;


Expand All @@ -35,6 +38,7 @@ public class MappingBinder extends AbstractBinder {
private static final Logger log = Logger.getLogger( MappingBinder.class ); private static final Logger log = Logger.getLogger( MappingBinder.class );


private final XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance(); private final XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
private JAXBContext hbmJaxbContext;


public MappingBinder(ClassLoaderService classLoaderService) { public MappingBinder(ClassLoaderService classLoaderService) {
super( classLoaderService ); super( classLoaderService );
Expand All @@ -54,7 +58,7 @@ protected Binding doBind(
log.debugf( "Performing JAXB binding of hbm.xml document : %s", origin.toString() ); log.debugf( "Performing JAXB binding of hbm.xml document : %s", origin.toString() );


XMLEventReader hbmReader = new HbmEventReader( staxEventReader, xmlEventFactory ); XMLEventReader hbmReader = new HbmEventReader( staxEventReader, xmlEventFactory );
JaxbHbmHibernateMapping hbmBindings = jaxb( hbmReader, LocalSchema.HBM.getSchema(), JaxbHbmHibernateMapping.class, origin ); JaxbHbmHibernateMapping hbmBindings = jaxb( hbmReader, LocalSchema.HBM.getSchema(), hbmJaxbContext(), origin );
return new Binding<JaxbHbmHibernateMapping>( hbmBindings, origin ); return new Binding<JaxbHbmHibernateMapping>( hbmBindings, origin );
} }
else { else {
Expand All @@ -71,6 +75,18 @@ protected Binding doBind(
} }
} }


private JAXBContext hbmJaxbContext() {
if ( hbmJaxbContext == null ) {
try {
hbmJaxbContext = JAXBContext.newInstance( JaxbHbmHibernateMapping.class );
}
catch ( JAXBException e ) {
throw new ConfigurationException( "Unable to build hbm.xml JAXBContext", e );
}
}
return hbmJaxbContext;
}

private Document toDom4jDocument(XMLEventReader jpaOrmXmlEventReader, Origin origin) { private Document toDom4jDocument(XMLEventReader jpaOrmXmlEventReader, Origin origin) {
// todo : do we need to build a DocumentFactory instance for use here? // todo : do we need to build a DocumentFactory instance for use here?
// historically we did that to set TCCL since, iirc, dom4j uses TCCL // historically we did that to set TCCL since, iirc, dom4j uses TCCL
Expand Down

1 comment on commit 78fcfe2

@IvanRF
Copy link

@IvanRF IvanRF commented on 78fcfe2 Sep 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for solving this issue!

I'll test it when the new version is released.

Please sign in to comment.