• Tidak ada hasil yang ditemukan

Advanced Java Programming

N/A
N/A
Protected

Academic year: 2017

Membagikan "Advanced Java Programming"

Copied!
396
0
0

Teks penuh

(1)
(2)

Te c h n o lo g y Ce n t e rs N o t e : Th is t u t o ria l is a va ila b le a s a b o o k fro m o n lin e b o o k s e lle rs Als o , s e n d yo u r

co m m e n t s a n d t h o u g h t s t o

jd cb o o k@s u n . co m

Co n t e n t s

Ch a p t e r 1 : Ma t ch in g Pro je ct Re q u ire m e n t s w it h Te ch n o lo g y

(3)
(4)

S p e cia l t h a n ks t o e xp e rt s Is a a c Elia s , Da n ie l Liu , a n d Ma rk

Ho rw a t h fo r t h e ir co n t rib u t io n s t o t h e a d va n ce d e xa m p le s in t h e b o o k.

Re a d e r Fe e d b a c k

Te ll u s w h a t yo u t h in k o f t h is b o o k.

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(5)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 1 : Ma t c h in g P ro je c t Re q u ire m e n t s w it h Te c h n o lo g y

[< < BACK] [CONTENTS] [NEXT> >]

On e ch a lle n g e in w rit in g a b o o k o n a d va n ce d a p p lica t io n d e ve lo p m e n t fo r t h e Ja vaTM p la t fo rm is t o fin d a p ro je ct s m a ll

e n o u g h t o w rit e a b o u t , w h ile a t t h e s a m e t im e , co m p le x e n o u g h t o w a rra n t a d va n ce d p ro g ra m m in g t e ch n iq u e s .

Th e p ro je ct p re s e n t e d in t h is b o o k is a w e b - b a s e d a u ct io n h o u s e . Th e a p p lica t io n is in it ia lly w rit t e n fo r t h e En t e rp ris e Ja va Be a n sTM p la t fo rm . La t e r ch a p t e rs e xp a n d t h e co re e xa m p le d e s crib e d h e re b y a d d in g a d va n ce d fu n ct io n a lit y, im p ro ve m e n t s , a n d a lt e rn a t ive s o lu t io n s t o d o s o m e o f t h e t h in g s yo u g e t fo r fre e w h e n yo u u s e t h e En t e rp ris e Ja va Be a n s p la t fo rm .

To ke e p t h e d is cu s s io n s im p le , t h e e xa m p le a p p lica t io n h a s o n ly a b a s ic s e t o f t ra n s a ct io n s fo r p o s t in g a n d b id d in g o n a u ct io n it e m s . Ho w e ve r, t h e a p p lica t io n s ca le s t o h a n d le m u lt ip le u s e rs , p ro vid e s a t h re e - t ie re d t ra n s a ct io n - b a s e d e n viro n m e n t , co n t ro ls s e cu rit y, a n d in t e g ra t e s le g a cy- b a s e d s ys t e m s . Th is ch a p t e r co ve rs h o w t o d e t e rm in e p ro je ct re q u ire m e n t s a n d m o d e l t h e

(6)

Te c h n o lo g y Ce n t e rs Pro je ct Re q u ire m e n t s In t e rvie w Us e r Ba s e Mo d e l t h e Pro je ct

Mo d e lin g Ho u s e Id e n t ifie s Bu ye rs a n d S e lle rs Ho u s e De t e rm in e s Hig h e s t Bid d e r Ho u s e No t ifie s Bu ye rs a n d S e lle rs An yo n e S e a rch e s fo r a n It e m An yo n e Vie w s It e m s fo r S a le An yo n e Vie w s It e m De t a ils S e lle r Po s t s It e m s fo r S a le Bu ye r Bid s o n It e m s

Act ivit y Dia g ra m

Ch o o s in g S o ft w a re Ja vaTM APIs

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(7)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 1 Co n t in u e d : P ro je c t Re q u ire m e n t s a n d Mo d e lin g

[< < BACK] [CONTENTS] [NEXT> >]

Th e firs t s t e p in d e t e rm in in g p ro je ct re q u ire m e n t s is t o in t e rvie w t h e u s e r b a s e t o fin d o u t w h a t t h e y w a n t in a n o n lin e a u ct io n . Th is is a n im p o rt a n t s t e p , a n d o n e t h a t ca n n o t b e o ve rra t e d b e ca u s e a s o lid b a s e o f u s e r- o rie n t e d in fo rm a t io n h e lp s yo u d e fin e yo u r ke y a p p lica t io n ca p a b ilit ie s .

Ch a p t e r 2 w a lks t h ro u g h t h e a p p lica t io n co d e , e xp la in s h o w t h e En t e rp ris e Ja va Be a n s p la t fo rm w o rks , a n d t e lls yo u h o w t o ru n a live d e m o n s t ra t io n . If yo u h a ve n e ve r s e e n o r u s e d a n o n lin e a u ct io n , h e re a re m o cku p s o f t h e e xa m p le a u ct io n a p p lica t io n HTML

(8)

Te c h n o lo g y Ce n t e rs Aft e r a n a lyzin g t h e re q u ire m e n t s , yo u ca n b u ild a u s e ca s e

d ia g ra m fo r t h e a p p lica t io n t o g a in a b e t t e r u n d e rs t a n d in g o f t h e e le m e n t s n e e d e d in t h e a p p lica t io n a n d h o w t h e y in t e ra ct .

A u s e ca s e d ia g ra m s h o w s t h e re la t io n s h ip s a m o n g a ct o rs a n d u s e ca s e s w it h in t h e s ys t e m . A u s e ca s e is a u n iq u e fu n ct io n in a

s ys t e m , a n d a n a ct o r is t h e p e rs o n o r s o ft w a re t h a t p e rfo rm s t h e a ct io n o r u s e ca s e . Fo r e xa m p le , a b u ye r is t h e a ct o r t h a t p e rfo rm s t h e fu n ct io n ( u s e ca s e ) o f b id d in g o n a n a u ct io n it e m , a n d t h e

s e lle r is t h e a ct o r t h a t p e rfo rm s t h e u s e ca s e o f p o s t in g a n it e m fo r a u ct io n .

No t a ll a ct o rs a re p e o p le , t h o u g h . Fo r e xa m p le , t h e s o ft w a re is t h e a ct o r t h a t d e t e rm in e s w h e n a n it e m h a s clo s e d , fin d s t h e h ig h e s t b id d e r, a n d n o t ifie s t h e b u ye r a n d s e lle r o f t h e s a le .

Th e Un ifie d Mo d e lin g La n g u a g e ( UML) is t h e t o o l o f ch o ice fo r cre a t in g u s e ca s e d ia g ra m s . Th e Us e Ca s e d ia g ra m b e lo w u s e s UML t o d e s crib e t h e b u ye r a n d s e lle r u s e ca s e s fo r t h e o n lin e a u ct io n a p p lica t io n .

