Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Mastering Enterprise JavaBeans™ and the Java 2 Platform, Enterprise Edition - Roman E

..pdf
Скачиваний:
41
Добавлен:
24.05.2014
Размер:
6.28 Mб
Скачать

Mastering Enterprise

JavaBeans

and the Java2 Platform,

Enterprise Edition

Ed Roman

Buy this book/CD-ROM set online at Amazon.com

Wiley Computer Publishing

John Wiley & Sons, Inc.

NEW YORK • CHICHESTER • WEINHEIM • BRISBANE • SINGAPORE • TORONTO

Publisher: Robert Ipsen

Editor: Robert M. Elliott

Managing Editor: Brian Snapp

Electronic Products, Associate Editor: Mike Sosa

Text Design & Composition: Rob Mauhar

Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where John Wiley & Sons, Inc., is aware of a claim, the product names appear in initial capital or all capital letters. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration.

Sun, Sun Microsystems, the Sun Logo, Enterprise JavaBeans, Java, and JNDI are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

This book is printed on acid-free paper.

Copyright © 1999 by Ed Roman. All rights reserved.

Published by John Wiley & Sons, Inc.

Published simultaneously in Canada.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4744. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 605 Third Avenue, New York, NY 10158-0012, (212) 850-6011, fax (212) 850-6008, e-mail: PERMREQ@WILEY.COM.

This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold with the understanding that the publisher is not engaged in professional services. If professional advice or other expert assistance is required, the services of a competent professional person should be sought.

Library of Congress Cataloging-in-Publication Data:

0-471-33229-1

Printed in the United States of America.

10 9 8 7 6 5 4 3 2 1

Go back to the first page for a quick link to buy this book online!

A D VA N C E P R A I S E

What People Are Saying about Ed Roman’s

Mastering Enterprise JavaBeans and the Java 2 Platform, Enterprise Edition...

“Ed Roman has done a great job of explaining a complex topic: how to build Java applications for the middle tier. Not only does he explain how to program with EJB, he explains how to design applications so that they can use EJB intelligently. This is a great starting place for programmers who are trying to move from simplistic client/server applications to true multi-tier development using the official Java middle-tier platform.”

—Roger Sessions, President, Objectwatch

Author, “ObjectWatch Newsletter”

“This book is a must-have for developers who want to jumpstart their EJB development process. Ed Roman shows the right way to use the J2EE technology with indepth examples and coding patterns from the real world. We recommend this book as part of our education materials for both in-house staff and customer engagements.”

—William W. Lee, Chief Technology Officer, The Theory Center

“Enterprise JavaBeans and the J2EE are among the most important technologies in enterprise computing. Organizations that are exploring or implementing missioncritical, Web-based, and distributed systems should understand the role that the Enterprise Java platform can play. Ed Roman has done an excellent job of taking this complex subject and explaining it in a clear and practical manner. I recommend this book to anyone who wants to increase their knowledge and expertise in building robust, ‘real-world’ computing systems.”

—Doug Hibberd, Chief Technology Officer, iMARK.COM

“This book explains all fundamentals of EJB wrapped up in an easy to follow set of examples. It is easy enough for the beginner and covers enough for more experienced users to like it. It also provides the reader with a guide to what you should consider when buying an EJB server, as well as a brief look into the future and what’s coming next in this exciting new technology.”

—Rickard ÖBerg, Software Architect, DreamBean

“This book starts off innocently enough with the idea of explaining Enterprise JavaBeans. However, by the end, you realize that Ed Roman has effectively unwrapped the onion that is today’s multi-tier architecture and shown how J2EE can revolutionize how systems are architected. I highly recommend this book to anyone who wishes to keep up with the latest in Java technology and internet systems architecture.”

—Mike Perham, Senior Web Developer, living.com

Go back to the first page for a quick link to buy this book online!

To my family:

Mom, Dad, and Mike

Go back to the first page for a quick link to buy this book online!

C O N T E N TS

Preface

 

xix

About the Author

xxii

Introduction

 

xxiii

Part I:

Overview

1

 

 

Chapter 1 Server-side Component Architectures

3

 

The Need for a Server-Side Component Architecture

4

 

Software Components

4

 

Component Architectures

7

 

The Needs of the Server Side

12

 

Multi-tier Architectures

13

 

Server-Side Component Architecture Solutions

23

 

Microsoft’s Distributed interNet Applications Architecture

24

 

Sun Microsystems’s Java 2 Platform, Enterprise Edition (J2EE)

25

 

The Object Management Group’s CORBA Standard

26

 

The Java 2 Platform, Enterprise Edition

27

 

Why J2EE?

27

 

The J2EE Technologies

29

 

Enterprise JavaBeans (EJB)

31

 

Remote Method Invocation (RMI) and RMI-IIOP

32

 

Java Naming and Directory Interface (JNDI)

33

 

Java Database Connectivity (JDBC)

34

 

Java Transaction API (JTA) and Java Transaction Service (JTS)

