xvi CONTENTS 12.3 Using JPA from the web tier 447 ,448·Using ar Accessing an application-managed EntityManager outside 450 the container 451 12.4 Summary 454 3 Taming wild EIBs:performance and scalability 455 13.1 Handling entity locking issues 457 Understanding locking types 458 Optimistic locking and entity versioning 461 EntityManager and lock modes 462 13.2 Improving entity performance 464 Remodeling and schema redesign 464.Tuning the IDBC layer 468.Reducing database operations 470 Improving query performance 474.Caching 478 13.3 Improving performance of EJB 3 components 483 Session bean performance 484 Improving MDB performance 487 13.4 Clustering EJB applications 488 Collocated architecture 489 Load-balancing stateless session beans 490.Clustering stateful session beans 493 Entities and clustered cache 495 13.5 Summary 496 PART 5 MIGRATION AND INTEROPERABILITY ISSUES .........497 14 Migrating to EJB 3 499 14.1 Backward compatibility and interoperability with EJB 2 500 Packaging E[B 2 and EJB 3 together 501.Invoking E[B 2 fom可B3502·Using EJB3 from EJB2503 14.2 Migrating session beans 505 Converting interface and bean classes 506 Resource usage 510.Transactions and security settings 511 Client applications 511
xvi CONTENTS 12.3 Using JPA from the web tier 447 Using a container-managed entity manager 448 ■ Using an application-managed EntityManager with JTA transactions 450 Accessing an application-managed EntityManager outside the container 451 12.4 Summary 454 13 Taming wild EJBs: performance and scalability 455 13.1 Handling entity locking issues 457 Understanding locking types 458 ■ Optimistic locking and entity versioning 461 ■ EntityManager and lock modes 462 13.2 Improving entity performance 464 Remodeling and schema redesign 464 ■ Tuning the JDBC layer 468 ■ Reducing database operations 470 Improving query performance 474 ■ Caching 478 13.3 Improving performance of EJB 3 components 483 Session bean performance 484 Improving MDB performance 487 13.4 Clustering EJB applications 488 Collocated architecture 489 ■ Load-balancing stateless session beans 490 ■ Clustering stateful session beans 493 Entities and clustered cache 495 13.5 Summary 496 PART 5 MIGRATION AND INTEROPERABILITY ISSUES .......... 497 14 Migrating to EJB 3 499 14.1 Backward compatibility and interoperability with EJB 2 500 Packaging EJB 2 and EJB 3 together 501 ■ Invoking EJB 2 from EJB 3 502 ■ Using EJB 3 from EJB 2 503 14.2 Migrating session beans 505 Converting interface and bean classes 506 Resource usage 510 ■ Transactions and security settings 511 Client applications 511
CONTENTS xvii 14.3 Migrating message-driven beans 513 14.4 Migrating CMP 2 entity beans to the EJB 3 JPA 513 Entity bean classes and interfaces 519Client applications 526 14.5 Migrating JDBC DAOs to use the EJB 3 JPA 529 class to use the queries for JPQL.531 14.6 Helping O/R frameworks to use the EJB 3 JPA 533 14.7 Approaches to migration 535 Strategies 535.Manual vs.automated 536 14.8 Summary 536 5 Exposing EJBs as web services 537 15.1 What is a web service?539 Identifying web service components 541 Web s developing web services 15.2 JAX-WS:Java EE 5 web services platform 549 Introducing the web services platform 549.Why choose EJB over a POJO for a web service?550 15.3 Developing EJB web services with JAX-WS 2.0 551 Using the @WebService annotation 553 Specifying the web service style with @SOAPBinding 555 Using @WebMethod 557.Using the @WebPa 558 Using the @WebRe 560·Using@0 neWay and @HandlerChain 561 15.4 Accessing a web service from an EJB 562 562·EJB as a web 15.5 Best practices for web service development 565 15.6 Summary 567
CONTENTS xvii 14.3 Migrating message-driven beans 513 14.4 Migrating CMP 2 entity beans to the EJB 3 JPA 513 Redesign your domain model 514 ■ Using DTOs as entities 517 ■ Entity bean classes and interfaces 519 ■ Client applications 526 14.5 Migrating JDBC DAOs to use the EJB 3 JPA 529 Identifying entities 529 ■ Reworking a DAO implementation class to use the EntityManager API 530 ■ Swapping SQL queries for JPQL 531 14.6 Helping O/R frameworks to use the EJB 3 JPA 533 14.7 Approaches to migration 535 Strategies 535 ■ Manual vs. automated 536 14.8 Summary 536 15 Exposing EJBs as web services 537 15.1 What is a web service? 539 Identifying web service components 541 Web services styles 547 ■ Approaches to developing web services 548 15.2 JAX-WS: Java EE 5 web services platform 549 Introducing the web services platform 549 ■ Why choose EJB over a POJO for a web service? 550 15.3 Developing EJB web services with JAX-WS 2.0 551 Using the @WebService annotation 553 ■ Specifying the web service style with @SOAPBinding 555 Using @WebMethod 557 ■ Using the @WebParam annotation 558 ■ Using the @WebResult annotation 560 ■ Using @OneWay and @HandlerChain 561 15.4 Accessing a web service from an EJB 562 Accessing the PlaceBid web service 562 ■ EJB as a web service client 564 15.5 Best practices for web service development 565 15.6 Summary 567
xvi CONTENTS 16 EJB 3 and Spring 568 16.1 Introducing the Spring framework 570 Benefits of the Spring framework 571.The inversion of control principle 571.The separation of concerns principle 572 16.2 Using IPA with Spring 573 Building JPA EAOs for Spring 574.Configuring Spring to use the [PA 577 16.3 Combining the power of EJB 3 and Spring 581 Deueloping Spring-enabled EJBs 581 Using sesion beans from Spring beans 583 16.4 Summary 585 appendix A RMI and INDI 587 appendix B Reviewing relational databases 596 appendix C Annotations reference 603 appendix D Deployment descriptors reference 623 appendix E Installing and configuring the Java EE5 SDK 646 resources 660 index 663
xviii CONTENTS 16 EJB 3 and Spring 568 16.1 Introducing the Spring framework 570 Benefits of the Spring framework 571 ■ The inversion of control principle 571 ■ The separation of concerns principle 572 16.2 Using JPA with Spring 573 Building JPA EAOs for Spring 574 ■ Configuring Spring to use the JPA 577 16.3 Combining the power of EJB 3 and Spring 581 Developing Spring-enabled EJBs 581 ■ Using session beans from Spring beans 583 16.4 Summary 585 appendix A RMI and JNDI 587 appendix B Reviewing relational databases 596 appendix C Annotations reference 603 appendix D Deployment descriptors reference 623 appendix E Installing and configuring the Java EE 5 SDK 646 resources 660 index 663
preface In its early days,EJB was inspired by the distributed computing ideas of tech- nologies such as CORBA and was intended to add scalability to server-side applications.EJB and J2EE enjoyed some of the greatest buzz in the industry during the dot.com boom. The initial goal for EJB was to provide a simpler alternative to CORBA through components and framework benefits-but the component benefits were oversold.By the time EJB2 was released,it became apparent that EJB could be used as a framework to make server-side development easier-but it was com- plicated.It became a heavy framework that provided features such as remoting transaction management,security,state maintenance,persistence,and web ser- vices.It was loaded with more features with each release,and while the devel- opment tools matured,its inventors failed to address the growing complexity As the community became disenchanted with the limitations of EJB 2, innovative open source tools like Spring and Hibernate emerged.They,along with the creeping dominance of Microsoft.NEr and the rise of scripting frameworks like Ruby on Rails,were signs of the increasing discontent with the complexities of Java.It was time for JCP and expert groups to work on the simplification of Java development.That was the sole motivation behind Java EE 5 and the goal of the EJB 3 Expert through Group. For a technology with a wide deployment base,the changes to EJB 3 are nothing short of stunning.EJB 3 successfully melds innovative techniques to xix
xix preface In its early days, EJB was inspired by the distributed computing ideas of technologies such as CORBA and was intended to add scalability to server-side applications. EJB and J2EE enjoyed some of the greatest buzz in the industry during the dot.com boom. The initial goal for EJB was to provide a simpler alternative to CORBA through components and framework benefits—but the component benefits were oversold. By the time EJB 2 was released, it became apparent that EJB could be used as a framework to make server-side development easier—but it was complicated. It became a heavy framework that provided features such as remoting, transaction management, security, state maintenance, persistence, and web services. It was loaded with more features with each release, and while the development tools matured, its inventors failed to address the growing complexity. As the community became disenchanted with the limitations of EJB 2, innovative open source tools like Spring and Hibernate emerged. They, along with the creeping dominance of Microsoft.NET and the rise of scripting frameworks like Ruby on Rails, were signs of the increasing discontent with the complexities of Java. It was time for JCP and expert groups to work on the simplification of Java development. That was the sole motivation behind Java EE 5 and the goal of the EJB 3 Expert through Group. For a technology with a wide deployment base, the changes to EJB 3 are nothing short of stunning. EJB 3 successfully melds innovative techniques to
XX PREFACE make component development as easy as possible.These techniques include Java 5 annotations,metadata programming,dependency injection,AspectJ-like interceptors,as well as intelligent defaulting.The heavyweight inheritance-based programming model was abandoned in favor of POJO programming and the verbose XML descriptor was now out of the developer's way. The changes to the persistence model were particularly dramatic.EJB3 leaves behind the flawed Entity Beans model in favor of the lightweight Java Persistence API (IPA).Unlike Entity Beans,IPA is not container-based.It has more in com- mon with Object Relational Mapping tools such as Hibernate,Oracle TopLink, and JDO than it does with EJB2 CMP Entity Beans.It can be used either inside or outside the container and aims to become the de-facto persistence standard for Java.Its Java Persistence Query Language (JPQL)standardizes object relational queries and supports native SOL queries. We liked the changes made in EJB 3.The simplification of the EJB 3 specifica- tion has been well received in the Java community.Even the competing Spring framework has integrated with JPA and is implementing some features of EJB 3. Since EJB is POJO-based,every Java developer can easily become an EJB devel oper!We felt the need for an EJB 3 book that presents the technology with a fresh approach without too much legacy EJB 2.Together,the three of us have signifi- cant experience using EJB 3,ORM,and lightweight frameworks like Spring.We have strived to keep our book different from other books on EJB 2 and EJB 3 by providing practical examples,best practices,and tips for performance tuning.We do not overlook the case made for competing frameworks such as Spring and do not hesitate to recommend them when these frameworks make sense.In fact.we have dedicated a complete chapter on the interoperability of Spring with EJB 3. We hope that you can use this book to quickly learn how to use EJB 3 effec- tively in your next enterprise applications
xx PREFACE make component development as easy as possible. These techniques include Java 5 annotations, metadata programming, dependency injection, AspectJ-like interceptors, as well as intelligent defaulting. The heavyweight inheritance-based programming model was abandoned in favor of POJO programming and the verbose XML descriptor was now out of the developer’s way. The changes to the persistence model were particularly dramatic. EJB 3 leaves behind the flawed Entity Beans model in favor of the lightweight Java Persistence API (JPA). Unlike Entity Beans, JPA is not container-based. It has more in common with Object Relational Mapping tools such as Hibernate, Oracle TopLink, and JDO than it does with EJB2 CMP Entity Beans. It can be used either inside or outside the container and aims to become the de-facto persistence standard for Java. Its Java Persistence Query Language (JPQL) standardizes object relational queries and supports native SQL queries. We liked the changes made in EJB 3. The simplification of the EJB 3 specification has been well received in the Java community. Even the competing Spring framework has integrated with JPA and is implementing some features of EJB 3. Since EJB is POJO-based, every Java developer can easily become an EJB developer! We felt the need for an EJB 3 book that presents the technology with a fresh approach without too much legacy EJB 2. Together, the three of us have significant experience using EJB 3, ORM, and lightweight frameworks like Spring. We have strived to keep our book different from other books on EJB 2 and EJB 3 by providing practical examples, best practices, and tips for performance tuning. We do not overlook the case made for competing frameworks such as Spring and do not hesitate to recommend them when these frameworks make sense. In fact, we have dedicated a complete chapter on the interoperability of Spring with EJB 3. We hope that you can use this book to quickly learn how to use EJB 3 effectively in your next enterprise applications