In UML, s ys t e m s a re g ro u p e d in t o s q u a re s , a ct o rs a re re p re s e n t e d b y s t ick fig u re s , u s e ca s e s a re d e n o t e d b y o va ls , a n d t h e lin e s s h o w h o w a ct o rs u s e t h e s ys t e m .

(9)

Th e fo llo w in g d e s crip t io n s fu rt h e r d e fin e t h e p ro je ct . Th e s e

d e s crip t io n s a re n o t p a rt o f UML, b u t a re a h e lp fu l t o o l in p ro je ct d e fin it io n .

Ho u s e I d e n t if ie s B u y e rs a n d S e lle rs

An a u ct io n a p p lica t io n is u s e d b y b u ye rs a n d s e lle rs . A b u ye r n e e d s t o kn o w w h o t h e s e lle r is t o p a y h im o r h e r, a n d t h e s e lle r n e e d s t o kn o w w h o t h e b u ye rs a re t o a n s w e r p ro d u ct q u e s t io n s a n d t o

fin a lize t h e s a le . S o , t o p o s t o r b id o n a n a u ct io n it e m , b u ye rs a n d s e lle rs a re re q u ire d t o re g is t e r. Re g is t ra t io n n e e d s t o g e t t h e

(10)
(11)
(12)

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(13)
(14)

Te c h n o lo g y Ce n t e rs

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(15)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 2 : Au c t io n Ho u s e Ap p lic a t io n

[< < BACK] [ CONTENTS] [NEXT> >]

Th e e xa m p le a p p lica t io n is a w e b - b a s e d a u ct io n h o u s e w rit t e n fo r t h e En t e rp ris e Ja va Be a n sTM p la t fo rm . Th e u s e r in t e rfa ce is a s e t o f

HTML p a g e s t h a t g e t in p u t fro m a n d s h o w in fo rm a t io n t o t h e u s e r.

Be h in d t h e HTML p a g e s is a s e rvle t t h a t p a s s e s d a t a b e t w e e n t h e

b ro w s e r a n d t h e En t e rp ris e Ja va Be a n s s e rve r. Th e En t e rp ris e Ja va Be a n s s e rve r h a n d le s re a d in g fro m a n d w rit in g t o t h e d a t a b a s e .

Th is ch a p t e r d e s crib e s t h e a p p lica t io n co d e , h o w it w o rks w it h t h e En t e rp ris e Ja va Be a n s s e rve r, a n d w h e re t o g e t a En t e rp ris e

(16)
(17)

Duke's Auction

Need to clean out that old office, garage, or

closet? or looking for something so unique you

cannot find it anywhere—or at least not at a price

you are willing to pay?

Look no further. At Duke's Auction you can post

items for sale and bid what you want to pay for

the items you want.

Registration

To bid on or list an item for auction, you must

register

first. Registration gives buyers a way to

pay you and us a way to contact buyers and

sellers. You only need register once, and

registration is not required to browse items on

the auction floor.

Auction Floor

The auction floor is open to anyone for browsing, but to bid on an item, you must be

registered.

New auction items today

Items closing today

All items (current and closed)

Search for Items

Post Items for Auction

Once you register, you can

post items for sale

at auction any time you want.

Register

|

New Items

|

Closing Items

|

All Items

|

Sell Items

(18)
(19)

Te c h n o lo g y Ce n t e rs

(20)

Mu lt it ie re d Arc h it e c t u re

Mu lt it ie r a rch it e ct u re o r t h re e - t ie r a rch it e ct u re e xt e n d s t h e s t a n d a rd t w o - t ie r clie n t a n d s e rve r m o d e l b y p la cin g a m u lt it h re a d e d a p p lica t io n s e rve r b e t w e e n t h e clie n t a n d t h e d a t a b a s e . Clie n t p ro g ra m s co m m u n ica t e w it h t h e d a t a b a s e t h ro u g h t h e a p p lica t io n s e rve r u s in g h ig h - le ve l a n d p la t fo rm in d e p e n d e n t ca lls . Th e a p p lica t io n s e rve r re s p o n d s t o t h e clie n t re q u e s t s , m a ke s d a t a b a s e ca lls a s n e e d e d in t o t h e u n d e rlyin g d a t a b a s e , a n d re p lie s t o t h e clie n t p ro g ra m a s a p p ro p ria t e .

Th e t h re e t ie rs in t h e w e b - b a s e d a u ct io n h o u s e e xa m p le co n s is t s o f t h e t h in - clie n t s e rvle t , t h e En t e rp ris e Ja va Be a n s s e rve r ( t h e a p p lica t io n s e rve r) , a n d t h e d a t a b a s e s e rve r a s s h o w n in t h e fig u re .

En t it y a n d S e s s io n B e a n s

Th e re a re t w o t yp e s o f En t e rp ris e Be a n s : e n t it y Be a n s a n d s e s s io n Be a n s . An En t e rp ris e Be a n t h a t im p le m e n t s a b u s in e s s e n t it y is a n e n t it y Be a n, a n d a n En t e rp ris e Be a n t h a t im p le m e n t s a b u s in e s s t a s k is a s e s s io n Be a n .

Typ ica lly, a n e n t it y Be a n re p re s e n t s o n e ro w o f p e rs is t e n t d a t a s t o re d in a d a t a b a s e t a b le . In t h e a u ct io n h o u s e e xa m p le ,

RegistrationBean is a n e n t it y Be a n t h a t re p re s e n t s d a t a fo r o n e

re g is t e re d u s e r, a n d AuctionItemBean is a n e n t it y Be a n t h a t

re p re s e n t s t h e d a t a fo r o n e a u ct io n it e m . En t it y Be a n s a re t ra n s a ct io n a l a n d lo n g - live d . As lo n g a s t h e d a t a re m a in s , t h e e n t it y Be a n ca n a cce s s a n d u p d a t e t h a t d a t a . Th is d o e s n o t m e a n yo u n e e d a Be a n ru n n in g fo r e ve ry t a b le ro w . In s t e a d , En t e rp ris e Be a n s a re lo a d e d a n d s a ve d a s n e e d e d .

A s e s s io n Be a n m ig h t e xe cu t e d a t a b a s e re a d s a n d w rit e s , b u t it is

(21)

m ig h t u s e a n e n t it y Be a n t o m a ke t h e ca ll, in w h ich ca s e t h e

s e s s io n Be a n is a clie n t t o t h e e n t it y Be a n . A s e s s io n Be a n 's fie ld s co n t a in t h e s t a t e o f t h e co n ve rs a t io n a n d a re t ra n s ie n t . If t h e s e rve r o r clie n t cra s h e s , t h e s e s s io n Be a n is g o n e . A s e s s io n Be a n is o ft e n u s e d w it h o n e o r m o re e n t it y Be a n s a n d fo r co m p le x

o p e ra t io n s o n t h e d a t a .

S e s s io n Be a n s En t it y Be a n s

Fie ld s co n t a in co n ve rs a t io n

s t a t e . Re p re s e n t s d a t a in a d a t a b a s e .

Ha n d le s d a t a b a s e a cce s s fo r

clie n t . S h a re s a cce s s fo r m u lt ip le u s e rs .