35

 

Java Messaging Service (JMS)

36

 

Java Servlets and Java Server Pages (JSPs)

37

 

Java IDL

38

 

JavaMail

38

 

Connectors

39

 

The Extensible Markup Language (XML)

40

 

Summary

40

Chapter 2 Enterprise JavaBeans Overview

41

 

Who’s Who in Enterprise JavaBeans

42

v

Go back to the first page for a quick link to buy this book online!

vi

 

M A S T E R I N G E N T E R P R I S E J A V A B E A N S

 

 

 

 

The Six Parties

43

 

 

 

The Bean Provider

45

 

 

 

The EJB Server and EJB Container Providers

46

 

 

 

The Application Assembler

49

 

 

 

The EJB Deployer

50

 

 

 

The System Administrator

50

 

 

 

Enterprise Beans

51

 

 

 

Types of Beans

51

 

 

 

Motivation for Multiple Bean Types

59

 

 

 

Overview of EJB Container and EJB Server Responsibilities

59

 

 

 

Resource Management and Bean Life Cycle Management

62

 

 

 

State Management

62

 

 

 

Transactions

63

 

 

 

Security

64

 

 

 

Persistence

64

 

 

 

Remote Accessibility and Location Transparency

64

 

 

 

Glue-Code and Bean Installation Tools

65

 

 

 

Specialized Container Features

65

 

 

 

Summary

67

 

 

Part II:

Developing Beans

69

 

 

 

 

 

 

Chapter 3 Introduction to Session Beans

71

 

 

 

What Constitutes an Enterprise Bean?

71

 

 

 

The Enterprise Bean Class

72

 

 

 

The EJB Object

73

 

 

 

The Home Object

77

 

 

 

Deployment Descriptors

78

 

 

 

Bean-Specific Properties

81

 

 

 

Ejb-jar File

81

 

 

 

Summary of Terms

82

 

 

 

What Is a Session Bean?

83

 

 

 

Session Bean Lifetime

83

 

 

 

Conversational versus Nonconversational Session Beans

84

 

 

 

All Session Beans’ Methods Are Serialized

85

 

 

 

Understanding How to Write Session Beans

85

 

 

 

setSessionContext(SessionContext ctx)

86

 

 

 

ejbCreate(...)

86

 

 

 

ejbPassivate()

87

 

 

 

ejbActivate()

88

 

 

 

ejbRemove()

89

 

 

 

Business Methods

89

 

 

 

Understanding How to Call Session Beans

90

 

 

 

Looking Up a Home Object

91

Go back to the first page for a quick link to buy this book online!

Contents

 

vii

Creating an EJB Object

94

Calling a Method

94

Destroying the EJB Object

94

Summary

95

Chapter 4 The Basics of Stateless Session Beans

97

Characteristics of Stateless Session Beans

97

No Conversational State

97

Only One Way to Initialize Stateless Sessions

98

Containers Can Pool and Reuse Stateless Sessions

98

EJB Object Decoupling

99

Writing a “Hello, World!” Stateless Session Bean

99

Constructing the “Hello, World!” Remote Interface

100

Implementing the “Hello, World!” Bean

101

Constructing the “Hello, World!” Home Interface

104

Writing the Deployment Descriptor

105

The Environment Properties

107

The Ejb-jar File

108

Deploying the Bean

110

Writing Client Code for Our Stateless Bean

111

Running the System

112

Summary

114

Chapter 5 The Basics of Stateful Session Beans

115

Characteristics of Stateful Session Beans

115

Achieving the Effect of Pooling with Stateful Beans

115

The Rules Governing Conversational State

117

Activation/Passivation Callbacks

118

A Simple Stateful Session Bean

118

Stateful or Stateless?

128

Myths and Facts about Statelessness

128

Summary

130

Chapter 6 Adding Functionality to Your Beans

133

EJB Contexts: Your Gateway to the Container

133

Session Bean Contexts

134

SessionContext.getEJBObject()

136

Understanding EJB Security

136

Step 1: Authentication

137

Step 2: Authorization

137

Security Propagation

141

Understanding EJB Object Handles

142

Example: The Puzzle Game “Fazuul”

143

What Is Fazuul?

143

Go back to the first page for a quick link to buy this book online!

viii

 

M A S T E R I N G E N T E R P R I S E J A V A B E A N S

 

 

 

Specifying Fazuul in EJB

145

 

 

Making the Game Data-Driven through Environment Properties

145

 

 

Implementing Fazuul

146

 

 

Running the Client

170

 

 

Summary

174

 

 

Chapter 7 Introduction to Entity Beans

175

 

 

Persistence Concepts

176

 

 

Java Object Serialization

176

 

 

Object-Relational Mapping

176

 

 

Object Databases

178

 

 

What Is an Entity Bean?

179

 

 

Files Included with Entity Beans

181

 

 

Features of Entity Beans

182

 

 

Entity Beans Are Long-Lived

183

 

 

Entity Beans Survive Failures

183

 

 

