EJBT Design Patterns Advanced Patterns,Processes and ldioms Floyd Marinescu Wiley Computer Publishing 韧 John Wiley Sons,Inc. NEW YORK·CHICHESTER·WEINHEIM·BRISBANE·SINGAPORE·TORONTO
EJB™ Design Patterns Advanced Patterns, Processes, and Idioms Floyd Marinescu Wiley Computer Publishing John Wiley & Sons, Inc. NEW YORK • CHICHESTER • WEINHEIM • BRISBANE • SINGAPORE • TORONTO
Contents Foreword.......... Introduction .......................................................xii Acknowledgments …xix About the contributors..........................................xxiii Part One EJB Pattern Language ........................1 Chapter 1 EJB Layer Architectural Patterns 3 Session Facade.............. Message Facade 12 EJB Command ............................................18 Data Transfer Object Factory ................................26 Generic Attribute Access. .32 Business Interface........................................40 Chapter2 Inter-Tier Data Transfer Patterns .45 Data Transfer Object......................................47 Domain Data Transfer Object .51 Custom Data Transfer Objects..............................56 Data Transfer HashMap...................................59 Data Transfer RowSet .63
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix About the Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii Part One EJB Pattern Language . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1 EJB Layer Architectural Patterns . . . . . . . . . . . . . . . . . . . . . . . . . .3 Session Façade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Message Façade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 EJB Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Data Transfer Object Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Generic Attribute Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Business Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 Chapter 2 Inter-Tier Data Transfer Patterns . . . . . . . . . . . . . . . . . . . . . . . . .45 Data Transfer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Domain Data Transfer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Custom Data Transfer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 Data Transfer HashMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Data Transfer RowSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Contents
viii Contents Chapter 3 Transaction and Persistence Patterns.....................69 Version Number..........................................70 JDBC for Reading......... ...76 Data Access Command Beans 81 Dual Persistent Entity Bean 87 Chapter 4 Client-Side EJB Interaction Patterns......................91 EJBHomeFactory.........................................92 Business Delegate.........................................98 Chapter 5 Primary Key Generation Strategies......................105 Sequence Blocks.........................................106 UUIDforEJB............................................112 Stored Procedures for Autogenerated Keys .117 Part Two Best Practices for EJB Design and Implementation ...........................121 Chapter 6 From Requirements to Pattern-Driven Design .123 TheServerSide's Forum Messaging System Use Cases .........124 A Quick Referesher on Design Issues and Terminology .126 What Is a Domain Model? ..126 Understanding the Layers in a J2EE System...............127 Pattern-Driven EJB Architectures........................130 Domain and Persistence Layer Patterns...................130 Services Layer Patterns...... .133 Asychronous Use Cases 134 Synchronous Use Cases............................. .134 Other Services Layer Patterns 。年,。。。。。年。。。。。年。。g。 .137 Inter-Tier Data Transfer Patterns .137 Application Layer Patterns .. Summary ..142 Chapter 7 EJB Development Process:Building with Ant and Unit Testing with Junit.........................143 Order of Development ..... .144 Layer-Independent Code ..145 Domain First .146 Persistence Second .146 Services Third......... .148 Clients Last 148
Chapter 3 Transaction and Persistence Patterns . . . . . . . . . . . . . . . . . . . . .69 Version Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 JDBC for Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Data Access Command Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Dual Persistent Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Chapter 4 Client-Side EJB Interaction Patterns . . . . . . . . . . . . . . . . . . . . . .91 EJBHomeFactory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Business Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Chapter 5 Primary Key Generation Strategies . . . . . . . . . . . . . . . . . . . . . .105 Sequence Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 UUID for EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112 Stored Procedures for Autogenerated Keys . . . . . . . . . . . . . . . . . .117 Part Two Best Practices for EJB Design and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . .121 Chapter 6 From Requirements to Pattern-Driven Design . . . . . . . . . . . .123 TheServerSide’s Forum Messaging System Use Cases . . . . . . . . .124 A Quick Referesher on Design Issues and Terminology . . . . . . . .126 What Is a Domain Model? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Understanding the Layers in a J2EE System . . . . . . . . . . . . . . .127 Pattern-Driven EJB Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . .130 Domain and Persistence Layer Patterns . . . . . . . . . . . . . . . . . . .130 Services Layer Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 Asychronous Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Synchronous Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Other Services Layer Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Inter-Tier Data Transfer Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Application Layer Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Chapter 7 EJB Development Process: Building with Ant and Unit Testing with Junit . . . . . . . . . . . . . . . . . . . . . . . . .143 Order of Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 Layer-Independent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Domain First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Persistence Second . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Services Third . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Clients Last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 viii Contents
Contents ix Automating Environment Administration with Ant.....19 What Is a J2EE Application Environment?.................149 What Does It Mean to Administer a J2EE Application Environment? .151 Using Ant .152 Unit Testing with JUnit .168 Summary .177 Chapter 8 Alternatives to Entity Beans .179 Entity Beans Features........ .179 Entity Beans and Cognitive Dissonance.....................180 In Defense of Entity Beans...... ..182 Alternatives to Entity Beans .183 Use Straight JDBC/Stored Procedures 183 Use a Third Party O/R Mapping Product.................184 Build a Custom Persistence Framework...................184 Use Java Data Objects 184 An EJB Developer's Introduction to Java Data Objects .......185 Class Requirements and Dependencies............. .185 Build and Deployment Processes 187 Inheritance............ .188 Client APIs............... Dynamic versus Static Discovery Mechanisms 189 An EJB Developer's Guide to Using JDO .189 Preparing Your EJB Environment .189 Configuring Session Beans 190 Executing Use Cases and Transaction Management .191 Container-Managed Transactions .191 Bean-Managed Transactions .192 Caching/Lazy Loading and Reference Navigation .193 Finding Java Data Objects .194 Inter-Tier Data Transfer................................196 Summary ..197 Chapter 9 EJB Design Strategies,Idioms,and Tips ..................199 Don't Use the Composite Entity Bean Pattern ..... ..199 Use a Field-Naming Convention to Allow for Validation in EJB 2.0 CMP Entity Beans. .200 Don't Get and Set Value/Data Transfer Objects on Entity Beans.............. .201
Automating Environment Administration with Ant . . . . . . . . . . .149 What Is a J2EE Application Environment? . . . . . . . . . . . . . . . . .149 What Does It Mean to Administer a J2EE Application Environment? . . . . . . . . . . . . . . . . . . . . . . . .151 Using Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152 Unit Testing with JUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 Chapter 8 Alternatives to Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . .179 Entity Beans Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 Entity Beans and Cognitive Dissonance . . . . . . . . . . . . . . . . . . . . .180 In Defense of Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Alternatives to Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Use Straight JDBC/Stored Procedures . . . . . . . . . . . . . . . . . . . .183 Use a Third Party O/R Mapping Product . . . . . . . . . . . . . . . . .184 Build a Custom Persistence Framework . . . . . . . . . . . . . . . . . . .184 Use Java Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 An EJB Developer’s Introduction to Java Data Objects . . . . . . . .185 Class Requirements and Dependencies . . . . . . . . . . . . . . . . . . .185 Build and Deployment Processes . . . . . . . . . . . . . . . . . . . . . . . .187 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Client APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Dynamic versus Static Discovery Mechanisms . . . . . . . . . . . . .189 An EJB Developer’s Guide to Using JDO . . . . . . . . . . . . . . . . . . . .189 Preparing Your EJB Environment . . . . . . . . . . . . . . . . . . . . . . . .189 Configuring Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 Executing Use Cases and Transaction Management . . . . . . . .191 Container-Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . .191 Bean-Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192 Caching/Lazy Loading and Reference Navigation . . . . . . . . .193 Finding Java Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194 Inter-Tier Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 Chapter 9 EJB Design Strategies, Idioms, and Tips . . . . . . . . . . . . . . . . . .199 Don’t Use the Composite Entity Bean Pattern . . . . . . . . . . . . . . . .199 Use a Field-Naming Convention to Allow for Validation in EJB 2.0 CMP Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Don’t Get and Set Value/Data Transfer Objects on Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Contents ix
Contents Using Java Singletons Is OK If They're Used Correctly ........201 Prefer Scheduled Updates to Real-Time Computation ...202 Use a Serialized Java Class to Add Compiler Type Checking to message-Driven Bean Interactions .202 Always Call setRollbackOnly when Application Exceptions Occur...................203 Limit Parameters to ejbCreate 202 Don't Use Data Transfer Objects in ejbCreate.................204 Don't Use XML to Communicate as a DTO Mechanism Unless You Really,Really Have To 204 Appendix Pattern code Listing ..................................207 References....… ....241 ...243
Using Java Singletons Is OK If They’re Used Correctly . . . . . . . .201 Prefer Scheduled Updates to Real-Time Computation . . . . . . . . .202 Use a Serialized Java Class to Add Compiler Type Checking to Message-Driven Bean Interactions . . . . . . . . . . . . . . . . . . . . . . .202 Always Call setRollbackOnly when Application Exceptions Occur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Limit Parameters to ejbCreate . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Don’t Use Data Transfer Objects in ejbCreate . . . . . . . . . . . . . . . . .204 Don’t Use XML to Communicate as a DTO Mechanism Unless You Really, Really Have To . . . . . . . . . . . . . . . . . . . . . . . . . .204 Appendix Pattern Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 x Contents