Life o f clie n t is life o f Be a n . Pe rs is t s a s lo n g a s d a t a e xis t s . Ca n b e t ra n s a ct io n a w a re . Tra n s a ct io n a l.

Do e s n o t s u rvive s e rve r

cra s h e s . S u rvive s s e rve r cra s h e s .

No t fin e - g ra in e d d a t a h a n d lin g Fin e - g ra in e d d a t a h a n d lin g

N o t e : In t h e En t e rp ris e Ja va Be a n s s p e cifica t io n ,

En t e rp ris e Ja va Be a n s S e rve r s u p p o rt fo r s e s s io n Be a n s is m a n d a t o ry. En t e rp ris e Ja va Be a n s s e rve r s u p p o rt fo r e n t it y Be a n s w a s o p t io n a l, b u t is m a n d a t o ry fo r ve rs io n 2 . 0 o f t h e s p e cifica t io n .

Au c t io n Ho u s e W o rk in g s

Th e d ia g ra m s h o w s t h e En t e rp ris e Be a n s fo r t h e a u ct io n h o u s e a p p lica t io n a n d t h e ir re la t io n s h ip t o t h e En t e rp ris e Ja va Be a n s s e rve r. Th e t h in - clie n t s e rve r in vo ke s b u s in e s s lo g ic in t h e fo u r En t e rp ris e Be a n s t h ro u g h t h e ir h o m e a n d re m o t e in t e rfa ce s . Th e En t e rp ris e Ja va Be a n s s e rve r in t h is e xa m p le h a n d le s t h e lo w - le ve l d e t a ils in clu d in g d a t a b a s e re a d a n d w rit e o p e ra t io n s .

(22)
(23)
(24)

s e rvice s t o lo ca t e re m o t e s e rve r o b je ct s ( t h e Be a n 's re m o t e

in t e rfa ce o b je ct ) , a n d d a t a co m m u n ica t io n s e rvice s t o m o ve d a t a fro m t h e clie n t , t h ro u g h t h e re m o t e s e rve r o b je ct , t o it s fin a l d e s t in a t io n in a s t o ra g e m e d iu m .

Lo o k u p S e rv ic e

To fin d re m o t e s e rve r o b je ct s a t ru n t im e , t h e clie n t p ro g ra m n e e d s a w a y t o lo o k t h e m u p . On e w a y t o lo o k re m o t e s e rve r o b je ct s u p a t ru n t im e is t o u s e t h e Ja va Na m in g a n d Dire ct o ry In t e rfa ceTM ( JNDI) API. JNDI is a co m m o n in t e rfa ce t o e xis t in g n a m in g a n d d ire ct o ry in t e rfa ce s . Th e En t e rp ris e Ja va Be a n s co n t a in e rs u s e JNDI a s a n in t e rfa ce t o t h e Re m o t e Me t h o d In vo ca t io n ( RMI) n a m in g s e rvice .

At d e p lo ym e n t t im e , t h e JNDI s e rvice re g is t e rs ( b in d s ) t h e re m o t e in t e rfa ce w it h a n a m e . As lo n g a s t h e clie n t p ro g ra m u s e s t h e s a m e n a m in g s e rvice a n d a s ks fo r t h e re m o t e in t e rfa ce b y it s re g is t e re d n a m e , it w ill b e a b le t o fin d it . Th e clie n t p ro g ra m ca lls t h e lookup m e t h o d o n a javax.naming.Context o b je ct t o a s k fo r t h e

re m o t e in t e rfa ce b y it s re g is t e re d n a m e . Th e javax.naming.Context

o b je ct is w h e re t h e b in d in g s a re s t o re d a n d is a d iffe re n t o b je ct fro m t h e En t e rp ris e Ja va Be a n s co n t e xt , w h ich is co ve re d la t e r.

D a t a Co m m u n ic a t io n

On ce t h e clie n t p ro g ra m g e t s a re fe re n ce t o a re m o t e s e rve r o b je ct , it m a ke s ca lls o n t h e re m o t e s e rve r o b je ct 's m e t h o d s . Be ca u s e t h e clie n t p ro g ra m h a s a re fe re n ce t o t h e re m o t e s e rve r o b je ct , a t e ch n iq u e ca lle d d a t a m a rs h a llin g is u s e d t o m a ke it

a p p e a r a s if t h e re m o t e s e rve r o b je ct is lo ca l t o t h e clie n t p ro g ra m . Da t a m a rs h a llin g is w h e re m e t h o d s ca lle d o n t h e re m o t e s e rve r o b je ct a re w ra p p e d w it h t h e ir d a t a a n d s e n t t o t h e re m o t e s e rve r o b je ct . Th e re m o t e s e rve r o b je ct u n w ra p s ( u n m a rs h a lls ) t h e m e t h o d s a n d d a t a , a n d ca lls t h e En t e rp ris e Be a n . Th e re s u lt s o f t h e ca ll t o t h e En t e rp ris e Be a n a re w ra p p e d a g a in , p a s s e d b a ck t o t h e clie n t t h ro u g h t h e re m o t e s e rve r o b je ct , a n d u n m a rs h a lle d . Th e En t e rp ris e Ja va Be a n s co n t a in e rs u s e RMI s e rvice s t o m a rs h a l d a t a . Wh e n t h e Be a n is co m p ile d , stub a n d skeleton file s a re

cre a t e d . Th e stub file p ro vid e s t h e d a t a w ra p p in g a n d u n w ra p p in g

co n fig u ra t io n o n t h e clie n t , a n d t h e skeleton p ro vid e s t h e s a m e

in fo rm a t io n fo r t h e s e rve r.

Th e d a t a is p a s s e d b e t w e e n t h e clie n t p ro g ra m a n d t h e s e rve r u s in g s e ria liza t io n . S e ria liza t io n is a w a y t o re p re s e n t a t Ja vaTM

(25)

o b je ct s a s b yt e s t h a t ca n b e s e n t o ve r t h e n e t w o rk a s a s t re a m a n d re co n s t ru ct e d o n t h e o t h e r s id e in t h e s a m e s t a t e t h e y w e re in w e n t o rig in a lly s e n t .

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(26)
(27)

Te c h n o lo g y Ce n t e rs N o t e : Th is t u t o ria l is a va ila b le a s a b o o k fro m o n lin e b o o k s e lle rs Als o , s e n d yo u r

co m m e n t s a n d t h o u g h t s t o

jd cb o o k@s u n . co m

Co n t e n t s

Ch a p t e r 1 : Ma t ch in g Pro je ct Re q u ire m e n t s w it h Te ch n o lo g y

(28)
(29)

S p e cia l t h a n ks t o e xp e rt s Is a a c Elia s , Da n ie l Liu , a n d Ma rk

Ho rw a t h fo r t h e ir co n t rib u t io n s t o t h e a d va n ce d e xa m p le s in t h e b o o k.

Re a d e r Fe e d b a c k

Te ll u s w h a t yo u t h in k o f t h is b o o k.

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(30)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 2 Co n t in u e d : En t it y a n d S e s s io n B e a n s

[< < BACK] [CONTENTS] [NEXT> >]

