Table Of ContentBitter EJB
BRUCE TATE
MIKE CLARK
BOB LEE
PATRICK LINSKEY
MANNING
Greenwich
(74° w. long.)
For electronic information and ordering of this and other Manning books,
go to www.manning.com. The publisher offers discounts on this book
when ordered in quantity. For more information, please contact:
Special Sales Department
Manning Publications Co.
209 Bruce Park Avenue Fax: (203) 661-9018
Greenwich, CT 06830 email: [email protected]
©2003 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
in any form or by means electronic, mechanical, photocopying, or otherwise, without
prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial
caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to
have the books they publish printed on acid-free paper, and we exert our best efforts to that
end.
ISBN 1-930110-95-2
Manning Publications Co. Copyeditor: Adrianne Harun
209 Bruce Park Avenue Typesetter: Tony Roberts
Greenwich, CT 06830 Cover designer: Leslie Haimes
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – VHG – 06 05 04 03
PART 1 THE BASICS ....................................................1
1 Bitter choices 3
1.1 A storm of controversy 5
1.2 A history of EJB antipatterns 6
March 1998: EJB 1.0 7 ■ November 1999: EJB 1.1 7
August 2001: EJB 2.0 8
1.3 A case study: Benchmarking PetStore 9
1.4 Antipattern: The Golden Hammer 12
Choosing unwisely 13 ■ Solution: Evaluate carefully 15
1.5 Summary: Looking ahead 19
1.6 Antipatterns in this chapter 20
2 The bitter cost 21
2.1 Sparking controversy 22
The value of EJB 23 ■ EJB-appropriate applications 23
Using a litmus test 25 ■ Passing the test 26
Weighing complexity 27 ■ Evaluating your talent 28
2.2 Antipattern: A Sledgehammer for a Fly 30
Adding complexity 32 ■ Solution: Simplify 33
Consider the cost of managing many files 36
Grading the finer points 39
2.3 Entity beans are a horse of a different color 41
The black sheep of the bean family 42
2.4 Entity beans: Take two 43
Local interfaces 44 ■ Container-managed relationships 45
2.5 Entity beans—a closer look 45
Employee management 45
2.6 Summary 50
2.7 Antipatterns in this chapter 51
3 Bitter interfaces 52
3.1 Building a good interface 53
Breaking down remote invocation performance 54
Passing by reference vs. value 55
3.2 Designing the application tier 57
Looking back on entity beans 59 ■
Questioning EJB local interfaces 59
3.3 Antipattern: Local & Remote Interfaces
Simultaneously 60
Combined interfaces muddle exception management 61
Combined interfaces hurt performance 62 ■ Mini-antipattern:
Ubiquitous Distribution 63 ■ Mini-antipattern: Transparent
Distribution 63 ■ Solution: Achieving equilibrium 64
Knowing when to distribute 65
3.4 Antipattern: Customers in the Kitchen 66
Nudging the diners toward the door 68
Solution: Funnel the customers through a waiter 70
Using Data Transfer Objects 71
3.5 Antipattern: Custom DTOs 73
Solution: Refactor to preserve flexibility 75
3.6 Summary 77
3.7 Antipatterns in this chapter 78
PART 2 SESSIONS AND MESSAGES................................. 81
4 Bitter sessions 83
4.1 Threading and synchronization 85
Antipattern: Tangled Threads 86 ■ Solution: Standardization
to the rescue 87 ■ Coping with hung threads 88
Searching for a solution 90
4.2 Handling exceptions 92
Mini-antipattern: Logic in Exception Implementations 92
Solution: Refactor logic out of exceptions 93 ■ Antipattern:
Swallowing Exceptions 94 ■ Solution: A simple exception
handling strategy 95 ■ Antipattern: Killer System Exceptions 97
Solution: Throw the correct exception type 98
4.3 Iterating large datasets 99
Antipattern: Database Connection Hog 101 ■ Solution: Manage
connections with surgical precision 103 ■ Antipattern: Eager
Iterator 103 ■ Solution: Test, test, test 106 ■ Caching
results 107 ■ Exporting results 108 ■ Determining the size of a
result set 109 ■ Iterating shuffled data 110
4.4 Interoperating efficiently 111
Using IIOP 112 ■ Antipattern: Narrow Servlet Bridges 113
Solution: A generic servlet bridge 113
4.5 Summary 120
4.6 Antipatterns in this chapter 121
5 Bitter session states 126
5.1 Making a case for session state 128
5.2 A pivotal antipattern: Conversational Baggage 129
The burden of state 130 ■ Lightening the load 131
Solution 1: Strive for statelessness 132
Solution 2: Leverage session state when beneficial 134
5.3 Managing sessions with stateful session beans 134
Shopping using a stateful session bean 135
Looking under the hood 136
5.4 Managing sessions with servlets 140
Keeping it simple with HttpSession 140 ■ Shopping using a
servlet 141 ■ Scaling up servlet sessions 142
5.5 Antipattern: Golden Hammers of Session State 143
Storing sessions on the client 144 ■ Storing sessions on the
server using servlets 145 ■ Storing sessions on the server
using stateful EJB 147 ■ Storing sessions in a
database 148 ■ Revisiting the shopping cart 149
Overall solution: Pick the right tool for the job 151
5.6 Mini-antipattern: Stateful Session Beans as Shared Data
Caches 151
5.7 Antipattern: Session Hodgepodge 152
Solution: Be explicit and conservative 154
5.8 Mini-antipattern: Session Thrashing 155
5.9 Mini-antipattern: Rotting Session Garbage 155
5.10 Summary: Taming the beast 156
5.11 Antipatterns in this chapter 157
6 Bitter messages 162
6.1 A brief overview of JMS 163
6.2 An early antipattern: Fat Messages 165
One size doesn’t fit all 167 ■ Solution 1: Put messages on a
diet 167 ■ Solution 2: Use references 168
6.3 Mini-antipattern: Skinny Messages 169
Solution: Use state to allow lazy loading 170
6.4 Seeds of an order processing system 171
Defining the system 171 ■ Designing messages 172
Choosing messaging models 173 ■ Responding to change 173
Building the OrderRequest producer 175
6.5 Antipattern: XML as the Silver Bullet 177
Solution: Use XML messages judiciously 177
6.6 Antipattern: Packrat 179
Putting a price on persistence 179 ■ Paying for durable
subscriptions 181 ■ Solution: Save only what’s important 181
6.7 Mini-antipattern: Immediate Reply Requested 182
6.8 Using message-driven beans (MDBs) 184
Pooling with MDBs 184
Building the OrderRequest consumer 185
6.9 Antipattern: Monolithic Consumer 188
Listening to the test 188
Solution: Delegate to modular components 189
6.10 Antipattern: Hot Potato 191
Solution: Acknowledge the message, not its result 193
6.11 Antipattern: Slow Eater 194
Solution: Eat faster, if you can 194
6.12 Antipattern: Eavesdropping 195
Solution: Use message selectors 196 ■ Declaring message
selectors 197 ■ Going beyond message selectors 198
6.13 Antipattern: Performance Afterthoughts 199
Solution: Measure early and often 199
6.14 Summary: Getting the message 201
6.15 Antipatterns in this chapter 202
PART 3 EJB PERSISTENCE .......................................211
7 Bitter entities 213
7.1 Understanding entity bean antipatterns 214
Understanding the entity bean antipattern landscape 214
7.2 Antipattern: Face Off 215
Network round-tripping chokes applications 216 ■ Losing
transactional integrity 217 ■ Solution: A Session Façade 219
Using a façade for transactional integrity 220
Using local interfaces 220
7.3 Antipattern: Ham Sandwich; Hold the Ham 222
The choice between BMP and CMP 223
Solution: Choose CMP when possible 225
7.4 Antipattern: Application Joins 228
Solution: Delegate joins to the database 228
Common examples of application joins 229
7.5 Antipattern: Application Filters 230
Understanding the types of application filters 230
7.6 Antipattern: Rusty Keys 233
Solution: shorten your primary key 234
7.7 Antipattern: Revolving Doors 235
Solution 1: Refactor to avoid re-entrancy 237
Solution 2: Disable the container’s re-entrancy checking 238
Solution 3: Lobby the EJB specification team 238
7.8 Summary 239
7.9 Antipatterns in this chapter 240
8 Bitter alternatives 245
8.1 Understanding entity bean alternatives 246
8.2 Using EJB persistence 249
Implementing CMP 250 ■ Adding the DTO and facade 253
Including deployment details 257
Rolling your own with BMP entity beans 259
8.3 Simplify with JDBC 260
Implementing a simple JDBC model 261 ■ Implementing the JDBC
Façade 263 ■ Deploying a Session Façade for JDBC 265
8.4 Using object persistence frameworks 267
Surveying the object persistence landscape 267 ■ Understanding
JDO 269 ■ Implementing a simple model with JDO 270
Implementing the JDO model’s façade 272 ■ Deploying the
solution 273 ■ Comparing the options 274
8.5 Antipattern: Persistent Problems 275
Generic entity bean weaknesses 276 ■ Inheritance and
polymorphism 276 ■ Query language flexibility is often
critical 279 ■ DTOs require non-EJB solutions for local
entities 281 ■ Container-bound persistence 282
8.6 Solution: Do not “inherit” a persistence architecture—
choose it 282
8.7 Summary 283
8.8 Antipatterns in this chapter 284
PART 4 BROADER TOPICS........................................285
9 Bitter tunes 287
9.1 Measures of success 289
Response time 289 ■ Throughput 290
9.2 Antipattern: Premature Optimization 291
Tuning EJB applications blindfolded 292
Solution 1: Plan, but don’t act (yet) 293
Solution 2: Write well-factored, modular code 294
9.3 Antipattern: Performance Afterthoughts 296
Solution: Plan early and often 296
9.4 Grist for the tuning mill 298
Putting an EJB to the test 299 ■ Passing the test 300
Specifying response time as a measure of success 301
Seeing light at the end of the tuning tunnel 302
9.5 Antipattern: Thrash-tuning 303
Solution: Use a performance testing methodology 304
9.6 Mini-antipattern: Manual Performance Testing 305
Solution: Automate performance testing 306
9.7 Automated performance testing with JUnitPerf 307
JUnitPerf overview 307 ■ Testing response time 308
Tweaking code 309 ■ Specifying scalability as a measure of
success 310 ■ Testing response time under load 310
Using a connection pool to increase throughput 312
Testing throughput 314
9.8 Modeling performance 315
9.9 Mini-antipattern: Stage Fright 317
Solution: Practice on stage 317
9.10 Summary: Tuning with confidence 318
9.11 Antipatterns in this chapter 319
10 Bitter builds 324
10.1 Wrapping big packages without bows 326
Understanding an example EJB 326 ■ Organizing your
directory structure 328 ■ Filling the EJB JAR 329
Loading classes 330
10.2 Antipattern: System Loaded Application Classes 332
Solution: Follow the J2EE guidelines 332
10.3 Antipattern: EJB Code Duplication 332
Solution: Autogenerate the EJB classes 333
Solution: Autogenerate the manifest 334
Solution: Autogenerate the EAR descriptor 336
10.4 Antipattern: Build Guru 337
Solution: Use Ant for heavy lifting 338
10.5 Antipattern: Running with Scissors 339
Solution: Test with impunity 339
10.6 Antipattern: Integration Hell 341
Solution: Integrate early, often, and automatically 341
10.7 Summary 342
10.8 Antipatterns in this chapter 343
11 A bittersweet future 348
11.1 Marking our place in history 349
Early mistakes 349
11.2 Plotting the next moves 351
Into the future 351 ■ Fix persistence 353
Fix the deployment strategy 354
Putting the economic house in order 354
11.3 Antipatterns and next moves 355