The Definitive Guide to Apache Myfaces and Facelets

The Definitive Guide to Apache Myfaces and Facelets

7370fm_final.qxd 8/29/08 4:26 PM Page i The Definitive Guide to Apache MyFaces and Facelets Zubin Wadia, Martin Marinschek, Hazem Saleh, and Dennis Byrne Contributing Authors: Bruno Aranda, Mario Ivankovits, Cagatay Civici, Arvid Hülsebus, Detlef Bartetzko, and Allan Lykke Christensen Excerpt from The Definitive Guide to Apache MyFaces and Facelets by Zubin Wadia, Martin Marinschek, Hazem Saleh, Dennis Byrne. Published by Apress, 2008. 7370fm_final.qxd 8/29/08 4:26 PM Page ii The Definitive Guide to Apache MyFaces and Facelets Copyright © 2008 by Zubin Wadia, Martin Marinschek, Hazem Saleh, Dennis Byrne, Mario Ivankovits, Cagatay Civici, Arvid Hülsebus, Detlef Bartetzko, Bruno Aranda, Allan Lykke Christensen All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-737-8 ISBN-10 (pbk): 1-59059-737-0 ISBN-13 (electronic): 978-1-4302-0344-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Dr. Sarang Poornachandra Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Editor: Heather Lang Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Linda Weidemann, Wolf Creek Press Proofreader: Martha Whitt Indexer: John Collin Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every pre- caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. The Definitive Guide to Apache MyFaces and Facelets is available from all good bookshops and at: http://eBookshop.apress.com 7370ch7_final 8/29/08 4:28 PM Page 229 CHAPTER 7 Antipatterns and Pitfalls This chapter covers antipatterns and pitfalls of day-to-day JSF development. Many of these issues have kept us up at night, and most of these are old problems with new faces: perform- ance, tight coupling, cache management, thread safety, security, and interoperability. N Plus One The N Plus One antipattern typically finds its way into web applications in a scenario like this: You want to render a web page of purchase orders along with some data about the customer of each order. This data must be read from a database. An efficient approach would be to pull a single dataset by joining the Customer table and the Order table.A far less efficient approach would be to read a dataset from the Order table, iterate over this dataset, and go back to the database for detailed customer information related to that order. The first approach costs one round-trip; the second approach costs N plus one round-trips, where N is the number of orders. Let’s look at how this antipattern can find its way into JSF applications. The powerful Open Transaction in View pattern has grown popular among application developers using object-relational mapping (ORM) frameworks. In the Hibernate community, the pattern often goes by a slightly different name, Open Session in View. This pattern begins by opening a transaction in a servlet filter as the request arrives and closing the transaction before the response is sent. The OpenTransactionInViewFilter class (OTVF) is an implemen- tation of this pattern: public class OpenTransactionInViewFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { ObjectRelationalUtility.startTransaction(); chain.doFilter(request, response); // commits transaction, if open ObjectRelationalUtility.commitTransaction(); } catch (Throwable throwable) { 229 Excerpt from The Definitive Guide to Apache MyFaces and Facelets by Zubin Wadia, Martin Marinschek, Hazem Saleh and Dennis Byrne. Published by Apress, 2008. 7370ch7_final 8/29/08 4:28 PM Page 230 230 CHAPTER 7 I ANTIPATTERNS AND PITFALLS try { ObjectRelationalUtility.rollback¯ Transaction(); } catch (Throwable _throwable) { /* sans error handling */ } } } public void init(FilterConfig config) throws ServletException { } public void destroy() { } } The beauty of this pattern is the convenience of querying the ORM framework in an action method, placing a persistent object in request scope, and letting the navigation handler forward the request to the appropriate view template. The page developer has the luxury of pulling data to the response via JSF EL expressions. Data can be lazily loaded as the page ren- ders and each JSF EL expression walks the Request-scoped object graph. We once assisted with a project where this pattern was applied. The project was on time and under budget, but the application had performance problems. During the first six months of development, the application had developed a very loud conversation with the database. The primary culprits were view templates like this: <!-- One trip to the database for the projects ... --> <h:dataTable value="#{projectBean.projects}" var="project"> <h:column> <h:commandLink action="#{projectBean.viewProject}" value="view project"/> </h:column> <h:column> <!-- ... and plus N trips for each project manager record --> <f:facet name="header">Project Manager</f:facet> #{project.manager.name} </h:column> <h:column> <f:facet name="header">Project Name</f:facet> #{project.name} </h:column> <h:column> <f:facet name="header">Start Date</f:facet> #{project.startDate} </h:column> The Definitive Guide to Apache MyFaces and Facelets is available from all good bookshops and at: http://eBookshop.apress.com 7370ch7_final 8/29/08 4:28 PM Page 231 CHAPTER 7 I ANTIPATTERNS AND PITFALLS 231 <h:column> <f:facet name="header">End Date</f:facet> #{project.endDate} </h:column> </h:dataTable> The data for this form could be retrieved with a single trip to the database. Instead, a single persistent instance of the domain model was being passed to the view template, and the JSF EL Resolver was triggering an additional trip to the database for each row to be ren- dered. The action methods in the managed beans weren’t digging deeply enough into the database and were causing the well-known N Plus One antipattern. An architectural decision was made to isolate all database transactions to the application invocation phase with the OpenTransactionInApplicationPhaseListener class (OTAPL), which accomplishes everything provided by the OTVF with finer transaction demarcation. The OTVF scopes a transaction to the life cycle of the request, while the OTAPL scopes a transaction to a single phase in the request life cycle. This subtly makes the difference between a web page that renders in O(n) and a web page that renders in O(1). public class OpenTransactionInApplicationPhaseListener implements PhaseListener { public void beforePhase(PhaseEvent event) { try { ObjectRelationalUtility.startTransaction(); } catch (Throwable throwable) { /* sans error handling */ } } public void afterPhase(PhaseEvent event) { try { // commits transaction, if open ObjectRelationalUtility.commitTransaction(); } catch (Throwable throwable) { try { ObjectRelationalUtility.rollback¯ Transaction(); Excerpt from The Definitive Guide to Apache MyFaces and Facelets by Zubin Wadia, Martin Marinschek, Hazem Saleh and Dennis Byrne. Published by Apress, 2008. 7370ch7_final 8/29/08 4:28 PM Page 232 232 CHAPTER 7 I ANTIPATTERNS AND PITFALLS } catch (Throwable _throwable) { /* sans error handling */ } /* sans error handling */ } } public PhaseId getPhaseId() { return PhaseId.INVOKE_APPLICATION; } } The OTVF was replaced with the OTAPL, and the tests were run. View templates trigger- ing read operations to the database could be smoked out by closing the transaction before the response rendering phase. Pages with exceptions could be used to trace which data- access objects needed to be fine-tuned. Every unit test passed; more than half the integration tests failed. Not only had this simple PhaseListener highlighted a performance issue,

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    49 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us