Th e e xa m p le u s e s t w o e n t it y Be a n s a n d t w o s e s s io n Be a n s . Th e e n t it y Be a n s , AuctionItemBean a n d RegistrationBean, re p re s e n t

p e rs is t e n t it e m s t h a t co u ld b e s t o re d in a d a t a b a s e , a n d t h e s e s s io n Be a n s , SellerBean a n d BidderBean, re p re s e n t s h o rt - live d

o p e ra t io n s w it h t h e clie n t a n d d a t a .

(31)

Te c h n o lo g y Ce n t e rs

Th e AuctionServlet m e t h o d s s h o w n a b o ve in vo ke b u s in e s s lo g ic

t h a t e xe cu t e s o n t h e s e rve r b y lo o kin g u p a n En t e rp ris e Be a n a n d ca llin g o n e o r m o re o f it s m e t h o d s . Wh e n t h e s e rvle t a d d s HTML

co d e s t o a p a g e fo r d is p la y t o t h e u s e r, t h a t lo g ic e xe cu t e s o n t h e clie n t .

Fo r e xa m p le , t h e listAllItems(out) m e t h o d e xe cu t e s co d e o n t h e

clie n t t o d yn a m ica lly g e n e ra t e a n HTML p a g e t o b e vie w e d b y t h e

clie n t in a b ro w s e r. Th e HTML p a g e is p o p u la t e d w it h t h e re s u lt s o f

a ca ll t o BidderBean t h a t e xe u ct e s lo g ic o n t h e s e rve r t o g e n e ra t e a

lis t o f a ll a u ct io n it e m s .

private void listAllItems(ServletOutputStream out) throws IOException{

//Put text on HTML page

setTitle(out, "Auction results");

String text = "Click Item number for description and to place bid.";

try{

addLine("<BR>"+text, out);

//Look up Bidder bean home interface.

BidderHome bhome=(BidderHome) ctx.lookup("bidder"); //Create Bidder bean remote interface.

Bidder bid=bhome.create();

//Call Bidder bean method through remote interface. Enumeration enum=(Enumeration)bid.getItemList();

(32)

//Put retrieved items on servlet page. displayitems(enum, out);

addLine("", out); }

} catch (Exception e) { //Pring error on servlet page.

addLine("AuctionServlet List All Items error",out); System.out.println("AuctionServlet <list>:"+e); }

out.flush(); }

En t it y B e a n s

AuctionItemBean a n d RegistrationBean a re e n t it y Be a n s .

AuctionItemBean a d d s n e w a u ct io n it e m s t o t h e d a t a b a s e a n d

u p d a t e s t h e b id a m o u n t a s u s e rs b id o n t h e it e m . RegistrationBean

a d d s in fo rm a t io n t o t h e d a t a b a s e o n re g is t e re d u s e rs . Bo t h Be a n s co n s is t o f t h e cla s s e s d e s crib e d h e re .

Au c t io n I t e m En t it y B e a n

He re a re t h e AuctionItemBean cla s s e s . Re m e m b e r t h a t t h e s e

(33)

s e rve r re q u ire s a co n t a in e r- m a n a g e d e n t it y Be a n t o h a ve a

p rim a ry ke y cla s s w it h a p u b lic p rim a ry ke y fie ld ( o r fie ld s , if u s in g co m p o s it e p rim a ry ke ys ) . Th e Be a n d e ve lo p e r im p le m e n t s t h is cla s s . Th e ID fie ld is t h e p rim a ry ke y in t h e AUCTIONITEMS t a b le

s h o w n ju s t b e lo w , s o t h e id fie ld is a p u b lic fie ld in t h is cla s s . Th e id fie ld is a s s ig n e d a va lu e w h e n t h e p rim a ry ke y cla s s is

co n s t ru ct e d .

Yo u ca n re q u e s t t h e co n t a in e r m a n a g e d a t a b a s e p e rs is t e n ce fo r a n En t e rp ris e Be a n o r w rit e t h e co d e t o m a n a g e t h e p e rs is t e n ce yo u rs e lf. In t h is ch a p t e r, a ll b e a n s ( e n t it y a n d s e s s io n ) a re

co n t a in e r- m a n a g e d . Wit h co n t a in e r- m a n a g e d Be a n s , a ll yo u d o is s p e cify w h ich fie ld s a re co n t a in e r m a n a g e d a n d le t t h e En t e rp ris e Ja va Be a n s s e rve r d o t h e re s t . Th is is g re a t fo r s im p le a p p lica t io n s , b u t if yo u a re co d in g s o m e t h in g t h a t is fa irly co m p le x, yo u m ig h t n e e d m o re co n t ro l.

Ho w t o o ve rrid e t h e u n d e rlyin g En t e rp ris e Ja va Be a n s s e rvice s t o g a in m o re co n t ro l o r p ro vid e s im ila r s e rvice s fo r n o n - En t e rp ris e Ja va Be a n a p p lica t io n s is co ve re d in Ch a p t e r 3 .

Au c t io n I t e m s Ta b le

He re is t h e AUCTIONITEMS t a b le .

create table AUCTIONITEMS (SUMMARY VARCHAR(80) , ID INT ,

COUNTER INT ,

DESCRIPTION VARCHAR(1000) , STARTDATE DATE ,

ENDDATE DATE ,

STARTPRICE DOUBLE PRECISION , INCREMENT DOUBLE PRECISION , SELLER VARCHAR(30) ,

MAXBID DOUBLE PRECISION, BIDCOUNT INT,

HIGHBIDDER VARCHAR(30) )

Re g is t ra t io n En t it y B e a n

RegistrationBean co n s is t s o f t h e s a m e kin d s o f cla s s e s a n d

d a t a b a s e t a b le a s t h e AuctionItem Be a n , e xce p t t h e a ct u a l

b u s in e s s lo g ic, d a t a b a s e t a b le fie ld s , a n d p rim a ry ke y a re s o m e w h a t d iffe re n t . Ra t h e r t h a n d e s crib e t h e cla s s e s , yo u ca n b ro w s e t h e m a n d re fe r b a ck t o t h e AuctionItem Be a n d is cu s s io n if

(34)
(35)

clie n t ca lls re m o t e ly.

BidderHome is t h e h o m e in t e rfa ce . It d e s crib e s h o w t h e Be a n is

cre a t e d in , fo u n d in , a n d re m o ve d fro m it s co n t a in e r.

BidderBean is t h e En t e rp ris e Be a n . It im p le m e n t s SessionBean,

p ro vid e s t h e b u s in e s s lo g ic fo r t h e d e ve lo p e r- d e fin e d m e t h o d s , a n d im p le m e n t s SessionBean m e t h o d s fo r cre a t in g t h e Be a n a n d

s e t t in g t h e s e s s io n co n t e xt .

Auction.java co n t a in s a s m a ll cla s s t h a t d e cla re s va ria b le s u s e d b y

BidderBean.

S e lle r S e s s io n B e a n

SellerBean co n s is t s o f t h e s a m e kin d s o f cla s s e s a s BidderBean,

