EJB 3 in Action
Author: Debu Panda
EJB 2 is widely used but it comes at a cost - procedural, redundant code. EJB 3 is a different animal. By adopting a POJO programming model and Java 5 annotations, it dramatically simplifies enterprise development. A cool new feature, its Java Persistence API, creates a standard for object-relational mapping. You can use it for any Java application, whether inside or outside the EJB container. With EJB 3 you will create true object-oriented applications that are easy to write, maintain and extend.
EJB 3 in Action is a fast-paced tutorial for both novice and experienced Java developers. It will help you learn EJB 3 and the JPA quickly and easily. This comprehensive, entirely new EJB 3 book starts with a tour of the EJB 3 landscape. It then moves quickly into core topics like building business logic with session and message-driven beans. You'll find four full chapters on the JPA along with practical code samples, design patterns, performance tuning tips, and best practices for building and deploying scalable applications.
Table of Contents:
Preface xix
Acknowledgments xxi
About this book xxiv
Overview of the EJB Landscape 1
What's what in EJB 3 3
EJB overview 5
EJB as a component 5
EJB as a framework 7
Layered architectures and EJB 9
Why choose EJB 3? 12
Understanding EJB types 14
Session beans 16
Message-driven beans 16
Entities and the Java Persistence API 17
Getting inside EJB 19
Accessing EJB sendees: the EJB container 20
Accessing JPA services: the persistence provider 20
Gaining functionality with EJB services 21
Renaissance of EJB 23
HelloUser Example 24
Simplified programming model 25
Annotations instead of deployment descriptors 26
Dependency injection vs. JNDI lookup 27
Simplified persistence API 27
Unit-testable POJO components 29
EJB 3 and Spring 30
Summary 32
A first taste of EJB 33
New features: simplifying EJB 35
Replacing deployment descriptors with annotations 35
Introducing dependency injection 40
Introducing the ActionBazaar application 41
Starting with the architecture 42
An EJB 3-based solution 43
Building business logic with session beans 45
Using stateless beans 45
The stateless bean client 47
Using stateful beans 50
A stateful bean client 54
Messaging with message-driven beans 56
Producing a billing message 57
Using the order billing message processor MDB 59
Persisting data with EJB 3 JPA 61
Working with the Java Persistence API 62
Using the EntityManager 65
Summary 67
Building Business Logic with EJB 3 69
Building business logic with session beans 71
Getting to know session beans 72
Why use session beans? 73
Session beans: the basics 75
Understanding the programming rules 78
Conversational state and session bean types 79
Bean lifecycle callbacks 79
Stateless session beans 83
The BidManagerBean example 84
Using the @Stateless annotation 86
Specifying bean business interfaces 87
Using bean lifecycle callbacks 90
Stateful session beans 93
Additional programming rules 94
The BidderAccountCreatorBean example 94
Business interfaces for stateful beans 98
Stateful bean lifecycle callbacks 98
Session bean clients 102
Using the @EJB annotation 104
Injection and stateful session beans 104
Performance considerations for stateful beans 105
Using stateful session beans effectrvely 105
Stateful session bean alternatives 107
Session bean best practices 108
Summary 109
Messaging and developing MDBs 110
Messaging concepts 111
Message-oriented middleware 112
Messaging in Action Bazaar 112
Messaging models 114
Introducing Java Messaging Service 117
Developing the JMS message producer 117
The JMS message interface 119
Working with message-driven beans 121
Why use MDBS? 122
Programming rules 124
Developing a message consumer with MDB 124
Using the @MessageDriven annotation 127
Implementing the MessageListener 127
Using ActivationConfigProperty 128
Using bean lifecycle callbacks 132
Sending JMS messages from MDBs 135
Managing MDB transactions 136
MDB best practices 137
Summary 139
Learning advanced EJB concepts 140
EJB internals 141
EJB behind the scenes 141
EJB context: accessing the runtime environment 143
Accessing resources using DI and JNDI 146
Resource injection using @Resource 146
The @Resource annotation in action 152
Looking up resources and EJBs 155
AOP in the EJB world: interceptors 157
What is AOP? 157
What are interceptors? 158
Specifying interceptors 160
Implementing business interceptors 162
Lifecycle callback methods in the interceptor class 165
Scheduling: the EJB 3 timer service 167
What are timers? 168
Using the timer service 169
When to use EJB timers 174
Summary 175
Transactions and security 176
Understanding transactions 177
A transactional solution in ActionBazaar 178
ACID properties 178
Transaction management internals 181
Two-phase commit 183
Transaction management in EJB 184
Container-managed transactions 185
Snag-It ordering using CMT 185
The @TransactionManagement annotation 186
The @TransactionAttribute annotation 187
Marking a CMT for rollback 191
Transaction and exception handling 193
Bean-managed transactions 196
Snag-It ordering using BMT 196
Getting a UserTransaction 198
Using UserTransaction 199
The pros and cons of BMT 201
Exploring EJB security 202
Authentication vs. authorization 202
Users, groups, and roles 203
A security problem in ActionBazaar 203
EJB 3 and Java EE security 205
Declarative security 208
Using EJB programmatic security 210
Summary 213
3 Diving into the Java Persistence API (JPA) 215
Implementing domain models 217
Domain modeling and the JPA 218
Introducing domain models 219
The ActionBazaar problem domain 219
Domain model actors 222
The EJB 3 Java Persistence API 224
Domain objects as Java classes 225
Implementing domain objects with JPA 227
The @Entity annotation 228
Persisting entity data 229
Specifying entity identity 233
The Embeddable annotation 238
Entity relationships 239
@OneToOne 240
@OneToMany and @ManyToOne 244
@ManyToMany 247
Summary 249
Object-relational mapping 250
The impedance mismatch 251
Mapping objects to databases 252
Introducing O/R mapping 257
Mapping entities 258
Specifying the table 261
Mapping the columns 262
Using @Enumerated 264
Mapping CLOBs and BLOBs 265
Mapping temporal types 265
Mapping an entity to multiple tables 266
Generating primary keys 267
Mapping embeddable classes 270
Mapping entity relationships 273
Mapping one-to-one relationships 273
One-to-many and many-to-one 278
Many-to-many 281
Mapping inheritance 284
Single-table strategy 285
Joined-tables strategy 287
Table-per-class strategy 288
Mapping polymorphic relationships 291
Summary 292
Manipulating entities with EntityManager 293
Introducing the EntityManager 294
The EntityManager interface 294
The lifecycle of an entity 297
Persistence contexts, scope, and the EntityManager 301
Using the EntityManager in ActionBazaar 303
Creating EntityManager instances 304
Container-managed EntityManagers 305
Application-managed EntityManager 308
Managing persistence operations 313
Persisting entities 314
Retrieving entities by primary key 319
Updating entities 325
Deleting entities 328
Controlling updates with flush 330
Refreshing entities 331
Entity lifecycle listeners 333
Using an entity listener 334
Default listener classes 336
Listener class execution order and exclusion 337
Entity operations best practices 338
Summary 338
Using the query API and JPQL to retrieve entities 340
Introducing the query API 341
The big picture 341
Anatomy of a query 343
Defining named queries 344
Executing the queries 345
Creating a query instance 346
Working with the Query interface 347
Specifying query hints 353
Introducing JPQL 354
Defining statement types 356
Using the From clause 358
Conditional expressions and operators 362
Working with JPQL functions 367
Using a Select clause 370
Using aggregations 372
Ordering the query result 374
Using subqueries 374
Joining entities 376
Bulk updates and deletes 379
Native SQL queries 380
Using dynamic queries with native SQL 381
Using a named native SQL query 382
Summary 383
Putting EJB 3 Into Action 385
Packaging EJB 3 applications 387
Packaging your applications 389
Dissecting the Ear file 391
Loading the Ear module 392
Exploring class loading 394
Class-loading basics 394
Exposing the classic parent delegation model 396
Class loading in Java EE applications 398
Dependencies between Java EE modules 398
Packaging session and message-driven beans 400
Packaging EJB-JAR 400
Deployment descriptors vs. annotations 402
Overriding annotations with deployment descriptors 406
Specifying default interceptor settings 407
Using vendor-specific annotations and descriptors 408
Packaging entities 410
Exposing the persistence module 410
Describing the persistence module with persistence.xml 411
Performing O/R mapping with orm.xml 418
Best practices and common deployment issues 422
Packaging and deployment best practices 422
Troubleshooting common deployment problems 423
Summary 424
Effectively integrating EJB 3 across your application tiers 425
Design patterns and web technologies 426
Presentation tier 427
Using the Entity Access Object pattern 429
Visiting the Session Facade pattern 434
Accessing session beans from the web tier 439
Accessing session beans using dependency injection 441
Referencing session beans from helper classes 442
Dealing with transactions 445
Working with stateful session beans 446
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
Summary 454
Taming wild EJBs: performance and scalability 455
Handling entity locking issues 457
Understanding locking types 458
Optimistic locking and entity versioning 461
EntityManager and lock modes 462
Improving entity performance 464
Remodeling and schema redesign 464
Tuning the JDBC layer 468
Reducing database operations 470
Improving query performance 474
Caching 478
Improving performance of EJB 3 components 483
Session bean performance 484
Improving MDB performance 487
Clustering EJB applications 488
Collocated architecture 489
Load-balancing stateless session beans 490
Clustering statefal session beans 493
Entities and clustered cache 495
Summary 496
Migration and Interoperability Issues 497
Migration to EJB 3 499
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
Migrating session beans 505
Converting interface and bean classes 506
Resource usage 510
Transactions and security settings 511
Client applications 511
Migrating message-driven beans 313
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
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
Helping O/R frameworks to use the EJB 3 JPA 533
Approaches to migration 535
Strategies 535
Manual vs. automated 536
Summary 536
Exposing EJBs as web services 537
What is a web service? 539
Identifying web service components 541
Web services styles 547
Approaches to developing web services 548
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
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
Accessing a web service from an EJB 562
Accessing the PlaceBid web service 562
EJB as a web service client 564
Best practices for web service development 565
Summary 567
EJB 3 and Spring 568
Introducing the Spring framework 570
Benefits of the Spring framework 571
The inversion of control principle 571
The separation of concerns principle 572
Using JPA with Spring 573
Building JPA EAOs for Spring 574
Configuring Spring to use the JPA 577
Combining the power of EJB 3 and Spring 581
Developing Spring-enabled EJBs 581
Using session beans from Spring beans 585
Summary 585
RMI and JNDI 587
Reviewing relational databases 596
Annotations reference 603
Deployment descriptors reference 623
Installing and configuring the Java EE 5 SDK 646
Resources 660
Index 663
See also: Bowersox Protocol for Fibromyalgia and Chronic Fatigue or Alzheimers Disease
The Definitive Guide to Django: Web Development Done Right
Author: Adrian Holovaty
Django, the Python-based equivalent to the Ruby on Rails web development framework, is presently one of the hottest topics in web development today. In The Definitive Guide to Django: Web Development Done Right, Adrian Holovaty, one of Django's creators, and Django lead developer Jacob Kaplan-Moss show you how they use this framework to create award-winning web sites. Over the course of three parts, they guide you through the creation of a web application reminiscent of chicagocrime.
The first part of the book introduces Django fundamentals like installation and configuration. You'll learn about creating the components that power a Django-driven web site. The second part delves into the more sophisticated features of Django, like outputting non-HTML content (such as RSS feeds and PDFs), plus caching and user management. The third part serves as a detailed reference to Django's many configuration options and commands. The book even includes seven appendixes for looking up configurations options and commands. In all, this book provides the ultimate tutorial and reference to the popular Django framework.