Entity Bean Instances Are a View into a Database

183

 

 

Several Entity Bean Instances May Represent the Same

 

 

 

Underlying Data

184

 

 

Entity Bean Instances Can Be Pooled

185

 

 

There Are Two Ways to Persist Entity Beans

188

 

 

Entity Beans Can Be Created, Removed, or Found

190

 

 

Entity Beans Can Represent Legacy Data and Legacy Systems

191

 

 

Entity Beans Can Be Modified without Going through EJB

191

 

 

Developing and Using Entity Beans

192

 

 

Creation of Entity Beans: ejbCreate()

193

 

 

Finding Existing Entity Beans: ejbFind()

194

 

 

Destruction of Entity Beans: ejbRemove()

197

 

 

Entity Contexts

197

 

 

getEJBObject()

199

 

 

getPrimaryKey()

199

 

 

Putting It All Together: Walking through an Entity Bean

 

 

 

Life Cycle

202

 

 

Summary

204

 

 

Chapter 8 Writing Bean-Managed Persistent Entity Beans

207

 

 

Implementation Guidelines for Bean-Managed Persistence

207

 

 

Bean-Managed Persistence Example: A Bank Account

211

 

 

Account.java

212

 

 

AccountHome.java

213

 

 

AccountPK.java

214

 

 

AccountBean.java

214

 

 

AccountException.java

227

 

 

Client.java

228

Go back to the first page for a quick link to buy this book online!

 

 

Contents

 

ix

 

 

The Deployment Descriptor

230

 

 

Environment Properties

231

 

 

Setting Up the Database

231

 

 

Running the Client Program

232

 

 

Server-Side Output

232

 

 

Client-Side Output

234

 

 

Summary

234

 

Chapter 9 Writing Container-Managed Persistent Entity Beans

235

 

 

Container-Managed Fields

235

 

 

Primary Key Class

236

 

 

Implementation Guidelines for Container-Managed

 

 

 

Persistence

237

 

 

Container-Managed Persistence Example: A Product Line

241

 

 

Product.java

242

 

 

ProductHome.java

242

 

 

ProductPK.java

244

 

 

ProductBean.java

244

 

 

Client.java

250

 

 

The Deployment Descriptor

252

 

 

Running the Client Program

253

 

 

Server-Side Output

254

 

 

Client-Side Output

255

 

 

Promises and Realities: Bean-Managed Persistence versus

 

 

 

Container-Managed Persistence

255

 

 

Promise: Container-Managed Persistence Reduces Code

256

 

 

Promise: Container-Managed Persistence Reduces Bugs

256

 

 

Promise: Container-Managed Persistence Makes It Easy to

 

 

 

Port to Different Databases

257

 

 

Resolving Your EJB Debugging Problems

258

 

 

Summary

260

 

Part III:

Advanced Enterprise JavaBeans Concepts

261

 

 

 

 

 

Chapter 10

Transactions

263

 

 

The Motivation for Transactions

264

 

 

Atomic Operations

264

 

 

Network or Machine Failure

265

 

 

Multiple Users Sharing Data

266

 

 

The Benefits of Transactions

266

 

 

The ACID Properties

268

 

 

Transactional Models

270

 

 

Flat Transactions

270

Go back to the first page for a quick link to buy this book online!

x

 

M A S T E R I N G E N T E R P R I S E J A V A B E A N S

Nested Transactions

272

Other Transactional Models

274

Enlisting in Transactions with Enterprise JavaBeans

274

Transaction Models Supported

274

Underlying Transaction System Abstraction

274

Declarative and Programmatic Transactions

275

Controlling How Your Enterprise Beans Are Enrolled in

 

Transactions

276

EJB Transaction Attribute Values

278

Transactional Isolation

281

The Need for Concurrency Control

282

Isolation and EJB

283

The Dirty Read Problem

285

The Unrepeatable Read Problem

287

The Phantom Problem

288

Transaction Isolation Summary

289

Distributed Transactions

290

Durability and the Two-Phase Commit Protocol

290

The Transactional Communications Protocol and

 

Transaction Contexts

291

Programmatic Transactions in EJB

293

CORBA’s Object Transaction Service (OTS)

293

The Java Transaction Service (JTS)

294

The Java Transaction API (JTA)

294

Declarative versus Programmatic Transactions Example

297

How to Control Transactions from Client Code

298

Designing Transactional Conversations in EJB

299

Summary

302

Chapter 11 CORBA and RMI-IIOP

303

What Is CORBA?

303

CORBA as the Basis for EJB

304

Why Should I Care about CORBA?

304

Benefits of Using CORBA

305

Drawbacks of CORBA

305

Understanding How CORBA Works

306

Object Request Brokers

306

Object Implementations and Object References

308

Object Adapters

308

Repositories

309

OMG’s Interface Definition Language

310

OMG IDL Maps to Concrete Languages

312

Static Invocations

313

Dynamic Invocations

314

Go back to the first page for a quick link to buy this book online!