e xce p t t h e b u s in e s s lo g ic is d iffe re n t . Ra t h e r t h a n d e s crib e t h e cla s s e s , yo u ca n b ro w s e t h e m a n d re fe r b a ck t o t h e BidderBean

(36)

t h e s e rve r.

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(37)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 2 Co n t in u e d : Ex a m in in g a Co n t a in e r- Ma n a g e d B e a n

[< < BACK] [CONTENTS] [NEXT> >]

Th is s e ct io n w a lks t h ro u g h t h e Re g is t ra t io n Be a n . ja va co d e t o s h o w h o w e a s y it is t o h a ve t h e co n t a in e r m a n a g e p e rs is t e n t d a t a

s t o ra g e t o a n u n d e rlyin g m e d iu m s u ch a s a d a t a b a s e ( t h e d e fa u lt ) . Ch a p t e r 3 m o d ifie s RegistrationBean t o u s e Be a n - m a n a g e d

(38)

Te c h n o lo g y Ce n t e rs public String theuser, password, creditcard, emailaddress;

public double balance;

Cre a t e Me t h o d

Th e Be a n 's ejbCreate m e t h o d is ca lle d b y t h e co n t a in e r a ft e r t h e

clie n t p ro g ra m ca lls t h e create m e t h o d o n t h e remote in t e rfa ce a n d

p a s s e s in t h e re g is t ra t io n d a t a . Th is m e t h o d a s s ig n s t h e in co m in g va lu e s t o t h e m e m b e r va ria b le s t h a t re p re s e n t u s e r d a t a . Th e co n t a in e r h a n d le s s t o rin g a n d lo a d in g t h e d a t a , a n d cre a t in g n e w e n t rie s in t h e u n d e rlyin g s t o ra g e m e d iu m .

