> is it better to share a single
> factory instance and synchronized on it or is it better to create a new
> factory instance for each thread?

I guess I would create a new one each time.  But you should experement.

> would it be faster to use compiled translet objects or trax templates
> objects?

Your code shouldn't know the difference... just use Templates.  If you are
using the XSLTC implementation, a translet will be built.  The overhead of
the Templates wrapper is very tiny, I think.

-scott




                                                                                
                                   
                    Donald Ball                                                 
                                   
                    <[EMAIL PROTECTED]       To:     <[EMAIL PROTECTED]>        
                            
                    gerZ.com>            cc:     (bcc: Scott Boag/CAM/Lotus)    
                                   
                                         Subject:     question on trax 
threadsafety                                
                    08/21/2001                                                  
                                   
                    05:00 PM                                                    
                                   
                                                                                
                                   
                                                                                
                                   




i'm writing a simple trax servlet, relying on the xalan-j implementation
naturally, and i want to make sure it's as threadsafe as possible. i'm
going to cache the stylesheets for the lifetime of the servlet. my code is
as follows:

    static final SAXTransformerFactory factory =
(SAXTransformerFactory)TransformerFactory.newInstance();
    static final Map templateMap = Collections.synchronizedMap(new
HashMap());

    static final TransformerHandler getTransformerHandler(String name)
throws
TransformerConfigurationException {
        Templates templates = (Templates)templateMap.get(name);
        if (templates == null) {
            synchronized (factory) {
                templates = factory.newTemplates(new
StreamSource(Thread.currentThread().getContextClassLoader
().getResourceAsStream(name);
            }
            templateMap.put(name,templates);
        }
        TransformerHandler handler = null;
        Transformer transformer = null;
        synchronized (factory) {
            handler = factory.newTransformerHandler(templates);
        }
        transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD,"html");
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");
        return handler;
    }

is this correct? note i'm synchronizing all factory methods since the docs
claim factory methods are not threadsafe. is it better to share a single
factory instance and synchronized on it or is it better to create a new
factory instance for each thread?

would it be faster to use compiled translet objects or trax templates
objects?

- donald





Reply via email to