HHH-10170 - Reuse JAXBContext instance (Slow mapping initialization) - port HHH-10065 fix to 5.0 branch

(cherry picked from commit 78fcfe26a7)
This commit is contained in:
Steve Ebersole 2015-09-01 10:05:52 -05:00
parent 5aaf3566dc
commit 43feb01a79
2 changed files with 20 additions and 3 deletions

View File

@ -155,11 +155,10 @@ public abstract class AbstractBinder implements Binder {
}
@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();
try {
final JAXBContext jaxbContext = JAXBContext.newInstance( modelClass );
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if ( isValidationEnabled() ) {
unmarshaller.setSchema( xsd );
@ -181,4 +180,6 @@ public abstract class AbstractBinder implements Binder {
);
}
}
}

View File

@ -6,6 +6,8 @@
*/
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.XMLEventReader;
import javax.xml.stream.XMLStreamException;
@ -21,6 +23,7 @@ import org.hibernate.boot.jaxb.internal.stax.JpaOrmXmlEventReader;
import org.hibernate.boot.jaxb.internal.stax.LocalSchema;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.config.ConfigurationException;
import org.jboss.logging.Logger;
@ -35,6 +38,7 @@ public class MappingBinder extends AbstractBinder {
private static final Logger log = Logger.getLogger( MappingBinder.class );
private final XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
private JAXBContext hbmJaxbContext;
public MappingBinder(ClassLoaderService classLoaderService) {
super( classLoaderService );
@ -54,7 +58,7 @@ public class MappingBinder extends AbstractBinder {
log.debugf( "Performing JAXB binding of hbm.xml document : %s", origin.toString() );
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 );
}
else {
@ -71,6 +75,18 @@ public class MappingBinder extends AbstractBinder {
}
}
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) {
// todo : do we need to build a DocumentFactory instance for use here?
// historically we did that to set TCCL since, iirc, dom4j uses TCCL