public RegistrationPK ejbCreate(String theuser, String password, String emailaddress, String creditcard) throws CreateException, RemoteException {

this.theuser=theuser; this.password=password;

this.emailaddress=emailaddress; this.creditcard=creditcard; this.balance=0;

En t it y Co n t e x t Me t h o d s

An e n t it y Be a n h a s a n a s s o cia t e d EntityContext in s t a n ce t h a t g ive s

t h e Be a n a cce s s t o co n t a in e r- m a n a g e d ru n t im e in fo rm a t io n s u ch a s t h e t ra n s a ct io n co n t e xt .

public void setEntityContext(

javax.ejb.EntityContext ctx) throws RemoteException { this.ctx = ctx;

}

public void unsetEntityContext() throws RemoteException{ ctx = null;

}

Lo a d Me t h o d

Th e Be a n 's ejbLoad m e t h o d is ca lle d b y t h e co n t a in e r t o lo a d d a t a

fro m t h e u n d e rlyin g s t o ra g e m e d iu m . Th is w o u ld b e n e ce s s a ry w h e n BidderBean o r SellerBean n e e d t o ch e ck a u s e r's ID o r

p a s s w o rd a g a in s t t h e s t o re d va lu e s .

N o t e : No t a ll Be a n o b je ct s a re live a t a n y o n e t im e . Th e En t e rp ris e Ja va Be a n sTM s e rve r m ig h t h a ve a co n fig u ra b le

(39)

n u m b e r o f Be a n s t h a t it ke e p s in m e m o ry.

Th is m e t h o d is n o t im p le m e n t e d b e ca u s e t h e En t e rp ris e Ja va Be a n s co n t a in e r s e a m le s s ly lo a d s t h e d a t a fro m t h e u n d e rlyin g s t o ra g e m e d iu m fo r yo u .

public void ejbLoad() throws RemoteException {}

S t o re Me t h o d

Th e Be a n 's ejbStore m e t h o d is ca lle d b y t h e co n t a in e r t o s a ve u s e r

d a t a . Th is m e t h o d is n o t im p le m e n t e d b e ca u s e t h e En t e rp ris e Ja va Be a n s co n t a in e r s e a m le s s ly s t o re s t h e d a t a t o t h e u n d e rlyin g s t o ra g e m e d iu m fo r yo u .

public void ejbStore() throws RemoteException {}

Co n n e c t io n P o o lin g

Lo a d in g d a t a fro m a n d s t o rin g d a t a t o a d a t a b a s e ca n t a ke a lo t t im e a n d re d u ce a n a p p lica t io n 's o ve ra ll p e rfo rm a n ce . To re d u ce d a t a b a s e co n n e ct io n t im e , t h e BEA We b lo g ic s e rve r u s e s a JDBCTM co n n e ct io n p o o l t o ca ch e d a t a b a s e co n n e ct io n s s o co n n e ct io n s a re a lw a ys a va ila b le w h e n t h e a p p a lica t io n n e e d s t h e m .

Ho w e ve r, yo u a re n o t lim it e d t o t h e d e fa u lt JDBC co n n e ct io n p o o l. Yo u ca n o ve rrid e t h e Be a n - m a n a g e d co n n e ct io n p o o lin g b e h a vio u r a n d s u b s t it u t e yo u r o w n . Ch a p t e r 8 : Pe rfo rm a n ce Te ch n iq u e s

e xp la in s h o w .

D e p lo y m e n t D e s c rip t o r

Th e re m a in in g co n fig u ra t io n fo r a co n t a in e r- m a n a g e d p e rs is t e n t Be a n s o ccu rs a t d e p lo ym e n t t im e . Th e fo llo w in g is t h e t e xt - b a s e d De p lo ym e n t De s crip t o r u s e d in a BEA We b lo g ic En t e rp ris e

Ja va Be a n s s e rve r.

Te x t D e p lo y m e n t D e s c rip t o r

(environmentProperties

(persistentStoreProperties

persistentStoreType jdbc

(jdbc

tableName registration dbIsShared false

poolName ejbPool

(40)
(41)

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(42)
(43)

Te c h n o lo g y Ce n t e rs

Th is s e ct io n w a lks t h ro u g h t h e d iffe re n t p a rt s o f t h e finder- b a s e d

s e a rch co d e . Ch a p t e r 3 d e s crib e s h o w t o cre a t e a Be a n - m a n a g e d s e a rch t o h a n d le co m p le x q u e rie s a n d s e a rch e s t h a t s p a n m o re t h a n o n e Be a n t yp e ( e n t it y a n d s e s s io n Be a n s ) o r d a t a b a s e t a b le s .

(44)
(45)
(46)

"(= enddate $closedtoday)"

"findAllMatchingItems(String searchString)" "(like summary $searchString)"

); end finderDescriptors

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(47)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 3 : D a t a a n d Tra n s a c t io n Ma n a g e m e n t

[< < BACK] [CONTENTS] [NEXT> >]

Wh e n yo u u s e t h e En t e rp ris e Ja va Be a n sTM a rch it e ct u re , d a t a is w rit t e n t o a n d re a d fro m t h e d a t a b a s e w it h o u t yo u r w rit in g a n y S QL co d e t o d o it . Bu t w h a t if yo u d o n o t w a n t t o s t o re t h e d a t a in a d a t a b a s e , o r w a n t t o w rit e yo u r o w n S QL co m m a n d s , o r m a n a g e t ra n s a ct io n s ? Yo u ca n o ve rrid e t h e b u ilt - in co n t a in e r- m a n a g e d p e rs is t e n ce a n d im p le m e n t Be a n - m a n a g e d p e rs is t e n ce u s in g yo u r o w n d a t a s t o ra g e a n d t ra n s a ct io n m a n a g e m e n t co d e .

Be a n - m a n a g e d p e rs is t e n ce co m e s in u s e fu l w h e n yo u w a n t m o re co n t ro l t h a n t h e co n t a in e r- m a n a g e d p e rs is t e n ce p ro vid e s . Fo r e xa m p le yo u m ig h t w a n t t o o ve rrid e t h e d e fa u lt o f m o s t

co n t a in e rs t o m a p t h e d a t a in o n e Be a n t o o n e ro w in a t a b le , im p le m e n t yo u r o w n finder m e t h o d s , o r cu s t o m ize ca ch in g .

Th is ch a p t e r p re s e n t s t w o ve rs io n s o f t h e RegistrationBean cla s s

fro m Ch a p t e r 2 . On e ve rs io n re a d s u s e r d a t a fro m a n d w rit e s it t o a file u s in g s e ria lize d in p u t a n d o u t p u t s t re a m s . Th e o t h e r ve rs io n p ro vid e s it s o w n S QL co m m a n d s fo r re a d in g fro m a n d w rit in g t o t h e d a t a b a s e . It a ls o e xp la in s h o w yo u ca n w rit e yo u r o w n

(48)

Te c h n o lo g y Ce n t e rs Be a n - Ma n a g e d Pe rs is t e n ce a n d t h e JDBC Pla t fo rm

(49)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 3 Co n t in u e d : B e a n - Ma n a g e d P e rs is t e n c e a n d t h e JD B CTM P la t f o rm

[< < BACK] [CONTENTS] [NEXT> >]

Th e re m ig h t b e t im e s w h e n yo u w a n t t o o ve rrid e co n t a in e r-m a n a g e d p e rs is t e n ce a n d ir-m p le r-m e n t e n t it y o r s e s s io n Be a n

m e t h o d s t o u s e t h e S QL co m m a n d s yo u p ro vid e . Th is t yp e o f Be a n -m a n a g e d p e rs is t e n ce ca n b e u s e fu l if yo u n e e d t o i-m p ro ve

p e rfo rm a n ce o r m a p d a t a in m u lt ip le Be a n s t o o n e ro w in a d a t a b a s e t a b le .

(50)
(51)
(52)
(53)
(54)
(55)

Te c h n o lo g y Ce n t e rs

W h y Ma n a g e Tra n s a c t io n s ?

Wh e n yo u a cce s s a d a t a b a s e s u s in g t h e JDBCTM a p p lica t io n

p ro g ra m m in g in t e rfa ce ( API) , a ll o p e ra t io n s a re ru n w it h a n e xp licit a u t o co m m it b y d e fa u lt . Th is m e a n s a n y o t h e r a p p lica t io n vie w in g t h is d a t a w ill s e e t h e u p d a t e d d a t a a ft e r e a ch JDBC ca ll.

Fo r s im p le a p p lica t io n s t h is m a y b e a cce p t a b le , b u t co n s id e r t h e a u ct io n a p p lica t io n a n d t h e s e q u e n ce s t h a t o ccu r w h e n SellerBean

in s e rt s a n a u ct io n it e m . Th e u s e r's a cco u n t is firs t ch a rg e d fo r lis t in g t h e it e m , a n d t h e it e m is t h e n a d d e d t o t h e lis t o f it e m s u p fo r a u ct io n . Th e s e o p e ra t io n s in vo lve RegistrationBean t o d e b it t h e

a cco u n t a n d AuctionItemBean t o a d d t h e it e m t o t h e a u ct io n lis t .

In a u t o co m m it m o d e , if t h e a u ct io n it e m in s e rt io n fa ils , o n ly t h e lis t in g is b a cke d o u t , a n d yo u h a ve t o m a n u a lly a d ju s t t h e u s e r's a cco u n t t o re fu n d t h e lis t in g ch a rg e . In t h e m e a n t im e , a n o t h e r t h re a d m ig h t t ry t o d e d u ct fro m t h e s a m e u s e r's a cco u n t , fin d n o cre d it le ft , a n d a b o rt w h e n p e rh a p s a fe w m illis e co n d s la t e r it w o u ld h a ve co m p le t e d .

Th e re a re t w o w a ys t o e n s u re t h e d e b it is b a cke d o u t if t h e a u ct io n it e m in s e rt io n fa ils :

(56)

Co n t a in e r- Ma n a g e d Ex a m p le

SellerBean is a s e s s io n Be a n t h a t u s e s RegistrationBean t o ch e ck

t h e u s e r ID a n d p a s s w o rd w h e n s o m e o n e p o s t s a n a u ct io n it e m a n d d e b it t h e s e lle r's a cco u n t fo r a lis t in g , a n d AuctionItemBean t o

a d d n e w a u ct io n it e m s t o t h e d a t a b a s e .

Th e t ra n s a ct o n b e g in s in t h e insertItem m e t h o d w it h t h e a cco u n t

d e b it a n d e n d s w h e n t h e e n t ire t ra n s a ct io n e it h e r co m m it s o r ro lls b a ck. Th e e n t ire t ra n s a ct io n in clu d in g t h e 5 0 ce n t s d e b it ro lls b a ck if t h e a u ct io n it e m is null ( t h e in s e rt io n fa ile d ) , o r if a n e xce p t io n

is ca u g h t . If t h e a u ct io n it e m is n o t null a n d t h e in s e rt io n

s u cce e d s , t h e e n t ire t ra n s a ct io n in clu d in g t h e 5 0 ce n t s d e b it co m m it s .

Co d e

To u s e s e s s io n s yn ch ro n iza t io n , a s e s s io n Be a n im p le m e n t s t h e

SessionSynchronzation in t e rfa ce a n d it s t h re e m e t h o d s , afterBegin, beforeCompletion, a n d afterCompletion. Th is e xa m p le a d a p t s t h e

S e lle rBe a n . ja va co d e t o u s e s e s s io n s yn ch ro n iza t io n .

public class SellerBean implements SessionBean, SessionSynchronization {

private transient SessionContext ctx;

private transient Properties p = new Properties(); private transient boolean success = true;

public void afterBegin() {}

public void beforeCompletion() { if (!success ) {

ctx.setRollbackOnly(); }

}

public void afterCompletion(boolean state) {}

afterBegin: Th e co n t a in e r ca lls t h is m e t h o d b e fo re t h e d e b it t o

n o t ify t h e s e s s io n Be a n a n e w t ra n s a ct io n is a b o u t t o b e g in . Yo u ca n im p le m e n t t h is m e t h o d t o d o a n y p re lim in a ry d a t a b a s e w o rk t h a t m ig h t b e n e e d e d fo r t h e t ra n s a ct io n . In t h is e xa m p le , n o p re lim in a ry d a t a b a s e w o rk is n e e d e d s o t h is m e t h o d h a s n o im p le m e n t a t io n .

beforeCompletion: Th e co n t a in e r ca lls t h is m e t h o d w h e n it is re a d y

t o w rit e t h e a u ct io n it e m a n d d e b it t o t h e d a t a b a s e , b u t b e fo re it a ct u a lly d o e s ( co m m it s ) . Yo u ca n im p le m e n t t h is m e t h o d t o w rit e o u t a n y ca ch e d d a t a b a s e u p d a t e s o r ro ll b a ck t h e t ra n s a ct io n . In

(57)
(58)

}

//Call to beforeCompletion //Call to afterCompletion

}

Tra n s a c t io n Co m m it Mo d e

If yo u co n fig u re t h e JDBC s e rvice s t o t ra n s a ct io n co m m it m o d e , yo u ca n h a ve t h e Be a n m a n a g e t h e t ra n s a ct io n . To s e t t h e JDBC s e rvice s t o co m m it , ca ll con.setAutoCommit(false) o n yo u r JDBC

co n n e ct io n . No t a ll JDBC d rive rs s u p p o rt co m m it m o d e , b u t t o h a ve t h e Be a n co n t ro l a n d m a n a g e t ra n s a ct io n s , yo u n e e d a JDBC d rive r t h a t d o e s .

Tra n s a ct io n co m m it m o d e le t s yo u a d d co d e t h a t cre a t e s a s a fe t y n e t a ro u n d a s e q u e n ce o f d e p e n d e n t o p e ra t io n s . Th e Ja vaTM

Tra n s a ct io n API ( JTA) p ro vid e s t h e h o o ks yo u n e e d t o cre a t e t h a t s a fe t y n e t . Bu t , if yo u a re u s in g t h e En t e rp ris e Ja va Be a n s

a rch it e ct u re , yo u ca n d o it w it h a lo t le s s co d e . Yo u o n ly h a ve t o co n fig u re t h e En t e rp ris e Ja va Be a n s s e rve r, a n d s p e cify w h e re t h e t ra n s a ct io n s t a rt s , s t o p s , ro lls b a ck, a n d co m m it s in yo u r co d e .

S e rv e r Co n f ig u ra t io n

(59)
(60)

He re a re t h e t ra n s a ct io n a t t rib u t e s w it h a b rie f d e s crip t io n fo r e a ch o n e . Th e a t t rib u t e n a m e s ch a n g e d b e t w e e in t h e 1 . 0 a n d 1 . 1

ve rs io n s o f t h e En t e rp ris e Ja va Be a n s s p e cifica t io n . 1 . 1 S p e cifica t io n 1 . 0 S p e cifica t io n

REQUIRED TX_REQUIRED

Co n t a in e r- m a n a g e d t ra n s a ct io n . Th e s e rve r e it h e r s t a rt s a n d m a n a g e s a n e w t ra n s a ct io n o n b e h a lf o f t h e u s e r o r co n t in u e s u s in g t h e t ra n s a ct io n t h a t w a s s t a rt e d b y t h e co d e t h a t ca lle d t h is Be a n .

REQUIRESNEW TX_REQUIRED_NEW

Co n t a in e r- m a n a g e d t ra n s a ct io n . Th e s e rve r s t a rt s a n d m a n a g e s a n e w t ra n s a ct io n . If a n e xis t in g t ra n s a ct io n s t a rt s t h is t ra n s a ct io n , it s u s p e n d s u n t il t h is t ra n s a ct io n co m p le t e s .

Specified as Bean transaction-type in deployment descriptor

TX_BEAN_MANAGED

Be a n - m a n a g e d t ra n s a ct io n . Yo u a cce s s t h e t ra n s a ct io n co n t e xt t o b e g in , co m m it , o r ro llb a ck t h e t ra n s a ct io n a s n e e d e d .

SUPPORTS TX_SUPPORTS

If t h e co d e ca llin g t h is Be a n h a s a t ra n s a ct io n ru n n in g , in clu d e t h is Be a n in t h a t t ra n s a ct io n .

NEVER TX_NOT_SUPPORTED

If t h e co d e ca llin g a m e t h o d in t h is Be a n h a s a t ra n s a ct io n

ru n n in g , s u s p e n d t h a t t ra n s a ct io n u n t il t h e m e t h o d ca lle d in t h is Be a n co m p le t e s . No t ra n s a ct io n co n t e xt is cre a t e d fo r t h is Be a n .

MANDATORY TX_MANDATORY

Th e t ra n s a ct io n a t t rib u t e fo r t h is Be a n is s e t w h e n a n o t h e r Be a n ca lls o n e o f it s m e t h o d s . In t h is ca s e , t h is Be a n g e t s t h e

t ra n s a ct io n a t t rib u t e o f t h e ca llin g Be a n . If t h e ca llin g Be a n h a s n o t ra n s a ct io n a t t rib u t e , t h e m e t h o d ca lle d in t h is Be a n t h ro w s a

TransactionRequired e xce p t io n .

I s o la t io n Le v e l D e s c rip t io n s: An e n t e rp ris e Be a n u s e s a n

is o la t io n le v e l t o n e g o t ia t e it s o w n in t e ra ct io n w it h s h a re d d a t a a n d t h e in t e ra ct io n o f o t h e r t h re a d s w it h t h e s a m e s h a re d d a t a . As t h e n a m e im p lie s , t h e re a re va rio u s le ve ls o f is o la t io n w it h

TRANSACTION_SERIALIZABLE p ro vid in g t h e h ig h e s t le ve l o f d a t a

in t e g rit y.

(61)

N o t e : Be s u re t o ve rify yo u r d a t a b a s e ca n h a n d le t h e le ve l yo u ch o o s e . In t h e En t e rp ris e Ja va Be a n s 1 . 1 s p e cifica t io n , o n ly Be a n - m a n a g e d p e rs is t e n ce s e s s io n Be a n s ca n s e t t h e is o la t io n le ve l.

If t h e d a t a b a s e ca n n o t h a n d le t h e is o la t io n le ve l, t h e En t e rp ris e Ja va Be a n s s e rve r w ill g e t a fa ilu re w h e n it t rie s t o ca ll t h e setTransactionIsolation JDBC m e t h o d .

TRANSACTION_SERIALIZABLE: Th is le ve l p ro vid e s m a xim u m d a t a

in t e g rit y. Th e Be a n g e t s w h a t a m o u n t s t o e xclu s ive a cce s s t o t h e d a t a . No o t h e r t ra n s a ct io n ca n re a d o r w rit e t h is d a t a u n t il t h e s e ria liza b le t ra n s a ct io n co m p le t e s .

S e ria liza b le in t h is co n t e xt m e a n s p ro ce s s a s a s e ria l o p e ra t io n, a n d s h o u ld n o t b e co n fu s e d w it h s e ria lizin g o b je ct s t o p re s e rve a n d re s t o re t h e ir s t a t e s . Ru n n in g t ra n s a ct io n s a s a s in g le s e ria l

o p e ra t io n is t h e s lo w e s t s e t t in g . If p e rfo rm a n ce is a n is s u e , u s e a n o t h e r is o la t io n le ve l t h a t m e e t s yo u r a p p lica t io n re q u ire m e n t s , b u t p ro vid e s b e t t e r p e rfo rm a n ce .

TRANSACTION_REPEATABLE_READ: At t h is le ve l, d a t a re a d b y a

t ra n s a ct io n ca n b e re a d , b u t n o t m o d ifie d , b y a n o t h e r t ra n s a ct io n . Th e d a t a is g u a ra n t e e d t o h a ve t h e s a m e va lu e it h a d w h e n firs t re a d , u n le s s t h e firs t t ra n s a ct io n ch a n g e s it a n d w rit e s t h e ch a n g e d va lu e b a ck.

TRANSACTION_READ_COMMITTED: At t h is le ve l, d a t a re a d b y a

t ra n s a ct io n ca n n o t b e re a d b y o t h e r t ra n s a ct io n s u n t il t h e fris t t ra n s a ct io n e it h e r co m m it s o r ro lls b a ck.

TRANSACTION_READ_UNCOMMITTED: At t h is le ve l, d a t a in vo lve d in a t ra n s a ct io n ca n b e re a d b y o t h e r t h re a d s b e fo re t h e firs t

t ra n s a ct io n e it h e r co m p le t e s o r ro lls b a ck. Th e o t h e r t ra n s a ct io n s ca n n o t t e ll if t h e d a t a w a s fin a lly co m m it t e d o r ro lle d b a ck

B e a n - Ma n a g e d Ex a m p le

SellerBean is a s e s s io n Be a n t h a t u s e s RegistrationBean t o ch e ck

t h e u s e r ID a n d p a s s w o rd w h e n s o m e o n e p o s t s a n a u ct io n it e m a n d d e b it t h e s e lle r's a cco u n t fo r a lis t in g , a n d AuctionItemBean t o

a d d n e w a u ct io n it e m s t o t h e d a t a b a s e .

Th e t ra n s a ct o n b e g in s in t h e insertItem m e t h o d w it h t h e a cco u n t

d e b it a n d e n d s w h e n t h e e n t ire t ra n s a ct io n e it h e r co m m it s o r ro lls b a ck. Th e e n t ire t ra n s a ct io n in clu d in g t h e 5 0 ce n t s d e b it ro lls b a ck

(62)
(63)

if(ai == null) { //Roll transaction back uts.rollback();

return Auction.INVALID_ITEM; }

else {

//Commit transaction uts.commit();

return(ai.getId()); }

}catch(Exception e){

System.out.println("insert problem="+e); //Roll transaction back if insert fails

uts.rollback();

return Auction.INVALID_ITEM; }

}

[TOP]

[ Th is p a g e w a s u p d a t e d : 4 - Ju n - 2 0 0 1 ]

Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt Co m m u n it y Dis cu s s io n | In d u s t ry Ne w s | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s

Glo s s a ry | Fe e d b a ck | A- Z In d e x

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(64)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 3 Co n t in u e d : B e a n - Ma n a g e d f in d e r Me t h o d s

[< < BACK] [CONTENTS] [NEXT> >]

Th e co n t a in e r- m a n a g e d s e a rch d e s crib e d in Ch a p t e r 2 is b a s e d o n a finder m e t h o d m e ch a n is m w h e re t h e d e p lo ym e n t d e s crip t o r,

ra t h e r t h a n t h e Be a n , s p e cifie s t h e finder m e t h o d b e h a vio r. Wh ile

t h e finder m e ch a n is m w o rks w e ll fo r s im p le q u e rie s a n d s e a rch e s ,

it ca n n o t h a n d le co m p le x o p e ra t io n s t h a t s p a n m o re t h a n o n e Be a n t yp e o r d a t a b a s e t a b le . Als o , t h e En t e rp ris e Ja va Be a n sTM 1 . 1

s p e cifica t io n cu rre n t ly p ro vid e s n o s p e cifica t io n fo r p u t t in g finder

ru le s in t h e d e p lo ym e n t d e s crip t o r.

S o , fo r m o re co m p le x q u e rie s a n d s e a rch e s , yo u h a ve t o w rit e Be a n - m a n a g e d q u e rie s a n d s e a rch e s . Th is s e ct io n e xp la in s h o w t o w rit e a Be a n - m a n a g e d ve rs io n o f t h e a u ct io n h o u s e s e a rch fa cilit y fro m Ch a p t e r 2 . Th e Be a n - m a n a g e d s e a rch in vo lve s ch a n g e s t o t h e AuctionServlet.searchItems m e t h o d a n d a n e w s e s s io n Be a n ,

(65)
(66)
(67)
(68)
(69)

Te c h n o lo g y Ce n t e rs

(70)
(71)

Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y

a n d o t h e r s o ft w a re fro m S u n Micro s ys t e m s , ca ll:

( 8 0 0 ) 7 8 6 - 7 6 3 8

Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's

AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e. Priva cy Po licy.

(72)

Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

B u g D a t a b a s e

S u b m it a B u g Vie w D a t a b a s e

N e w s le t t e rs

B a c k I s s u e s S u b s c rib e

Le a rn in g Ce n t e rs

Art ic le s B o o k s h e lf Co d e S a m p le s N e w t o Ja v a

Qu e s t io n o f t h e W e e k Qu iz z e s

Te c h Tip s Tu t o ria ls

Fo ru m s

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s

Ch a p t e r 4 : Lo o k u p S e rv ic e s

[< < BACK] [CONTENTS] [NEXT> >]

(73)

Te c h n o lo g y Ce n t e rs

Referensi

Dokumen terkait

Peta Jalur Penelitian di Stasiun Penelitian Hutan Batang Toru Blok Barat Tapanuli Utara... Foto

Pada penelitian ini didapati bahwa kelompok umur terbanyak penderita PPOK eksaserbasi dan PPOK stabil adalah kelompok umur diatas 69 tahun dan tidak ada perbedaan secara

Jika sumber daya yang dimiliki memiliki intelektual untuk mengelolah aktiva dengan baik, agar tidak mengandung atau menghasilkan resiko akibat dari kerugian bank

Pengembangan dan komersialisasi varietas unggul baru memerlukan promosi. penyediaan benih yang memadai, insentif bagi produsen benih, dan dorongan kebijakan dalam

Gambar 4.22 Halaman Untuk Mengubah Data Jabatan Bagian

Gambar 7 menjelaskan hasil pengukuran return loss pada filter berada pada frekuensi tengah 2,425 GHz sebesar -11.42 dB yang ditandai pada marker 2.. Bandwidth yang ditunjukkan

Ranah kognitif adalah perubahan perilaku yang terjadi dalam kawasan kognisi. Proses belajar yang melibatkan kawasan kognitif imeliputi kegiatan sejak dari penerimaan

yang nantinya digunakan untuk mengukur variabel motivasi belajar siswa. Dengan maksud untuk memberikan informasi mengenai butir-butir yang di