Jboss-ejb

Embed Size (px)

Citation preview

  • 7/25/2019 Jboss-ejb

    1/35

    Notes on JBoss Application Server and EJB 3.0Created 03/15/08Updated 03/20/08, Updated 04/04/08, Updated 04/25/08, Updated 05/12/08, Updated 05/19/08, Updated 05/27/08Updated 06/08/08, Updated 06/15/08, Updated 06/27/08, Updated 07/12/08, Updated 07/20/08, Updated 08/02/08Updated 09/08/08, Updated 08/11/08, Updated 12/17/08, Updated 02/25/09, Updated 02/28/09, Updated 03/23/09Updated 03/26/09, Updated 05/31/09, Updated 08/22/09, Updated 09/14/09

    IntroductionThese notes cover usage of the !oss "pp#$cat$on %erver and the &ost $&portant spec$f$cat$on that $t $&p#e&ents'(! 3)0) !oss $s a soft*are product, and (!3)0 $s part of a #arger spec$f$cat$on ca##ed ava (( 5)0) (! 3)0focuses on the serv$ces that support +ac-end +us$ness #og$c and pers$stence, *h$#e ava (( 5)0 $nc#udesspec$f$cat$ons for presentat$on too#s, secur$t., *e+ serv$ces, %, and &ore)

    The !oss "pp#$cat$on %erver $s the #atest $n a ser$es of re#eases of a 2((-co&p#$ant app#$cat$on server) t dates+ac to around 2001-2002, and has +een great#. $&proved each .ear) !as$ca##., an app#$cat$on server #$e th$sprov$des the fo##o*$ng'

    ata+ase connect$on &anage&ent

    ers$stence &anage&ent Co&ponent deve#op&ent the co&ponents are ca##ed (nterpr$se ava !eans

    Transact$on anage&ent

    %ecur$t. anage&ent

    e+ %erv#et %upport

    C#uster$ng and a$#-ver %upport

    ther 2(( $nterfaces and standards, $nc#ud$ng %, avaa$#, ava e+ %erv$ces, and &ore

    !oss $s #$e a super-vers$on of To&cat, prov$d$ng data &anage&ent and pers$stence, $n add$t$on to :TT re;uestprocess$ng !oss uses a port$on of To&cat for th$s purpose) !oss $nc#udes conta$ner &anage&ent of pers$stence,$t $nc#udes separat$on of *e+ app#$cat$on and +us$ness #og$c port$ons< $t $nc#udes the = $nfrastructure, and $t$nc#udes &essage-dr$ven +eans *$th %)

    !oss $s such a #arge set of fac$#$t$es that $t $s hard to #earn *h$ch ones to use under *hat cond$t$ons) :ence, thereare +oth reference &anua#s, and usage-or$ented tutor$a#s)

    The standard that $s &ost spec$f$c to 2(( app#$cat$on servers no* ca##ed (( app#$cat$on servers, *$th there#ease of ava 5 $s the (nterpr$se ava !ean (! standard) Th$s has +een great#. rev$sed fro& vers$on 1)0 to 2)0to 3)0) (! 3)0 $s a &a>or step for*ard, and $s a pr$&ar. focus of th$s docu&ent)

    h$#e not d$scussed here, the &a>or co&pet$ng app#$cat$on server products $nc#ude %un?s @#assf$sh, and !("?se+Aog$c, !?s e+%phere, and rac#e?s C4) n ear#. 2008, rac#e ac;u$red !(")

    Th$s docu&ent +eg$ns *$th the re#ease spec$f$cat$on notes for !oss, and then focuses on (! 3)0 and re#ated ava(( 5)0 serv$ces for the &a$n teBt) Then $t conc#udes *$th a nu&+er of !oss-+ased eBa&p#es, der$ved fro& theor+oo part of the !ure and onson-:aefe# +oo) Th$s docu&ent a#so conta$ns a nu&+er of p#aces *here%ea& $s d$scussed, part$cu#ar#. ho* %ea& eBtends the sess$on +ean &anage&ent fac$#$t$es to prov$de statefu#co&ponents)

    JBoss Current Version and DownloadsThe current sta+#e vers$on of !oss $s 5)1)0)@", re#eased a. 23, 2009) The 5)B)B ser$es $s $&portant +ecause $t $sthe f$rst vers$on $&p#e&ent$ng a## of ava (( 1)5) :o*ever, &an. deve#op&ent efforts are st$## on the 4)2)B ser$es,and th$s docu&ent *as $n$t$a##. *r$tten for the 4)2)B ser$es) %o *e *$## $gnore the 5)0)B/5)1)B ser$es for no*)

    age 1 of 35

  • 7/25/2019 Jboss-ejb

    2/35

    The current 4)2)B vers$on as of th$s *r$t$ng $s 4)2)3)@" re#eased u#. 18, 2008) The 4)2)B vers$ons are $&portant+ecause the. are the f$rst vers$ons to dep#o. (!3 nat$ve#.) The 4)2)3 vers$on $nc#udes a nu&+er of f$Bes andnu&+er of updated dependent #$+rar$es such as %, "=-%, !oss Cache, and avass$st) %pec$f$c patches $n 4)2)3$nc#ude'

    Upgrade >+oss transact$ons to 4)2)3)%7 fro& 4)2)3)%6

    Upgrade % to 1)208 Upgrade >+oss cache to 1)4)1)%7 fro& 1)4)1)%5

    Upgrade to >+ossB+ 1)0)0)%3 fro& 1)0)0)%1

    "## the no*n +$nd address propert$es shou#d +e set to the defau#t +$nd address

    Upgrade >+oss re&ot$ng to v2)2)2)%5 fro& v2)2)2)%1

    Transact$on var$a+#e $s not reset $n e>+2 $nf#o* $nterceptor

    Upgrade >+oss*s to 2)0)4)@" fro& 2)0)1)%2

    "dd a transact$on status $nterface for the connect$on &anager and $&p#e&ent $t $n TBConnect$onanager

    "dd the transact$on act$ve chec to the >d+c resource adapter

    "dd the transact$on act$ve chec to the >&s resource adapter

    Upgrade >avass$st to 3)8)0)@"

    Upgrade >+oss re&ot$ng to v2)2)2)%8 fro& v2)2)2)%5

    Upgrade to the #atest % $&p#e&entat$on 1)209 fro& 1)208 Upgrade !oss Cache to 1)4)1)%9

    The 4)2)B re#eases are cons$dered trans$t$ona# +et*een 4)B and 5)B, +ecause the. $&p#e&ent (! 3)0, +ut are st$##cons$dered a ava (( 1)4 product rather than ava (( 5) $th &ost#. ne* support for (( too#s and techno#og$esthat are e. to ava (( 5) The &a>or ne* $&prove&ents $n 4)2)B $nc#ude'

    u## support of "=-%, +. $nc#ud$ng vers$on 2)0)B of "=-% as the *e+ serv$ces $&p#e&entat$on

    u## support of ava 1)6

    To&cat 6)0, *h$ch adds support for % 2)1 and %erv#et 2)5

    ava%erver aces 1)2 $ntegrat$on th$s $s +ased on %un?s $&p#e&entat$on

    !oss Transact$ons "r>una T" 4)2)3 as the defau#t transact$on &on$tor

    ntegrat$on of :$+ernate 3)2)3)@" ntegrat$on of :$+ernate "nnotat$ons 3)3)0)@"

    There *ere a nu&+er of 4)0)B vers$ons, *h$ch $&p#e&ent a ava (( 1)4 app#$cat$on server, *$th e&+edded "pacheTo&cat 5)5) The. $nc#uded ava%erver aces 1)1 us$ng "pache .aces) "n. ava D$rtua# ach$ne$&p#e&entat$on +et*een vers$ons 1)4 and 1)5 $s supported) The 4)0)B vers$ons of !oss *ere re#eased $n #ate 2005through 2007)

    Resources!oss $n "ct$on' Conf$gur$ng the !oss "pp#$cat$on %erver +. av$d a&ae and eter ohnson) ann$ng ress,anuar. 2009, 496 pages) A$st pr$ce E49)99, "&aFon pr$ce E31)49, used fro& E23)77) Gated 5 stars on "&aFon)co&fro& 3 rev$e*s) Th$s $s the f$rst +oo to focus on teach$ng readers $n deta$# ho* to use the !oss app#$cat$onserver) Un#$e other t$t#es a+out !oss, the authors of H!oss $n "ct$on? go deeper $nto the advanced features and

    conf$gurat$on of the server) n part$cu#ar, $t focuses on enterpr$se-c#ass top$cs, such as h$gh ava$#a+$#$t., secur$t., andperfor&ance) Th$s +oo *a#s readers through the !oss 5 "pp#$cat$on %erver fro& $nsta##at$on to conf$gurat$on toproduct$on deve#op&ent) t $s not as &uch of a deve#oper?s gu$de as $t $s an ad&$n$strator?s gu$de and an arch$tect?sgu$de to so&e of the advanced features, *h$ch $nc#ude !oss %ea& and !oss orta#)

    (nterpr$se ava !eans 3)0 $fth (d$t$on +. !$## !ure and G$chard onson-:aefe#) ?Ge$##. ress, a. 2006,760 pages) A$st pr$ce E49)99, "&aFon pr$ce E34)66, used fro& E20)12) Gated 4)5 stars on "&aFon) Th$s f$fthed$t$on, *r$tten +. !$## !ure and G$chard onson-:aefe#, has +een updated to capture the ver. #atest need-to-no*ava techno#og$es $n the sa&e a*ard-*$nn$ng fash$on that drove the success of the prev$ous four strong-se##$ng

    age 2 of 35

  • 7/25/2019 Jboss-ejb

    3/35

    ed$t$ons) !$## !ure, Ch$ef "rch$tect at !oss, nc), represents the co&pan. on the (! 3)0 and ava (( 5spec$f$cat$on co&&$ttees) G$chard onson-:aefe# $s one of the *or#dIs #ead$ng eBperts on (nterpr$se ava) The+oo $s organ$Fed $nto t*o parts' the techn$ca# &anuscr$pt fo##o*ed +. the !oss *or+oo) The techn$ca#&anuscr$pt eBp#a$ns *hat (! $s, ho* $t *ors, and *hen to use $t) The !oss *or+oo prov$des step-+.-step$nstruct$ons for $nsta##$ng, conf$gur$ng, and runn$ng the eBa&p#es fro& the &anuscr$pt on the !oss 4)0)B"pp#$cat$on %erver) !u. th$s +oo)

    (! 3 $n "ct$on +. anda, Gah&an, and Aane) ann$ng ress, "pr$# 2007, 712 pages) A$st pr$ce E49)99, "&aFonpr$ce E39)40, used fro& E34)50) Gated 4 stars on "&aFon)co&) Cons$dered to +e a ver. n$ce and +road deve#oper?sgu$de coverage of (! 3 *$th a ver. s$&p#e and eBce##ent#. crafted case stud.) The +oo starts ver. #$ght#. on th$sco&p#eB su+>ect and s#o*#. d$ves $nto the deta$#s of advanced concepts #$e ", $nterceptors, transact$ons, secur$t.,and perfor&ance $ssues) f deve#ops each fac$#$t. *$th$n the case stud.) e #$ed the $dea of case stud. start$ngs$&p#e and deve#op$ng $t +ased on co&p#eB$t.) %o&e rev$e*s stated that $t *as a ver. good +oo *$th a ver.s&ooth read, *h$#e other rev$e*s stated that $t *as poor#. *orded, ho*ever) The +oo a#so has co&par$sons of (!3 *$th %pr$ng) Th$s +oo $s ava$#a+#e as an e-+oo $n the ((( Co&puter %oc$et. %afar$ +ooshe#f)

    !eg$nn$ng (! 3 "pp#$cat$on eve#op&ent' ro& Jov$ce to rofess$ona# +. Koda#$, eather+., and Ledron$)"ress, %epte&+er 2006, 512 pages) A$st pr$ce E44)95, "&aFon pr$ce E35)66, used fro& E17)88) Gated 4 stars on"&aFon) p$n$ons var$ed great#., fro& 1 star to 5 stars) %$&$#ar $n content to the a+ove +oos)

    !oss at or +. To& arrs and %cott av$s) ?Ge$##. ress, 306 pages, cto+er 2005) A$st pr$ce E34)95,"&aFon pr$ce E23)07, used fro& E18)66) Gated 4)5 stars on "&aFon)co&) escr$+ed as a great overv$e* of a +roadspectru& of product techno#og$es) t spends enough t$&e on each to get the& *or$ng and to prov$de a fee# for the$rva#ue, custo&$Fa+$#$t., and f#eB$+$#$t.) (Ba&p#es are *e##-presented and conta$n d$scuss$on a+out ho* the. *orand are conf$gured for &u#t$p#e popu#ar app#$cat$on env$ron&ents, *here appropr$ate) t has eBce##ent +acgroundand h$stor. for each techno#og., popu#ar a#ternat$ves, and a d$scuss$on a+out *h. the author preferred th$s part$cu#artechno#og.) n other *ords, the authors prov$de &ore than >ust the +are &ent$on $nc#uded $n &an. Msurve.M +oos)There $s enough to get a## of the& *or$ng, and *or$ng together as a cohes$ve set) t does not, ho*ever, prov$de a#ot of deta$# on each one) Unfortunate#. the +oo represents a trans$t$on +et*een the (! 2)0 *or#d and the (! 3)0*or#d, and hence &an. eBa&p#es are descr$+ed $n ter&s of (! 2)0) t uses !oss 4)0)B rather than 4)2)B)

    aster$ng (nterpr$se ava!eans 3)0 +. %r$ganesh, !rose, and %$#ver&an, $#e., u#. 2006, 685 pages) A$st pr$ceE44)99, "&aFon pr$ce E29)19, Used fro& E19)55) Gated 4 stars on "&aFon)co&) p$n$ons var$ed great#., so&e

    co&&ents $nd$cat$ng that $t *as a great reference, others th$n$ng that $t *as #$tt#e &ore than the on#$ne tutor$a#) ehave a cop. fro& the a#o "#to #$+rar.) The &ost usefu# chapter $s a co&par$son of (! 2)0 and 3)0)

    !oss' " eve#oper?s Jote+oo +. Jor&an G$chard) IGe$##. ed$a, nc), une 2005, 172 pages) Gated 4 starson "&aFon) A$st pr$ce E29)99, "&aFon pr$ce E19)33, used fro& E7)90) Th$s $s perhaps the o#dest +oo that $s st$##re#evant, though $t st$## predates (! 3)0) :o*ever, $t prov$des a ver. fr$end#. and tr. th$ngs out st.#e of *r$t$ng,*h$ch *e have found usefu# *hen #earn$ng so&eth$ng ne* and co&p#eB for the f$rst t$&e)

    JBoss-Provided DocumentationThere $s +as$ca##. no docu&entat$on $n the do*n#oad $tse#f) There are four eBa&p#es, +ut these are not part$cu#ar#.re#evant to our pro>ects and usage of !oss or (! 3)0)

    nstead .ou shou#d read the !oss @ett$ng %tarted on#$ne +oo at

    http'//docs)>+oss)org/>+ossas/gett$ngstarted/v4/ht/These are focused on a tutor$a# ca##ed the ue?s !an app#$cat$on, *h$ch has account and transact$on records) Th$s$s ;u$te usefu#, as $t +u$#ds out ent$t. +eans, sess$on +eans, and &ore)

    age 3 of 35

    http://docs.jboss.org/jbossas/getting_started/v4/html/http://docs.jboss.org/jbossas/getting_started/v4/html/
  • 7/25/2019 Jboss-ejb

    4/35

    Architectural Overview of EJB 3.0(! 3)0 def$nes a conta$ner, *h$ch supports co&ponent-+ased deve#op&ent and dep#o.&ent of app#$cat$ons) Theco&ponents are ca##ed (nt$t. !eans, %ess$on !eans and essage-dr$ven !eans each to +e def$ned +e#o*) Theconta$ner a#so supp#$es a var$et. of serv$ces for *h$ch the &ost $&portant one $s a pers$stence serv$ce, ca##ed ")The re#at$onsh$p $s as sho*n +e#o*'

    (Ba&p#es of the other serv$ces $nc#ude state &anage&ent, transact$ons, c#uster$ng, &essag$ng, t$&ers, etc) ava (( 5and (! 3)0 can +e thought of as a v$rtua# &ach$ne #$e the D $tse#f, +ut *$th support for enterpr$se

    app#$cat$ons) Th$s *a. of th$n$ng a+out ava (( 5 appeared $n the anda, Gah&an, and Aane +oo and see&s ;u$teon target)

    (! 3)0 $s an outgro*th of the ear#$er (! 2)B spec$f$cat$on, *h$ch $nc#uded &ost of the a+ove, $nc#ud$ng a## of thepers$stence fac$#$t$es, ho*ever, (! 3)0 $s far eas$er to use, as $t $s +u$#t around ?s p#us ava "nnotat$ons)The (! 3)0 spec$f$cat$on *as generated fro& a+out 2003 to 2005, and has #ed to a &a>or resurgence of (( serversand too#s)

    The +$ggest changes $n the (! 3)0 spec$f$cat$on are the re&ova# of dep#o.&ent descr$ptors and the ne*spec$f$cat$on for pers$stence, ca##ed the ava ers$stence ") Th$s ne* " $s ver. heav$#. +ased on fac$#$t$es*$th$n :$+ernate, and *h$#e an "pp %erver &a. use :$+ernate underneath !oss does, the " has def$n$t$ons*$th$n pacages such as >avaB)pers$stence that prov$de a *rapper over :$+ernate and hence .ou don?t see $t d$rect#.)

    The Entity Bean(nt$t. +eans $n the (! 3)0 &ode# are def$ned +. start$ng *$th a , and then &app$ng $t to the ta+#es of there#at$ona# data+ase v$a annotat$ons) " +eco&es an ent$t. +ean s$&p#. +. hav$ng the N(nt$t. annotat$on)ther annotat$ons $nc#ude the ta+#e na&e, and the getters conta$n $nfor&at$on a+out the co#u&n na&es)

    Un#$e (! 2)1, $n *h$ch ent$t. +eans *ere a+stract, ava ers$stence ent$t. +eans are rea# o+>ects) The. don?t$&p#e&ent an. part$cu#ar (! $nterface)

    (nt$t$es are pers$sted and fetched through the (nt$t.anager *h$ch $s part of the pers$stence serv$ce, *h$ch $ss$&$#ar to the :$+ernate %ess$on o+>ect) t prov$des operat$ons such as pers$st, and createOuer.)

    The Session Bean

    " %ess$on !ean represents a d$str$+uta+#e o+>ect that carr$es out a funct$on or serv$ce, and co&es $n t*o for&s'sess$on +eans and &essage-dr$ven +eans) %ess$on +eans tend to &anage part$cu#ar $nds of act$v$t$es, such as the actof &a$ng a reservat$on *$th a trave# agent) " Trave#"gent sess$on +ean, eBa&p#e, &ust use a Cru$se, a Ca+$n, and aCusto&erPa## ent$t. +eansPto &ae a reservat$on)

    The re#evant d$st$nct$on *$th$n the scope of (! and ava ers$stence $s that an ent$t. +ean has pers$stent state, *h$#esess$on and &essage-dr$ven +eans &ode# $nteract$ons +ut do not have a pers$stent state)

    age 4 of 35

  • 7/25/2019 Jboss-ejb

    5/35

    "n eBa&p#e *ou#d +e the Trave#"gent and Geservat$onrocessor sess$on +eans, *h$ch are respons$+#e for &a$ng areservat$on, can even access the data+ase d$rect#., and perfor& reads, *r$tes, and updates to the data) !ut there?s noTrave# "gent or Geservat$onrocessor record $n the data+ase)

    " sess$on +ean has a re&ote $nterface and a #oca# $nterface) The for&er $s an $nterface annotat$on *$th NGe&ote,and the #atter $s annotated NAoca#) These $nd$cate the $&portance g$ven to ava "nnotat$ons *$th$n (! 3)0, *h$#e$t $s st$## poss$+#e to use =A dep#o.&ent descr$ptors to def$ne the sa&e th$ng)

    " t.p$ca# eBa&p#e *ou#d +e'

    @Remotepublic interface CalcuatorRemote { public int add(int x, int y); public int subtract(int x, int y);}

    $th$n the $&p#e&entat$on +e$ng (@Statelessdef$nes a state#ess sess$on +ean

    @Statelesspublic class CalculatorBean implements CalculatorRemote { public int add(int x, int y) { return x+y; } public int substract(int x, int y) { return xy; }}

    The #oca# $nterface *ou#d +e def$ned $n a s$&$#ar *a.)

    The (! conta$ner $s respons$+#e for the $nvocat$on and secur$t., and other $ssues of the env$ron&ent, so thatdeve#opers dea# on#. *$th the +eans and the +us$ness-#eve# code) e *$## see that a var$et. of add$t$ona# annotat$onscontro# these features of the conta$ner)

    The essa!e-Driven Beanust as sess$on +eans process d$rect +us$ness re;uests fro& the c#$ent, !s process $nd$rect &essages) nenterpr$se s.ste&s, &essag$ng has nu&erous uses, $nc#ud$ng s.ste& $ntegrat$on, as.nchronous process$ng, andd$str$+uted s.ste& co&&un$cat$on) f .ouIve +een *or$ng on enterpr$se deve#op&ent for so&e t$&e, .ouIrepro+a+#. fa&$#$ar *$th at #east the $dea of &essag$ng) n the &ost +as$c ter&s, &essag$ng $nvo#ves co&&un$cat$ng+et*een t*o separate processes, usua##. across d$fferent &ach$nes) ava (( &essag$ng fo##o*s th$s sa&e $deaP>uston stero$ds) ost s$gn$f$cant#., ava (( &aes &essag$ng ro+ust +. add$ng a re#$a+#e &$dd#e&an +et*een the&essage sender and rece$ver)

    Servicesn th$s sect$on, *e +r$ef#. $ntroduce so&e of the serv$ces that an (! conta$ner offers) +v$ous#., *e canIt eBp#a$nthe $&p#e&entat$on deta$#s of each serv$ce $n th$s sect$on) Je$ther $s $t necessar. to cover ever. serv$ce (! offersr$ght no*) The ta+#e +e#o* fro& the anda, Gah&an, and Aane +oo +r$ef#. #$sts the &a>or (! 3 serv$ces andeBp#a$ns *hat the. &ean to .ou fro& a pract$ca# perspect$ve)

    Service Applies To What It Means for You

    ntegrat$on %ess$on+eans and!s

    :e#ps g#ue together co&ponents, $dea##. through s$&p#e conf$gurat$on $nsteadof code) n (! 3, th$s $s done through dependenc. $n>ect$on as *e## as#ooup)

    oo#$ng %tate#esssess$on+eans,!s

    or each (! co&ponent, the (! p#atfor& creates a poo# of co&ponent$nstances that are shared +. c#$ents) "t an. po$nt $n t$&e, each poo#ed $nstance$s on#. a##o*ed to +e used +. a s$ng#e c#$ent) "s soon as an $nstance $s f$n$shedserv$c$ng a c#$ent, $t $s returned to the poo# for reuse $nstead of +e$ng fr$vo#ous#.d$scarded for the gar+age co##ector to rec#a$&)

    age 5 of 35

  • 7/25/2019 Jboss-ejb

    6/35

    Service Applies To What It Means for You

    Thread-safet. %ess$on+eans and!s

    (! &aes a## co&ponents thread-safe and h$gh#. perfor&ant $n *a.s that areco&p#ete#. $nv$s$+#e) Th$s &eans that .ou can *r$te .our server co&ponents as$f .ou *ere deve#op$ng a s$ng#e-threaded destop app#$cat$on) t doesnIt &atterho* co&p#eB the co&ponent $tse#f $s< (! *$## &ae sure $t $s thread-safe)

    %tate &anage&ent %tatefu#sess$on+eans

    The (! conta$ner &anages state transparent#. for statefu# co&ponents $nsteadof hav$ng .ou *r$te ver+ose and error-prone code for state &anage&ent) Th$s&eans that .ou can &a$nta$n state $n $nstance var$a+#es as $f .ou *eredeve#op$ng a destop app#$cat$on) (! taes care of a## the deta$#s of sess$on&a$ntenance +eh$nd the scenes)

    essag$ng !s (! 3 a##o*s .ou to *r$te &essag$ng-a*are co&ponents *$thout hav$ng to dea#*$th a #ot of the &echan$ca# deta$#s of the ava essag$ng %erv$ce % ")

    Transact$ons %ess$on+eans and!

    (! supports dec#arat$ve transact$on &anage&ent that he#ps .ou addtransact$ona# +ehav$or to co&ponents us$ng s$&p#e conf$gurat$on $nstead ofcode) n effect, .ou can des$gnate an. co&ponent &ethod to +e transact$ona#) fthe &ethod co&p#etes nor&a##., (! co&&$ts the transact$on and &aes thedata changes &ade +. the &ethod per&anent) ther*$se the transact$on $s

    ro##ed +ac)

    %ecur$t. %ess$on+eans

    (! supports $ntegrat$on *$th the ava "uthent$cat$on and "uthor$Fat$on%erv$ce ""% ", so $t $s ver. eas. to co&p#ete#. eBterna#$Fe secur$t. andsecure an app#$cat$on us$ng s$&p#e conf$gurat$on $nstead of c#utter$ng up .ourapp#$cat$on *$th secur$t. code)

    nterceptors %ess$on+eans and!s

    (! 3 $ntroduces " $n a ver. #$ght*e$ght, access$+#e &anner us$ng$nterceptors) Th$s a##o*s .ou to eas$#. separate out crosscutt$ng concerns suchas #ogg$ng, aud$t$ng, and so on $n a conf$gura+#e *a.)

    Ge&ote access a#soca##ed $str$+uted $nthe !ure +oo

    %ess$on+eans

    n (! 3, .ou can &ae co&ponents re&ote#. access$+#e *$thout *r$t$ng an.code) n add$t$on, (! 3 ena+#es c#$ent code to access re&ote co&ponents as $fthe. *ere #oca# co&ponents us$ng )

    e+ serv$ces %tate#esssess$on+eans

    (! 3 can transparent#. turn +us$ness co&ponents $nto ro+ust *e+ serv$ces*$th &$n$&a# code change)

    ers$stence (nt$t$es rov$d$ng standards-+ased, 100 percent conf$gura+#e auto&ated pers$stence asan a#ternat$ve to ver+ose and error-prone !C/%OA code $s a pr$nc$pa# goa# ofthe (! 3 p#atfor&)

    Cach$ng andperfor&ance

    (nt$t$es n add$t$on to auto&at$ng pers$stence, " transparent#. prov$des a nu&+er ofserv$ces geared to*ard data cach$ng, perfor&ance opt$&$Fat$on, and app#$cat$ontun$ng) These serv$ces are $nva#ua+#e $n support$ng &ed$u& to #arge-sca#es.ste&s)

    Deployment"n app#$cat$on cons$sts of c#ass f$#e and the$r other resources F$pped up $nto ("G f$#e, *h$ch $s a spec$a# $nd of"G f$#e) Upon open$ng the "G f$#e, the (! conta$ner scans a## c#asses for annotat$ons such as N%tate#ess to f$ndthese spec$a# c#asses Q there $s no d$rector. or &an$fest &a$ntenance that $s needed)

    Namin! in the "ontainerJa&$ng $s a## +ased on J, ho*ever, $n (!3, th$s na&$ng operat$on $s carr$ed out +. annotat$ons, rather than*r$t$ng a J &app$ng f$#e)

    age 6 of 35

  • 7/25/2019 Jboss-ejb

    7/35

    There are nu&+er of $&portant na&$ng convent$ons that are often used, such as hav$ng a## +eans have na&es thatend $n $&p#, or +ean) h$#e th$s *as &ore $&portant $n (! 2, $t $s #ess cr$t$ca# $n (! 3, +ecause there arefe*er c#asses to def$ne)

    Entity Beans Introduction

    Use a and add the N(nt$t. annotat$on)

    Then create getters and setters)

    There are rea##. no other re;u$re&ents for a s$&p#e ent$t. +ean) :o*ever, as *e *$## see $n the sect$on +e#o*, there&an. add$t$ona# annotat$ons that can +e used to spec$f. pers$stence &app$ng, such as ta+#e na&es, co#u&ns na&es,$d &anage&ent, and re#at$onsh$ps)

    ession BeansTh$s $s &ost cr$t$ca# area of understand the (! 3)0 &ode#, s$nce *e are a#read. fa&$#$ar *$th concepts of pers$stencefro& our eBper$ence *$th :$+ernate)

    Stateless Session Beans" state#ess sess$on +ean has one or &ore +us$ness $nterfaces, t.p$ca##. grouped $nto re#ated sets on each sess$on+ean) t *$## t.p$ca##. $nteract *$th the pers$stence fac$#$t$es, +ut no content can +e preserved fro& one ca## toanother) Th$s &eans that a## $nfor&at$on &ust +e prov$ded $n the argu&ents to each &ethod) "n eBa&p#e *ou#d +ea pa.&ent +ean that uses a pa.&ent ta+#e)

    The argu&ents can +e ent$t$es the&se#ves, as #ong as a## content $s ser$a#$Fa+#e) Th$s an $&prove&ent over (! 2)1*here one had to *r$te para##e# va#ue o+>ect c#asses $f one *anted to pass around the state of an ent$t. $nstead ofaccess $t through the o#d re&ote $nterface &ode#)

    " sess$on +ean can have re&ote and #oca# $nterfaces) t can a#so thro* eBcept$ons)

    Rou can p#ace the NGe&ote and NAoca# annotat$ons on the $nterface spec$f$cat$on or on the +ean $tse#f) hen used

    on +ean c#ass, the annotat$ons tae an arra. of $nterface c#asses) t $s not reco&&ended that .ou use th$s approachun#ess .ou have to, as hav$ng +us$ness $nterfaces ava$#a+#e to progra&&er?s &eans that the. enforce the contract+et*een the +ean c#ass and the ca##$ng code)

    " sess$on +ean can access the %ess$onConteBt, *h$ch a##o*s .ou to get the current user access$ng the (!, +ecause $teBtends (! conteBt, and that conta$ns the &ethod getCa##err$nc$pa# and $sCa##ernGo#e%tr$ng ro#eJa&e)

    The #$fec.c#e of a sess$on +ean $s ver. s$&p#e, as $t has on#. t*o states' oes Jot (B$st and ethod-Gead. oo#)The for&er $s a defau#t state *hen no such $nstance eB$sts) The #atter $s *hen the $nstance has +een constructed $)e),after c#ass)ne*nstance, $n>ect$on process$ng, and runn$ng the NostConstruct &ethod $f present)

    t $s rare for a state#ess sess$on +ean to change fro& ethod-Gead. to not eB$st$ng, +ut $t can $f the server dec$des toreduce &e&or.) n th$s case, the Nreestro. &ethod *$## +e ca##ed f$rst)

    State#ul Session Beans(ach statefu# sess$on +ean $s ded$cated to one c#$ent for the #$fe of the +ean $nstance) " statefu# sess$on +ean hasconversat$ona# content) t doesn?t have pers$stent content, ho*ever, &ean$ng that noth$ng reaches the data+ase +.$tse#f)

    %tatefu# sess$on +eans are often cons$dered eBtens$ons of the c#$ent) t a##o*s .ou to &ove +us$ness #og$c to theserver and therefore th$n the c#$ent)

    age 7 of 35

  • 7/25/2019 Jboss-ejb

    8/35

    " ne* $nstance of a statefu# sess$on +ean $s &ade each t$&e that $t $s #ooed up) The $nstance $s re#eased after so&econf$gura+#e t$&eout per$od)

    The #$fec.c#e of a statefu# sess$on +ean $s &ore co&p#eB, as $t can +e pass$vated +. the server to save resources)Th$s &eans that $t *$## +e ser$a#$Fed, and stored so&e*here) t can +e react$vated $f there $s ne* traff$c re;uestsfro& the or$g$na# c#$ent) The statefu# sess$on +ean goes through a## of the sa&e conta$ner-+ased &ethod process$ngsuch as NostConstruct that app#$es to state#ess sess$on +eans)

    u#t$p#e &ethods can +e decorated *$th the NGe&ove &ethod) Th$s &eans that the sess$on +ean state can +ere&oved after that &ethod co&p#etes)

    (Btended pers$stence conteBts are an advanced concept that *e don?t use as .et) The. have the +enef$t of #eav$ng a##of the o+>ects that are +e$ng used re&a$n$ng the act$ve or &anaged state)

    $th %ea&, *e don?t dea# *$th %tatefu# vs) %tate#ess sess$on +eans, +ecause a## sess$on-o+>ects are statefu#co&ponents, and .ou >ust se#ect the$r #$fet$&e) or $nstance the #$fet$&es can +e an.th$ng fro& Hevent? +as$ca##. no#ong-ter& state, hence state#ess to Hconversat$on? or Hsess$on? d$fferent var$at$ons on statefu# sess$on +eans)

    E$ample

    :ere $s an eBa&p#e of a sess$on +ean fro& the (!3 +oo'

    @Statefulpublic class !ra"el#$ent%mpl implements !ra"el#$ent { @&ersistenceContextpri"ate 'ntityana$er entityana$er;

    @'Bpri"ate &rocess&ayment*ocal process;

    pri"ate Customer customer; pri"ate Cruise cruise; pri"ate Cabin cabin;

    public "oid setCustomer(Customer cust) { entityana$ercreate(cust);

    tiscustomer - cust; }

    public "oid setCabin%d(int id) { tiscabin - entityana$erfind(Cabinclass, id); }

    public "oid setCruise%d(int id) { tiscruise - entityana$erfind(Cruiseclass, id); }

    public Reser"ation boo.&assa$e(Strin$ card, double price) tro/s %ncompleteCon"ersationalState { if (customer -- null 00 cruise -- null 00 cabin --null) { tro/ ne/ %ncompleteCon"ersationalState();

    }

    try { Reser"ation reser"ation -

    ne/ Reser"ation(customer, cruise, cabin, price, ne/ 1ate());

    entityana$erpersist(reser"ation); processbyCredit(customer, card, price);

    return reser"ation; }

    age 8 of 35

  • 7/25/2019 Jboss-ejb

    9/35

    catc ('xception ex) { tro/ ne/ 'B'xception (e); } }}

    %o&e of the $&portant aspects of th$s eBa&p#e are the $n>ect$on of the pers$stenceConteBt, and the reference to the

    other (! sess$on +ean) "#so note ho* there $s state $n the sess$on +ean, such as the Hth$s)custo&er? var$a+#e) orth$s reason, the %ess$on +ean &ust +e dec#ared N%tatefu#) " state#ess sess$on +ean has &ethods for *h$ch a## $nput$s #ocated $n the para&eters)

    The #$fec.c#e of statefu# sess$on $s on#. as #ong as the conversat$on) " conversat$on $s started *hen the statefu#sess$on +ean $s #ooed up, and co&p#eted *hen the conversat$on t$&es out)

    "odin! Session BeansA$e a## (! 3 +eans, sess$on +eans are s that fo##o* a s&a## set of ru#es) The fo##o*$ng su&&ar$Fes the ru#esthat app#. to a## t.pes of sess$on +eans'

    " sess$on +ean &ust have at #east one +us$ness $nterface)

    The sess$on +ean c#ass &ust +e concrete) Rou cannot def$ne a sess$on +ean c#ass as e$ther f$na# or a+stract

    s$nce the conta$ner needs to &an$pu#ate $t)

    Rou &ust have a no-argu&ent constructor $n the +ean c#ass) "s *e sa*, th$s $s +ecause the conta$ner

    $nvoes th$s constructor to create a +ean $nstance *hen a c#$ent $nvoes an (!) Jote that the co&p$#er$nserts a defau#t no-argu&ent constructor $f there $s no constructor $n a ava c#ass)

    " sess$on +ean c#ass can su+c#ass another sess$on +ean or an. other ) or eBa&p#e, a state#ess sess$on

    +ean na&ed Bidana$ercan eBtend another sess$on +ean &laceBidBean$n the fo##o*$ng *a.'

    @Statelesspublic Bidana$erBean extends &laceBidBean implements Bidana$er {

    }

    The +us$ness &ethods and #$fec.c#e ca##+ac &ethods &a. +e def$ned e$ther $n the +ean c#ass or $n a

    superc#ass) tIs *orth &ent$on$ng here that annotat$on $nher$tance $s supported *$th severa# #$&$tat$ons *$th(! 3 sess$on +eans) or eBa&p#e, the +ean t.pe annotat$on @Statelessor @Statefulspec$f$ed $n

    the &laceBidBeansuperc#ass *$## +e $gnored *hen .ou dep#o. the Bidana$erBean) :o*ever, an.

    annotat$ons $n the superc#asses used to def$ne #$fec.c#e ca##+ac &ethods &ore a+out that #ater $n th$ssect$on and resource $n>ect$ons *$## +e $nher$ted +. the +ean c#ass)

    !us$ness &ethod na&es &ust not start *$th Me>+)M or eBa&p#e, avo$d a &ethod na&e #$e e2bCreateor

    e2b#ddBid+ecause $t &a. $nterfere *$th (! $nfrastructure process$ng) Rou &ust def$ne a## +us$ness

    &ethods as pu+#$c, +ut not f$na# or stat$c) f .ou are eBpos$ng a &ethod $n a re&ote +us$ness $nterface of the(!, then &ae sure that the argu&ents and the return t.pe of the &ethod $&p#e&ent the2a"aioSeriali3able$nterface)

    :ere $s a su&&ar. of the ca##+ac &ethods

    Ca##+ac "nnotat$on T.pe of (! T.p$ca##. Used or)))

    2a"axannotation&ostConstruct

    %tate#ess,statefu#,!

    Th$s annotated &ethod $s $nvoed after a +ean $nstance $screated and dependenc. $n>ect$on $s co&p#ete) @enera##. th$sca##+ac $s used to $n$t$a#$Fe resources for eBa&p#e, open$ngdata+ase connect$ons)

    2a"axannotation&re1estroy

    %tate#ess,statefu#,!

    Th$s annotated &ethod $s $nvoed pr$or to a +ean $nstance +e$ngdestro.ed) @enera##. th$s ca##+ac $s used to c#ean up resourcesfor eBa&p#e, c#os$ng data+ase connect$ons)

    age 9 of 35

  • 7/25/2019 Jboss-ejb

    10/35

    Ca##+ac "nnotat$on T.pe of (! T.p$ca##. Used or)))

    2a"axe2b&re&assi"ate %tatefu# Th$s annotated &ethod $s $nvoed pr$or to a +ean $nstance +e$ngpass$vated) @enera##. th$s ca##+ac $s used to c#ean upresources, such as data+ase connect$ons, TC/ socets, or an.resources that cannot +e ser$a#$Fed dur$ng pass$vat$on)

    2a"axe2b&ost#cti"ate %tatefu# Th$s annotated &ethod $s $nvoed after a +ean $nstance $sact$vated) @enera##. th$s ca##+ac $s used to restore resources,such as data+ase connect$ons that .ou c#eaned up $n the &re

    &assi"ate&ethod)

    These are the on#. fe* statefu# +eanQspec$f$c features that *e needed to ta# a+outS !efore conc#ud$ng th$s sect$onon statefu# +eans, *eI## +r$ef#. su&&ar$Fe the d$fferences +et*een statefu# and state#ess sess$on +eans as a hand.reference'

    Features Stateless Stateful

    Conversat$ona# state Jo Res

    oo#$ng Res Jo

    erfor&ance pro+#e&s Un#$e#. oss$+#e

    A$fec.c#e events &ostConstruct,&re1estroy

    &ostConstruct, &re1estroy,

    &re&assi"ate, &ost#cti"ate

    T$&er Res Jo

    SessionSyncroni3ationfor

    transact$ons

    Jo Res

    e+ serv$ces Res Jo

    (Btended &ersistenceContext Jo Res

    !ersistence usin" the Entity #ana"er

    %ntroductionTh$s $s #arge#. ne* &ater$a# $n 3)0, and +eg$ns *$th the $dea that ent$t$es are , and the ent$t. &anager $s a:$+ernate-#$e %ess$on, ca##ed the ers$stenceConteBt) n fact, as &ent$oned a+ove, !oss (! 3)0 $s +u$#t on top of:$+ernate 3)0 G so#ut$on)

    or deve#opers *$th eBper$ence $n :$+ernate, th$s $s ver. s$&$#ar and fa&$#$ar) n genera#, a## of the :$+ernate na&esand convent$ons have +een changed to *rapper na&es and funct$ons) or $nstance, &ost of the f$#e ca##edpers$stence)B $s e;u$va#ent to the h$+ernate)cfg)B f$#e) :o*ever, there are d$fferent fac$#$t$es for spec$f.$ngthe c#ass f$#es to rev$e* for &app$ng $nfor&at$on)

    The $nterface to the (nt$t.anager $s'

    pac.a$e 2a"axpersistence;

    public interface 'ntityana$er { public "oid persist(4b2ect entity); public 5!6 ! find(Class 5!6 entityClass, 4b2ect primary7ey); public 5!6 ! $etReference(Class 5!6 entityClass, 4b2ect primary7ey); public 5!6 ! mer$e(! entity);

    age 10 of 35

  • 7/25/2019 Jboss-ejb

    11/35

    public "oid remo"e(4b2ect entity); public "oid loc.(4b2ect entity, *oc.ode!ype loc.ode);

    public "oid refres(4b2ect entity); public boolean contains(4b2ect entity); public "oid clear( );

    public "oid 2oin!ransaction( ); public "oid flus( ); public 8lusode!ype $et8lusode( ); public "oid set8lusode(8lusode!ype type);

    public 9uery create9uery(Strin$ :ueryStrin$); public 9uery createamed9uery(Strin$ name); public 9uery createati"e9uery(Strin$ s:lStrin$); public 9uery createati"e9uery(Strin$ s:lStrin$, Strin$ resultSetappin$); public 9uery createati"e9uery(Strin$ s:lStrin$, Class resultClass);

    public 4b2ect $et1ele$ate( );

    public "oid close( ); public boolean is4pen( );

    }

    The &ost co&&on#. used &ethods are pers$st, f$nd, &erge, re&ove, #oc, and createOuer.nd$na&e .ou se#ected for .our datasource)

    2arfile and class

    The c#ass e#e&ent spec$f$es a fu##. ;ua#$f$ed c#assna&e that .ou *$## +e#ong to the pers$stence un$t) The >ar-f$#e e#e&ent spec$f$es another >ar .ou *ant auto&at$ca##. scanned for N(nt$t. c#asses) hen us$ng >ar-f$#e,.ou &ust spec$f. a path re#at$ve to the >ar f$#e the pers$stence)B f$#e $s $n) !. defau#t a#so, the >ar thepers$stence)B f$#e $s p#aced $n $s scanned for N(nt$t. c#asses as *e##) :ence .ou don?toften have tospec$f. the >ar-f$#eV or c#assV e#e&ents)

    properties

    The propert$es e#e&ent $s used to spec$f. vendor spec$f$c propert$es) Th$s $s *here .ou *$## def$ne .our!oss and :$+ernate spec$f$c conf$gurat$ons) Rou &a. need to prov$de $nfor&at$on to :$+ernate so that $tno*s the data+ase vendor d$a#ect .%OA, rac#e, etc))), cach$ng, as *e## as other sett$ngs)

    Simple 'perations Supported (y the Entity ana!er

    The ent$t.anager can perfor& a ;uer. for one or &ore o+>ects) "n eBa&p#e of the s$&p#est ;uer. $s'

    Sales4rder so - entityana$erfind(Sales4rderclass, =);

    *h$ch *ou#d f$nd a %a#esrder *$th pr$&ar. e. 1 the assu&pt$on here $s that the c#ass has a s$ng#e-part pr$&ar.e.) The ent$t.anager &aps data+ase ta+#es to o+>ects) f there $s no o+>ect, then the f$nd &ethod return nu##)

    There $s a s$&$#ar &ethod, ca##ed getGeference, *h$ch *$## return an eBcept$on of the record $s not found)

    n (! 3, there $s a#so a ;uer. #anguage, *h$ch $s +ased on :$+ernate?s :OA) Th$s $s covered $n &ore deta$# +e#o*)The ;uer. #anguage saves .ou fro& hav$ng to *r$te f$nder &ethods for each of the d$fferent t.pes of ;uer$es, as done$n (! 3)

    To *r$te out a changed o+>ect, use the pers$st+>ect o+>ect &ethod)

    h$#e &ost of our app#$cat$ons use pers$stence sess$on that are &a$nta$ned as #ong as *e need the& +ecause the.are part of %ea& conversat$ons, the (! #$terature goes through a +$t of deta$# to d$scuss the $dea of o+>ects that arepart of pers$stence sess$ons $n *h$ch changes to the& are not actua##. *r$tten out unt$# the end of the sess$on, or the$dea of detached o+>ects) The #atter are ones that *ere found as part of a pers$stence sess$on, +ut are st$## used afterthe sess$on $s c#osed) %uch o+>ects can +e ser$a#$Fed out to c#$ent, then +rought +ac and &erged $n) The C"(n#$ne app#$cat$on *ored th$s *a.)

    "nother $&portant note $s that one can force changes to +e s.nchron$Fed *$th the data+ase +. ca##$ng f#ush) !.defau#t f#ush$ng auto&at$ca##. happens +efore a corre#ated ;uer. $s eBecuted $neff$c$ent $&p#e&entat$ons &a. f#ush+efore any;uer. $s eBecuted and at transact$on co&&$t t$&e) Rou can contro# th$s f#ush +ehav$or *$th the#ushodeT.pe va#ue, *h$ch $s set on the ent$t.anager) %ee onson-:aefe# pages 81-82)

    $na##., $t $s poss$+#e to get a reference to the under#.$ng pers$stence prov$der +. ca##$ngent$t.anager)gete#egate) n !oss, th$s *$## +e :$+erate)

    !e#o*, *e *$## d$scuss &ore advanced aspects of ent$t$es, and use of transact$ons)

    #a$$in" !ersistent O%&ectsapp$ng $n th$s case refers to def$n$ng the &app$ng +et*een the o+>ects and the data+ase) Th$s uses the (! 3)0vers$on of the :$+ernate &app$ng fac$#$t$es, ca##ed ava ers$stence) t $s heav$#. +ased on ava "nnotat$ons)

    age 12 of 35

  • 7/25/2019 Jboss-ejb

    13/35

    The &ost $&portant annotat$ons are NTa+#e, NCo#u&n, Nan.Tone, No$nCo#u&n, etc)

    %$nce the ava ers$stence us$ng :$+ernate +oo fro& ann$ng covers the sa&e concepts $n a #$tt#e &ore deta$#,see our docu&ent Jotes on :$+ernate for &ore $nfor&at$on) The !ure and onson-:aefe# (! 3)0 +oo taesa#&ost 3 chapters to cover th$s &ater$a#)

    Basic AnnotationsThe annotat$ons covered are N(nt$t., NTa+#e, NCo#u&n, Nd, and &ore)

    Th$s chapter $n the !ure and onson-:aefe# +oo a#so covers the NTa+#e@enerator annotat$on and theN%e;uence@enerator)

    ropert. &app$ngs $nc#ude NTrans$ent, N!as$c, and NetchT.pe) There are a#so N(&+edded o+>ects, andNTe&pora#) The #atter $s $&portant to $nd$cate that the &app$ng of a propert. *hose t.pe $s >ava)ut$#)ate or>ava)ut$#)Ca#endar $s to +e a ate, a T$&e, or a T$&esta&p) !. defau#t, t$&esta&p $s assu&ed)

    Entity )elationships$th$n ava ers$stence, .ou def$ne re#at$onsh$ps +et*een o+>ects us$ng the Nan.Tone annotat$on, and otherannotat$ons) ost of the (! #$terate goes through th$s $n great deta$#, +ecause the co&&un$t. of (! 2)0deve#opers has not seen :$+ernate +efore)

    :ence, the &ater$a# $s ver. s$&$#ar to the chapter $n the :$+ernate +oo on advanced &app$ngs and re#at$onsh$ps)

    t a#so def$nes +ehav$or such as Cascade, ers$st, and erge) "ga$n, these para##e# the :$+ernate &ater$a#)

    Entity %nheritanceTh$s &ater$a# $s a#so para##e# to the :$+ernate +oo, cover$ng approaches to def$n$ng $nher$tance re#at$onsh$ps $n the-G &app$ng) %$nce the :$+ernate +oo covers $t >ust as *e##, *e have not pa$d a great dea# of attent$on to th$schapter $tse#f)

    'ueries and EJB '(Ouer.$ng $s a funda&enta# feature of a re#at$ona# data+ase) " ;uer. a##o*s .ou to fetch fro& pers$stence storage)The +as$c ;uer. fac$#$t. $s the %tructured Ouer. Aanguage %OA, ho*ever, too#s #$e :$+ernate and ava ers$stenceprov$de a h$gher #eve# fac$#$t. as *e##)

    The h$gh #eve# fac$#$t. $s ca##ed (! OA and $s a dec#arat$ve ;uer. #anguage ta$#ored to *or *$th ava o+>ectsrather than a re#at$ona# sche&a) To eBecute ;uer$es, .ou reference the propert$es and re#at$onsh$ps of .our ent$t.+eans rather than the under#.$ng ta+#es and co#u&ns these o+>ects are &apped to) hen the ;uer. $s eBecuted, theent$t. &anager uses the $nfor&at$on .ou prov$ded through the &app$ng &etadata, and auto&at$ca##. trans#ates $t toone or severa# nat$ve %OA ;uer.) The resu#ts are then converted +ac $nto o+>ects)

    %$nce (! OA $s a ;uer. #anguage that represents ava o+>ects, $t $s porta+#e across d$fferent data+ase$&p#e&entat$ons +ecause the ent$t. &anager hand#es the convers$on to ra* %OA for .ou) Th$s appears to $nc#udestandard$F$ng a nu&+er of d$a#ect-spec$f$c data+ase funct$ons and ;uer. s.ntaB ru#es)

    The ;uer. fac$#$t$es $nc#ude an " for +u$#d$ng cond$t$ons, support for pag$ng, and support for order$ng, etc)

    %ee the d$scuss$on $n Chapter 9 of the !ure and onson-:aefe# (! 3)0 +oo)

    *uery Synta$The +as$c s.ntaB $s se#ect $dent$f$erV fro& ta+#eJa&eV $dent$f$erV)

    Rou can a#so have se#ect $dent$f$erV fro& ta+#eJa&eV $dent$f$erV *here cond$t$onV

    age 13 of 35

  • 7/25/2019 Jboss-ejb

    14/35

    The cond$t$on can have para&eters such as se#ect c fro& Custo&er c *here c)f$rstJa&eW:firstandc)#astJa&eW:last) n th$s case .ou *ou#d spec$f. the para&eters va#ues on the ;uer. o+>ect)

    T*o s.ntaBes are prov$ded' na&ed para&eters and pos$t$ona# para&eters) Ja&ed para&eters are reco&&ended)

    T.p$ca##., *e are fetch$ng a set of o+>ects, such as Custo&er $n the a+ove o+>ect) n th$s case the resu#t *$## +e aA$stCusto&erV o+>ect) The ava c#ass for Custo&er $s found +. search$ng fro& the pacage conta$n$ng the ;uer.ca##, then search$ng up*ards) T.p$ca##., the c#ass na&es are e$ther the na&e of the c#ass that def$nes the ent$t. or arepacage-a+so#ute)

    These eBa&p#es return o+>ects) :o*ever, *e can return $nfor&at$on *h$ch $s &ore +as$c, as $n the eBa&p#e'

    select cfirstame, clastame from Customer as c

    n th$s case, the resu#t *$## +e a #$st of the +>ect arra.s, and the va#ues *$## +e stored $nto each +>ect arra.pos$t$ona##. $)e), f$rstJa&e $s at X0Y, and #astJa&e $s at X1Y) Th$s a#so avo$ds the co#u&n nu&+ers $n !C +e$ngnu&+ered fro& 1 $ssue)

    The constructor to +e used for the o+>ect can +e spec$f$ed) Aet?s sa. that *e *ant to return a f$rstJa&e/#astJa&e

    pa$r as an $nstance of the Ja&e c#ass, *h$ch has f$e#ds ca##ed f$rst and #ast, and an appropr$ate constructor) Roucou#d then *r$te'

    select ne/ comincraame(cfirstame, clastame) from Customer c

    Rou can a#so ;uer. for spec$f$c f$e#ds on the record, and for re#ated records) or eBa&p#e'se#ect so)cred$tCard)cred$tCardCo&pan.)address)state fro& %a#esrder so)

    Joins and other *uery +acilitiesan. re#at$onsh$ps +et*een ent$t. +eans are co##ect$on-+ased, and +e$ng a+#e to access and se#ect +ens fro& thesere#ea$tons$ps $s $&portant)

    The fo##o*$ng ;uer. uses the J operator to se#ect e#e&ents fro& a co##ect$on) t returns the reservat$ons of a## the

    custo&ers'

    select r from Customer c, in( creser"ations ) r

    Th$s ;uer. can a#so +e eBpressed as an $nner >o$n *h$ch $s &ore fa&$#$ar to %OA progra&&ers'

    select r from Customer c inner 2oin creser"ations r

    The #eft >o$n s.ntaB ena+#es retr$eva# of a set of ent$t$es *here &atch$ng va#ues $n the >o$n state&ent &a. not eB$st)or such va#ues, a nu## va#ue $s p#aced $n the resu#t) :ere $s an eBa&p#e'

    select cfirstame, clastame, pnumber from Customer c left cponenumbers p

    Th$s can a#so +e eBpressed as #eft outer >o$n s.ntact$c sugar to para##e# %OA 92)

    The $&portant po$nt here $s that the >o$ns are eBpressed $n ter&s of the f$e#ds $n the o+>ect such as c)phonenu&+ersrather than *r$t$ng out the $d &atches that are t.p$ca# of >o$ns) urther, the &etadata $n the c#ass o+>ect $nd$cates*hat c#ass the >o$ned ta+#es are to +e found through not$ce that the c#ass of p $s never spec$f$ed here Q on#. theor$g$n c#ass of Custo&er &ust +e spec$f$ed)

    The Hfetch >o$n? s.ntaB a##o*s .ou to pre#oad a returned ent$t.?s re#at$onsh$ps even $f the re#at$onsh$p propert. has aetchT.pe of AaF.) or eBa&p#e, #et?s sa. *e have a def$ned our custo&er?s one-to-&an. re#at$onsh$p to hone asfo##o*s'

    age 14 of 35

  • 7/25/2019 Jboss-ejb

    15/35

    @4ne!oany(fetc-8etc!ype*#) public Collection5&one6 $et&ones() { return pones; }

    The defau#t for fetc!ype$s '#D'R, eBcept for a any!oanyand a 4ne!oanyre#at$onsh$p, for *h$ch the

    defau#t $s *#) "fetc!ypeof '#D'R&eans that a pers$stence prov$der *$## #oad the attr$+ute of an ent$t.

    a#ong *$th the ent$t., *hereas a fetc!ypeof *#$s a h$nt to the prov$der that the attr$+ute need not +e fetched

    a#ong *$th the ent$t.)

    f *e *ere to #oop over a #$st of custo&ers, and ca## gethones on each one, there *ou#d +e an add$t$ona# ;uer.created for each one) Th$s $s ca##ed an JZ1 ;uer$es pro+#e&) nstead, use

    select c from Customer c *'8! 4% 8'!C cpones

    The e. $n th$s eBa&p#e $s the *ord J, *h$ch causes a >o$n to +e &ade $n the resu#t$ng ;uer.) Jo* the phones*$## +e #oaded $n >o$n *hen the custo&ers are #oaded)

    Jote that $n :$+ernate, .ou can spec$f. a runt$&e ho* the fetches are to +e carr$ed out +. ca##$ngsetetchodeattr$+uteJa&e, fetchode) :o*ever, there $s no s$&$#ar funct$on $n the regu#ar (!3 pers$stence&anager, so .ou &ust dea# *$th th$s $n the structure of the ;uer. str$ngs)

    " ;uer. can have a H*here? c#ause *h$ch $s +as$ca##. the sa&e as $n regu#ar %OA *e are s$pp$ng over thed$scuss$on a+out ar$th&et$c operat$ons $n *here c#auses that appears on pages 185 to 187 of the !ure and onson-:aefe# +oo)

    There are a#so fac$#$t$es for co&par$ng *$th JUAA and JT JUAA)

    The d$scuss$on regard$ng Horder +.? and Hgroup +.? $s +as$ca##. the sa&e as $n %OA and :$+ernate) Rou &ust +ecarefu# *h$ch +as$c f$e#ds .ou use $n the G(G !R c#ause) f the ;uer. se#ects a co##ect$on of ent$t$es, then theG(G !R c#ause can +e used *$th on#. +as$c f$e#ds of the ent$t. t.pe that $s se#ected)

    $na##., the (! 3 ;uer. #anguage supports the operators such as &aB/&$n/su&/avg on the spec$f$c fetched +as$cf$e#ds of the ent$t$es)

    "#so f$na##., the (! ;uer. #anguage supports su+;uer$es or su+se#ects) don?t no* *hat happens $f theunder#.$ng data+ase doesn?t support such, perhaps $t +reas the ;uer$es up $nto d$st$nct ones)

    *uery AP%pac.a$e 2a"axpersistence;

    public interface 9uery { public *ist $etResult*ist( ); public 4b2ect $etSin$leResult( ); public int executeEpdate( ); public 9uery setaxResults(int maxResult); public 9uery set8irstResult(int start&osition); public 9uery setint(Strin$ intame, 4b2ect "alue);

    public 9uery set&arameter(Strin$ name, 4b2ect "alue); public 9uery set&arameter(Strin$ name, 1ate "alue, !emporal!ype temporal!ype); public 9uery set&arameter(Strin$ name, Calendar "alue, !emporal!ype temporal!ype); public 9uery set&arameter(int position, 4b2ect "alue); public 9uery set&arameter(int position, 1ate "alue, !emporal!ype temporal!ype); public 9uery set&arameter(int position, Calendar "alue, !emporal!ype temporal!ype); public 9uery set8lusode(8lusode!ype flusode);}

    age 15 of 35

  • 7/25/2019 Jboss-ejb

    16/35

    The three &ost co&&on ca##s are' getGesu#tA$st, get%$ng#eGesu#t, and eBecuteUpdate) The get%$ng#eGesu#t*$## thro* a JonUn$;ueGesu#t(Bcept$on $f &ore than one record &eets the ;uer. cond$t$on)

    Ouer$es are &ade us$ng the fo##o*$ng " on the (nt$t.anager'

    pac.a$e 2a"axpersistence;

    public interface 'ntityana$er { public 9uery create9uery(Strin$ e2b:lStrin$); public 9uery createamed9uery(Strin$ name); public 9uery createati"e9uery(Strin$ s:lStrin$); public 9uery createati"e9uery(Strin$ s:lStrin$, Class resultClass); public 9uery createati"e9uery(Strin$ s:lStrin$, Strin$ resultSetappin$);}

    *uery E$amplespublic *ist findByame(Strin$ first, Strin$ last) { 9uery :uery - entityana$ercreate9uery(

  • 7/25/2019 Jboss-ejb

    17/35

    first - first + results$etSi3e( );} /ile (resultssi3e( ) 6 F);

    n th$s eBa&p#e, *e #oop through a## custo&ers $n the data+ase and output the$r f$rst and #ast na&es to the s.ste&output strea&) f *e had thousands or even &$##$ons of custo&ers $n the data+ase, *e cou#d ;u$c#. run out of&e&or., as each eBecut$on of the $etCustomers( )&ethod *ou#d return custo&ers that *ere st$## &anaged +.

    the ent$t. &anager) %o, after *e are f$n$shed outputt$ng a +#oc of custo&ers, *e ca##

    'ntityana$erclear()to detach these custo&ers and #et the& +e gar+age-co##ected +. the ava D) Useth$s pattern *hen .ou need to dea# *$th a #ot of ent$t. o+>ects *$th$n the sa&e transact$on)

    &sin! Native *ueriesf .our ;uer. does not &atch the standard pattern of return an o+>ect or a #$st of o+>ects th$s $s ;u$te co&&on for;uer$es $nc#ud$ng @GU !R c#auses or co&p#eB >o$ns, .ou can use the createNativeQuery &ethod of the

    EntityManager $nterface to create a d.na&$c ;uer. us$ng %OA as fo##o*s'

    Query q = em.createNativeQuery("SELECT user_id, first_name, last_name " +

    " FROM users WHERE user_id IN (SELECT seller_id FROM " +

    "items GROUP BY seller_id HAVING COUNT(*) > )",

    actionbazaar.persistence.User.class);

    return q.getResultList();

    n th$s state&ent, the createNativeQuery &ethod taes t*o para&eters' the %OA ;uer. and the ent$t. c#ass

    +e$ng returned) Th$s *$## +eco&e an $ssue $f the ;uer. returns &ore than one ent$t. c#assP*h$ch $s *h. " a##o*sa @SqlResultSetMapping to +e used *$th the createNativeQuery &ethod $nstead of pass$ng an ent$t.

    c#ass) " @SqlResultSetMapping &a. +e &apped to one or &ore ent$t$es)

    :ere $s an other eBa&p#e' Ouer$es &a. return &u#t$p#e o+>ects and/or propert$es as an arra. of t.pe4b2ectGH,

    select moter, offspr, matenamefrom 1omesticCat as moter inner 2oin motermate as mate left outer 2oin moter.ittens as offspr

    or as a *ist:OA spec$f$c feature

    select ne/ list(moter, offspr, matename)from 1omesticCat as moter inner 2oin motermate as mate left outer 2oin moter.ittens as offspr

    or as an actua# t.pesafe ava o+>ect,

    select ne/ 8amily(moter, mate, offspr)from 1omesticCat as moter 2oin motermate as mate left 2oin moter.ittens as offspr

    assu&$ng that the c#ass 8amilyhas an appropr$ate constructor)

    e haven?t +een a+#e to f$nd a correspond$ng set of eBa&p#es $n the (! 3)0 +oo

    The selectc#ause p$cs *h$ch o+>ects and propert$es to return $n the ;uer. resu#t set) f .ou #$st a set of co#u&ns,

    the. *$## +e returned as an +>ect arra. for each ro*)

    age 17 of 35

  • 7/25/2019 Jboss-ejb

    18/35

    Rou &a. a#so eBpress ;uer$es $n the nat$ve %OA d$a#ect of .our data+ase) Th$s $s usefu# $f .ou *ant to ut$#$Fedata+ase spec$f$c features such as ;uer. h$nts or the CJJ(CT !R opt$on $n rac#e) t a#so prov$des a c#ean&$grat$on path fro& a d$rect %OA/!C +ased app#$cat$on to :$+ernate) Jote that :$+ernate3 a##o*s .ou to spec$f.hand*r$tten %OA $nc#ud$ng stored procedures for a## create, update, de#ete, and #oad operat$ons p#ease refer to thereference gu$de for &ore $nfor&at$on)

    ,imitations o# the *uery +acility compared to i(ernateThere $s no ;uer. construct$on ", #$e :$+ernate?s Cr$ter$a o+>ects)

    e need to add &ore d$scuss$on of #aF. vs) eager #oad$ng here) ne reason *h. $t doesn?t get &ent$oned as &uch $nthe !oss &ater$a# $s that the sess$on $s t.p$ca##. a#*a.s open, and so $t $s eas. to fetch *hen needed) There $s &ored$scuss$on a+out th$s $n the %ea& +oo)

    (ifecycle of Beans!eans, part$cu#ar#. sess$on +eans, have a #$fec.c#e of +e$ng created, used destro.ed, poo#ed, arch$ved, recovered, etc)" ser$es of ca##+acs #et .ou no* *hat $s happen$ng)

    "s descr$+ed a+ove, the #$fec.c#e of a sess$on +ean $s ver. s$&p#e, as $t has on#. t*o states' oes Jot (B$st andethod-Gead. oo#) The for&er $s a defau#t state *hen no such $nstance eB$sts) The #atter $s *hen the $nstance has+een constructed $)e), after c#ass)ne*nstance, $n>ect$on process$ng, and runn$ng the NostConstruct &ethod $fpresent)

    Bean ,i#ecycle "all(acs" sess$on +ean has a #$fec.c#e) Th$s &eans that +eans go through a predef$ned set of state trans$t$ons) f .ouIve used%pr$ng or (! 2, th$s shou#d co&e as no surpr$se) f .ou havenIt, the concept can +e a #$tt#e tr$c. to grasp)

    To understand the +ean #$fec.c#e, $t $s $&portant to rev$s$t the concept of &anaged resources) Geca## that theconta$ner &anages a#&ost ever. aspect of sess$on +eans) Th$s &eans that ne$ther the c#$ent nor the +ean $srespons$+#e for deter&$n$ng *hen +ean $nstances are created, *hen dependenc$es are $n>ected, *hen +ean $nstancesare destro.ed, or *hen to tae opt$&$Fat$on &easures) anag$ng these act$ons ena+#es the conta$ner to prov$de thea+stract$ons that const$tute so&e of the rea# va#ue of us$ng (!s, $nc#ud$ng , auto&ated transact$on &anage&ent,

    ", transparent secur$t. &anage&ent, and so on)

    The ,i#ecycle EventsThe #$fec.c#e of a sess$on +ean &a. +e categor$Fed $nto severa# phases or events) The &ost o+v$ous t*o events of a+ean #$fec.c#e are creat$on and destruct$on) "## (!s go through these t*o phases) n add$t$on, statefu# sess$on+eans go through the pass$vat$on/act$vat$on c.c#e, *h$ch *e d$scuss $n depth +e#o*) :ere, *e tae a c#ose #oo atthe phases shared +. a## sess$on +eans' creat$on and destruct$on)

    The #$fec.c#e for a sess$on +ean starts *hen a +ean $nstance $s created) Th$s t.p$ca##. happens *hen a c#$ent rece$vesa reference to the +ean e$ther +. do$ng a J #ooup or +. us$ng dependenc. $n>ect$on) The fo##o*$ng steps occur*hen a +ean $s $n$t$a#$Fed'

    The conta$ner $nvoes the ne/%nstance&ethod on the +ean o+>ect) Th$s essent$a##. trans#ates to a

    constructor $nvocat$on on the +ean $&p#e&entat$on c#ass)

    f the +ean uses , a## dependenc$es on resources, other +eans, and env$ron&ent co&ponents are $n>ected

    $nto the ne*#. created +ean $nstance)

    #essa"e)Driven Beansessage-dr$ven +eans are +u$#t on %, *h$ch $s s$&$#ar to T!/GD) ost of the docu&entat$on prov$des a good +$tof +acground $nfor&at$on on % and &essages, $nc#ud$ng suggest$ons on *hen to use po$nt-to-po$nt and *hen touse pu+#$sh-and-su+scr$+e)

    age 18 of 35

  • 7/25/2019 Jboss-ejb

    19/35

    %o&e of the character$st$cs of % re#at$ve to T!/GD are as fo##o*s'

    % appears +u$#t on dep#o.&ent-t$&e def$ned su+>ects or ;ueues *h$ch have na&es) The $dea of us$ng

    na&es $s s$&$#ar to GD, though the na&es don?t see& to have a dotted se;uence of su+>ect toens for use*$th *$#dcards, and the $dea of def$n$ng a## of the su+>ects at dep#o.&ent t$&e $s ;u$te d$fferent fro& thed.na&$c +ehav$or of T!/GD)

    The &essages are hash&aps that have set of str$ng, va#ue pa$rs) Th$s $s s$&$#ar to T!/GD &essages)

    There are a var$et. of de#$ver. protoco#s ava$#a+#e, $nc#ud$ng po$nt to po$nt and ;ueue, *here the #atter $spu+#$sh and su+scr$+e)

    essage-r$ven !eans are $dent$f$ed us$ng the Nessager$ven annotat$on, and can rece$ve and send &essages)The $nfor&at$on $n the annotat$on spec$f$es the su+>ect and the protoco#) Th$s $s an $&prove&ent over ear#$ervers$ons of the concept *h$ch puFF#ed &e, +ecause the. su+>ect na&e *as $n the =A conf$gurat$on f$#es, and thenot the ava code)

    Interce$torsnterceptors are o+>ects that $nterpose the&se#ves on &ethod ca##s or the #$fec.c#e events of sess$on and &essage-dr$ven +eans) The. a##o* .ou to encapsu#ate the co&&on +ehav$or that cuts across #arge parts of .our app#$cat$on)"n eBa&p#e *ou#d +e to add #ogg$ng ca##s to each of .our (! c#asses)

    (! 3 $nterceptors $&p#e&ent H"spect r$ented rogra&&$ng? and so#ve th$s pro+#e&) n our eBa&p#e .ou s$&p#.create a #ogg$ng $nterceptor that does the #ogg$ng, and .ou can &ae $t the defau#t $nterceptor for .our app#$cat$on)The #ogg$ng $nterceptor *$## +e eBecuted *hen an. +ean &ethod $s eBecuted) f the re;u$re&ent for #ogg$ng orprof$#$ng changes, then .ou have to change on#. one c#ass)

    Speci#ication and E$ampleThe @%nterceptorsannotat$on a##o*s .ou to spec$f. one or &ore $nterceptor c#asses for a &ethod or c#ass) or

    eBa&p#e, +e#o* *e attach a s$ng#e $nterceptor to the add*ine%tem &ethod'

    @%nterceptors(&rofilin$%nterceptorclass)public "oid add*ine%tem (

    The rof$#$ngnterceptor c#ass *ou#d #oo #$e'

    public class &rofilin$%nterceptor {

    @#round%n"o.e public 4b2ect profile(%n"ocationContext ctx) tro/s 'xception { Systemoutprintln(

  • 7/25/2019 Jboss-ejb

    20/35

    @%nterceptors(&rofilin$%nterceptorclass)@Statelesspublic class Sales4rderana$er implements 4rderana$er { public "oid add*ine%tem ( public "oid addSipment#ddress (}

    The attach&ent can a#so +e done through a dep#o.&ent descr$ptor f$#e $n =A

    5assemblydescriptor6 5interceptorbindin$6 5e2bname6Sales4rderana$er5?e2bname6 5interceptorclass6 incrautil&rofilin$%nterceptor 5?interceptorclass6 5?interceptorbindin$65?assemblydescriptor6

    Th$s *ou#d app#. to the %a#esrderanager, +ut .ou cou#d a#so $nd$cate a H? for a## c#asses)

    'ther &ses

    Jot on#. can .ou $nterface &ethod $nvocat$ons, +ut .ou can a#so $ntercept (! #$fec.c#e events)

    *ransactions$rst *e def$ne the features of "C transact$ons)

    JeBt, *e focus on dec#arat$ve transact$on &anage&ent) Us$ng the Transact$on"ttr$+ute t.pe attr$+ute, *e candec#are a &ethod?s transact$on ru#e as one of the fo##o*$ng'

    "J"TGR

    G(OUG(

    G(OUG(J(

    %UGT%

    JT%UGT(

    J(D(G

    Th$s $nfor&at$on can +e g$ven $n the annotat$on or $n the =A f$#e, and the annotat$on approach $s suggested)

    ages 366 through 369 of the !ure and onson-:aefe# (! 3)0 +oo def$nes each of the t.pes #$sted a+ove)

    The propagat$on of transact$ons $s a cr$t$ca# concept) The conta$ner $&p#e&ents cha$ns of transact$ons)

    %solation and Data(ase ,ocin!ov$ng on to a d$scuss$on of so#at$on and ata+ase Aoc$ng, *e have the def$n$t$ons of d$rt., repeata+#e, andphanto& reads) Then a d$scuss$on of #ocs)

    pt$&$st$c #oc$ng $s neBt d$scussed) Th$s $s contro##ed through the NDers$on annotat$on on a f$e#d that $s a ava#ong, t.p$ca##. ca##ed vers$on)

    !eans outs$de of the transact$on?s scope prov$de a state#ess serv$ce

    Transaction ana!ement Approaches(Bp#$c$t transact$on &anage&ent $s ava$#a+#e, +ut $t $s suggested that .ou not tr. to &anage the transact$ons .ourse#f)The " used $s ca##ed the ava Transact$on " T", *h$ch $s ver. s$&$#ar to the :$+ernate sess$on?s transact$on

    age 20 of 35

  • 7/25/2019 Jboss-ejb

    21/35

    &anage&ent fac$#$t$es) t $s $nterest$ng that the :$+ernate +oo has .ou perfor&$ng transact$on &anage&ent, +ut th$s$nd$cates that the conta$ner $s a stronger too# than the Gapper)

    $na##., the +oo ta#s a+out (Bcept$ons and Transact$ons, and then conversat$ona# pers$stence conteBts) These see$e good $deas to tr. out as *e##)

    *he J+DI E+C and In&ection(ver. (! conta$ner that $s dep#o.ed $n an app#$cat$on server has $t o*n persona# reg$str. ca##ed the (nterpr$seJa&$ng ConteBt (JC)

    hat can +e reg$stered $n the (JC[

    references to $nterfaces

    % ;ueues or top$cs

    % connect$on factor$es

    ata sources

    C" resources

    (tc)

    The chapter has great#. eBpanded coverage of the J, and co&+$ned *$th the fac$#$t$es of ependenc. n>ect$onfro& %pr$ng)

    The (JC can +e popu#ated fro& =A or fro& annotat$ons)

    There $s a fac$#$t. $n (! to $n>ect references fro& the (JC) Th$s a##o*s .ou to p#ace annotat$ons on .our c#ass&e&+ers, &ared *$th the annotat$on (!, and the conta$ner *$## #oo up the na&e and $n>ect the reference)

    ecurity in EJB 3.0Th$s d$scuss$on $s ent$re#. ne* co&pared to the (! 2 +ased sources that *e used to use) The reason for the ne*&ater$a# $s that no* secur$t. can +e descr$+ed through dec#arat$ve annotat$ons on the var$ous &ethods of the sess$on+eans)

    The chapter $n !ure and onson-:aefe# chapter 17 covers the #og$n &anage&ent fac$#$t$es and ""%)

    or secur$t., each t.pe and &ethod can have a NGo#es"##o*ed &ethod annotat$on attached) Ner&$t"## $s thedefau#t $f not spec$f$ed) f a Go#es"##o*ed spec$f$cat$on $s g$ven on a t.pe, $t app#$es to a## &ethods un#essoverr$dden) The per&$ss$ons can a#so +e #ocated $n an =A f$#e,

    n add$t$on to spec$f.$ng the ro#es that have access to an enterpr$se +ean?s &ethods, the dep#o.er can a#so spec$f. therun"s ro#e for the ent$re +ean) Th$s ro#e *$## +e $n effect for a## &ethods eBecuted *$th$n the spec$f$ed &ethod)

    ost of the secur$t. features d$scussed so far have focused on dec#arat$ve secur$t., +ut there $s a#so progra&&at$csecur$t. prov$ded +. t*o &ethods'

    public interface 'BContext {

    &rincipal $etCaller&rincipal(); boolean isCaller%nRole(Strin$ roleame);};

    t $s up to the deve#oper to $&p#e&ent ca##s to these to create a secur$t. po#$c.)

    There $s #$tt#e d$scuss$on $n the chapter a+out ho* the ro#es are def$ned or assoc$ated *$th the user) erhaps that $s$&p#e&entat$on-spec$f$c)

    age 21 of 35

  • 7/25/2019 Jboss-ejb

    22/35

    n the %ea& fra&e*or, there are add$t$ona# fac$#$t$es for hand#$ng secur$t., $nc#ud$ng an authent$cate &ethod *h$chcan +e def$ned, and an authent$cat$on co&ponent that $s t.p$ca##. created v$a the co&ponent conf$gurat$on fac$#$t$es)

    *he *i,er erviceThe t$&er serv$ce can a$d $n &anag$ng schedu#ed tass) or eBa&p#e, .our +us$ness app#$cat$on &a. have to run ada$#. report to deter&$ne $nventor. #eve#s and auto&at$ca##. send out restoc$ng re;uests) or &ost #egac.

    app#$cat$ons, $t $s t.p$ca# to have a +atch >o+ to c#ean up te&porar. ta+#es at the start or end of each da.) The Un$Bcronut$#$t. $s pro+a+#. the &ost popu#ar and *e##-#oved schedu#$ng ut$#$t.)

    n ((, the T$&er %erv$ce $s a *rapper over the OuartF pacage)

    The (! 3 t$&er serv$ce $s +ased on the $dea of t$&e-de#a.ed ca##+acs) The conta$ner $nvoes the t$&eout &ethodon .our +eha#f *hen the t$&e $nterva# .ou spec$f. e#apses)

    (Ba&p#e'

    -e% ervicesava (( 5 prov$des a ro+ust p#atfor& on *h$ch .ou can +u$#d and dep#o. *e+ serv$ces) ava (( 5 a##o*s .ou to+u$#d *e+ serv$ces *$th e$ther regu#ar ava c#ass or (! 3 state#ess sess$on +eans) n th$s sect$on *eI##+r$ef#. $ntroduce the *e+ serv$ces p#atfor& and then eBp#a$n *h. .ou *ou#d choose (! 3 sess$on +eans overs)

    Standards

    Specification Purpose

    ava " for =A e+ %erv$ces 2)0 #atfor& spec$f$cat$on

    ava " for =A !$nd$ng 2)0 !$nd$ng for %A to ava

    % !as$c rof$#e 1)1 nteropera+$#$t. *$th )J(T

    e+ %erv$ces etadata 2)0 etadata approach to def$ne *e+ serv$ce

    ava " for =A GC 1)1 !ac*ard co&pat$+$#$t. *$th 2(( 1)4 *e+ serv$ces

    n (! 3, .ou def$ne a *e+ serv$ce *$th an annotat$on)

    %ee a#so our docu&ent Jotes on e+ %erv$ces and Too#s)

    age 22 of 35

  • 7/25/2019 Jboss-ejb

    23/35

    %mplementation o# /e( Services%upport for *e+ serv$ces $n (! 3)0 $s +ased on the ava " for =A-+ased e+ %erv$ces "=-% 2)0spec$f$cat$on, as *e## as $ts predecessor, the ava " for =A-+ased GC "=-GC 1)1) The reason for the na&echange $s to conve. that *e+ serv$ces are &ore than >ust GC-#$e $nnovat$on)

    "=-GC uses a serv$ce-refV dep#o.&ent e#e&ent $n the (! dep#o.&ent descr$ptor, and a f$#e that def$nes the

    "=-GC &app$ng for the serv$ce) The &app$ng f$#e $s der$ved fro& the %A f$#e for the serv$ce) The p$eces $nthe dep#o.&ent $nc#ude the %A f$#e, the &app$ng f$#e, and the *e+serv$ces)B f$#e)

    e+ %erv$ces can +e created us$ng the Ne+%erv$ce annotat$on, *h$ch &aes $t eas. to def$ne and ca## one) Theseare the %G-181 annotat$ons) These are s$&$#ar to the %a#esforce)co& c#a$& that an. &ethod $n "peB Code can +e a*e+ serv$ce *$th >ust one #a+e#$ng annotat$on) Co&pared to the ear#$er efforts of def$n$ng dep#o.&ents throughver.-hard-to-construct =A f$#es, th$s shou#d &ae e+ %erv$ce $&p#e&entat$ons &uch eas$erS

    The $&p#e&entat$on of "=-% $n !oss $s +ased on the "pache "B$s pro>ect, start$ng *$th !oss 4)0)B)

    E$amplespac.a$e comtitan/ebser"ice;

    import 2a"axe2bStateless;import 2a"ax2/sIebSer"ice;import 2a"ax2/sIebetod;

    @Stateless@IebSer"ice(name -

    Broader Java EE *o$ics(! 3)0 and the +ehav$or *$th$n the (! conta$ner $s >ust a part of the *ho#e ava (( p$cture)

    Servlets JSP and JS+h$#e (! 3 $s pr$&ar$#. des$gned to dea# *$th server-s$de funct$ona#$t., $t has to +e $ntegrated *$th a front end atso&e po$nt $n order for the *ho#e app#$cat$on to do an.th$ng usefu# as far as an end user $s concerned) e use theter&s presentat$on t$er and *e+ t$er $nterchangea+#.)

    ne approach to runn$ng an (! app#$cat$on $s to *r$te a c#$ent progra& that &aes the ca##s to the %ess$on +eans):o*ever, a &ore co&&on approach $s to create a screen-+ased *e+ app#$cat$on that uses %erv#ets)

    The presentat$on s$de of a (( *e+ app#$cat$on $s to use serv#ets co&+$ned *$th ava%erver ages or ava%erveraces)

    The presentat$on *ou#d t.p$ca##. +e +u$#t fro& % co&ponents that have +ac$ng +eans that fetch or send content tothe %ess$on!eans of the app#$cat$on) Co&&on#. used serv#et fra&e*ors $nc#ude %truts see our docu&ent Joteson %truts and T$#es, %pr$ng DC see our docu&ent Jotes on %pr$ng, or % see our docu&ent Jotes on ava%erver aces or %ea& see our docu&ent Jotes on !oss %ea&)

    Java ail(( 5 has ava a$# fac$#$t$es) %ee our docu&ent Jotes on ava a$#)

    age 23 of 35

  • 7/25/2019 Jboss-ejb

    24/35

    ,o!!in!%ee our docu&ent Jotes on ava Aogg$ng)

    +ittin! the pieces to!etherCo&+$n$ng serv#ets *$th the (! 3)0-+ased +acend g$ves'

    DeploymentTo understand (! pacag$ng, .ou &ust cons$der ho* $t f$ts $nto the +$gger p$cture of ava (( pacag$ng and no**hat const$tutes a co&p#ete enterpr$se ava app#$cat$on) Up to th$s po$nt *e have focused on us$ng (! co&ponentssuch as sess$on +eans and !s to +u$#d +us$ness #og$c and " ent$t$es to $&p#e&ent .our pers$stence code):o*ever, .our app#$cat$on *$## not +e co&p#ete *$thout a presentat$on t$er that accesses the +us$ness #og$c .ou +u$#t

    *$th (!s) ost #$e#., .ouIve used standard techno#og$es such as % or % to +u$#d the *e+ t$er of .ourapp#$cat$ons) These *e+ app#$cat$ons, together *$th (!s, const$tute an enterpr$se app#$cat$on that .ou can dep#o. toan app#$cat$on server)

    To dep#o. and run an app#$cat$on, .ou have to pacage the co&p#ete app#$cat$on togetherPthe *e+ &odu#e and(!sPand dep#o. to an app#$cat$on server) Usua##. .ou *$## group s$&$#ar p$eces of the app#$cat$on together $n&odu#es) ava (( def$nes a standard *a. of pacag$ng these &odu#es $n "G f$#es, and spec$f$es the for&ats forthese "Gs) ne of the advantages of hav$ng these for&ats def$ned as part of the spec$f$cat$on $s that the. areporta+#e across app#$cat$on servers)

    age 24 of 35

  • 7/25/2019 Jboss-ejb

    25/35

    The ta+#e +e#o* #$sts the arch$ves or &odu#es supported +. ava (( 5 and the$r contents) Jote that each arch$ve t.pe$s used for pacag$ng a spec$f$c t.pe of &odu#e, such as (! or *e+) or $nstance, a "G $s used to pacage a *e+-t$er app#$cat$on &odu#e, and the ("G f$#e $s $ntended to +e the \+er arch$ve conta$n$ng a## the other arch$ves so that$n the end, .ouIre on#. dep#o.$ng one f$#e) The app#$cat$on server *$## scan the contents of the ("G and dep#o. $t)

    Type Description Descriptor Contents

    C"G C#$entapp#$cat$onarch$ves

    applicationclientxml

    Th$c ava c#$ent for (!s)

    ("G (nterpr$seapp#$cat$onarch$ve

    applicationxml ther ava (( &odu#es such as (!-"Gs)

    (!-"G

    (! avaarch$ve

    e2b2arxml %ess$on +eans, &essage-dr$ven +eans, and opt$ona##. ent$t$es)Jeeds a persistencexml$f ent$t$es are pacaged)

    G"G Gesourceadapter arch$ves

    raxml Gesource adapters)

    "G e+ app#$cat$onarch$ves

    /ebxml e+ app#$cat$on art$facts such as serv#ets, %s, %, stat$cf$#es, etc) (nt$t$es can a#so +e pacaged $n th$s &odu#e) Jeedsa persistencexml$f ent$t$es are pacaged)

    To create these f$#es, .ou can use the 2arut$#$t. that co&es *$th K) The f$na# step $s to asse&+#e a## the "G f$#es

    $nto one ("G f$#e for dep#o.&ent) n 11)3)1 *e sho* .ou a +u$#d scr$pt that creates a "G f$#e) (ach of these "Gt.pes conta$ns an opt$ona# dep#o.&ent descr$ptor that descr$+es the arch$ve) "s *e have +een d$scuss$ng throughoutth$s +oo, .ou can use &etadata annotat$ons $nstead of a dep#o.&ent descr$ptor)

    n th$s sect$on, *e focus pr$&ar$#. on the ("G f$#e and the (!-"G f$#e, *h$ch conta$ns the sess$on and &essage-dr$ven +eans, as *e## as ent$t$es) Rour ("G f$#e &ust have an app#$cat$on)B f$#e at the top #eve#, *h$ch prov$des a&ap of the conta$ned p$eces'

    age 25 of 35

  • 7/25/2019 Jboss-ejb

    26/35

    The )ear f$#e $s created +. runn$ng the >ar ut$#$t. prov$ded $n ava &u#t$p#e t$&es for the d$fferent #a.ers, or &oreco&&on#. *r$t$ng an "nt scr$pt that does that sa&e th$ng) The "nt vers$ons 1)6 and #ater have tass for +u$#d$ng)*ar and )ear f$#es)

    EJB Best !racticesost of th$s +ased on Chapter 21 of the !ure and onson-:aefe# (! 3)0 +oo) The chapter $s a+out rea#*or#d use, and $t prov$des gu$de#$nes for eva#uat$ng conta$ner prov$ders, for deter&$n$ng *hen to use ent$t. andsess$on +eans, etc)

    The structure that the eBa&p#e creates for the trave# agent s.ste& $s ;u$te app#$ca+#e to trad$ng s.ste&s, as fo##o*s'

    age 517 d$scusses *hether or not to use (!?s) %$tuat$ons *here the. are strong $nc#ude'

    %$ng#e and &u#t$-s.ste& +us$ness transact$ons

    $str$+uted funct$ona#$t.

    age 26 of 35

  • 7/25/2019 Jboss-ejb

    27/35

    orta+#e co&ponents not c#asses

    "pp#$cat$ons re#.$ng on as.nchronous &essag$ng

    %ecur$t. ro#es

    ers$stence conteBt &anage&ent

    "#ternat$ves #$sted $nc#ude %pr$ng for the runt$&e fra&e*or, and :$+ernate or $!at$s for the pers$stence fac$#$t$es)

    The chapter sa.s that *$## +e ret$red)

    JBoss /or(oo

    /or(oo 12 covers deployment con#i!uration directory structure etc.The d$rector. structure of !oss $s'

    The eBa&p#es are organ$Fed as source code p#us conf$gurat$on $nfor&at$on, and an ant scr$pt that hand#es +u$#d anddep#o.&ent) The ant scr$pt t.p$ca##. has targets #$e defau#ttasV *h$ch does a co&p$#e and dep#o.&ent,co&p$#e, c#ean, and c#ean)d+, and run)c#$ent)

    :.person$c $s used as the data+ase for !oss +. defau#t)

    age 27 of 35

  • 7/25/2019 Jboss-ejb

    28/35

    /or(oo 2 #or chapter 42 coverin! #irst (eans%o&e good progra&&$ng eBa&p#es, s$&$#ar to those $n the !oss at or +oo, eBcept *$th a c#$ent progra& that $snot *e+-+ased) n th$s chapter, .ou create a c#$ent that $nteracts *$th a %ess$on!ean ca##ed Trave#"gent)

    The do&a$n o+>ect $s Ca+$n, and the sess$on!ean $s Trave#"gent!ean and Trave#"gentGe&ote) The pers$stence)Bf$#e spec$f$es that the efau#t ata source $s to +e used) Th$s $s #ocated $n the resources/(T"-J d$rector.)

    The chapter a#so d$scusses ho* to use the = $nterface screen to start and stop the :.person$c serv$ce, and to the:%OA ata+ase anager to v$e* contents of the d+)

    There are three parts to th$s chapter' the f$rst uses the code fro& Chapter 4, the second uses J +$nd$ngs *$thannotat$on to overr$de the defau#t J +$nd$ng, and the th$rd uses a !oss-spec$f$c =A dep#o.&ent descr$ptor#ocated $n (T"-J/>+oss)B)

    /or(oo 32 #or chapter 52 coverin! the entity mana!er and persistenceCovers runn$ng eBa&p#es of us$ng pers$stence for co&p#eB o+>ects such as the cru$se/dec/ca+$n tree)

    n these eBa&p#es, *e focus on the (nt$t.anager, *h$ch $s found us$ng'

    Ners$stenceUn$tun$tJa&eWt$tan pr$vate (nt$t.anageractor. factor.ects used are' %h$p, Ca+$n, Cru$se, Custo&er, hone, Cred$tCard, and Geservat$on)

    age 28 of 35

  • 7/25/2019 Jboss-ejb

    29/35

    The pr$&ar. one that gave us c#ar$f.$ng $ns$ghts *as $nverse) (ach re#at$onsh$p has an o*n$ng s$de) Rou &ust setthe $nfor&at$on $nto the o*n$ng s$de for $t to +e pers$sted) or $nstance custo&er''cred$tCard $s o*ned +. custo&er)

    /or(oo 62 #or chapter 92 coverin! mappin! o# inheritance relations%et up the data+ase for ta+#es that have $nher$tance, tr. out the d$fferent &odes)

    The do&a$n o+>ects that are used are' erson, Custo&er, and (&p#o.ee) The &ode#$ng approaches used are'

    %$ng#e ta+#e per h$erarch.)

    %$ng#e ta+#e per c#ass

    Jed $nher$tance strateg.

    /or(oo 82 #or chapter :2 coverin! ;ueriesOu$te a #ong one, as $t goes through each of the *a.s to descr$+e and eBecute ;uer$es, such as Cr$ter$a, etc) t $s+ased on a standa#one c#$ent, *h$ch &eans that th$s progra& $s a#&ost $dent$ca# to one that cou#d +e *r$tten for the:$+ernate +oo) The t.pes of ;uer$es d$scussed $nc#ude'

    se#ect

    para&eters

    pag$ng J

    JJ(G J

    UT(G J

    %TJCT

    :(G( C#ause

    %, AK(

    "ggregat$on and @roup !.

    %ect$on 9)2 covers Jat$ve %OA Ouer$es, though *e have tended to avo$d such, and thus s$pped over th$s sect$on)

    /or(oo 92 #or chapter 102 coverin! call(acs and eventsTh$s $s a good one to carr. out, s$nce a## of these concepts are re#at$ve#. ne*)

    101' or ca##+acs, *e use a &od$f$ed vers$on of Custo&er, that has pr$nt state&ents $n &ethods decorated *$thNreers$st, Nosters$st, and &ore)%$nce these pr$nt state&ents are *$th$n the !oss server process, the. appear on the !oss output conso#e)

    102' or events, *e create a A$stener c#ass that gather a## ent$t. stats, and reg$ster $t as a = +ean) Th$s $s done$n the >+oss-serv$ce)B f$#e) Gun the c#$ent, then go to the !oss = anage&ent Conso#e, f$nd the +ean, and$nvoe $ts output &ethod)

    :o*ever, ne$ther of these *or+oo un$ts prov$de &uch $nfor&at$on a+out *hen .ou *ou#d *ant to use th$sfac$#$t.)

    /or(oo :2 #or chapter 112 coverin! session (eansTh$s chapter puts &ost of the p$eces together regard$ng sess$on +eans $n (! 2)0 and (! 3)0) The eBerc$ses heresho* .ou ho* to +u$#d one of each)

    (Berc$se 111' %tate#ess %ess$on !ean' th$s $s a s$&p#e pa.&ent processor, us$ng annotat$ons to reg$ster $n the (!fra&e*or) t a#so has a conf$gura+#e set of #$&$ts for va#$dat$on, eBpressed as $n-a+#e resources(Berc$se 112' =A verr$de' th$s uses =A to change the #$&$ts(Berc$se 113' "nnotat$on#ess %tate#ess %ess$on !ean' th$s $s the (! 2)0 counterpart of the a+ove

    age 29 of 35

  • 7/25/2019 Jboss-ejb

    30/35

    (Berc$se 114' %tatefu# %ess$on !ean' th$s $s ;u$te $nterest$ng eBa&p#e, as $t &a$nta$ns sess$on on the server, +ut hasthe c#$ent prov$de a d$spatch #oop to as ;uest$ons ;uer$es or &ae se#ect$ons) n the eBa&p#e, .ou can ;uer. andse#ect the cru$se, the sh$p, and the custo&er, then carr. out a reservat$on) The annotat$ons on the statefu# +ean are'

    @Statefulpublic class !ra"el#$entBean implements !ra"el#$entRemote {

    @&ersistenceContext(unitame-

  • 7/25/2019 Jboss-ejb

    31/35

    /or(oo 102 #or chapter 12 coverin! messa!e-driven (eansThe eBa&p#e $s s$&$#ar to the a+ove c#$ent progra& eBcept that the sender $s no* us$ng % ;ueues, and the +eansare &essage-dr$ven) The +eans a#so send out &essages on another ;ueue) "#so, .ou need to dep#o. the % ;ueues+efore an.th$ng e#se) These ;ueues have na&es such as t$tan-Geservat$onOueue and t$tan-T$cetOueue) These aredef$ned through the fo##o*$ng f$#es'

    5ser"er6 5mbean code-

  • 7/25/2019 Jboss-ejb

    32/35

    import 2a"axe2bO;import 2a"axinterceptorO;

    public class &rofiler { @#round%n"o.e public 4b2ect profile(%n"ocationContext in"ocation) tro/s 'xception { lon$ start!ime - Systemcurrent!imeillis();

    try { return in"ocationproceed(); } 8inally { lon$ end!ime - Systemcurrent!imeillis() start!ime; Systemoutprintln(ar f$#es, a server f$#e and a c#$ent f$#e) TheTrave#"gent!ean $s $n the server, and has +een annotated *$th Ne+%erv$ce and Ne+ethod annotat$ons) Th$sena+#es !oss to construct the *sd# f$#e, *h$ch can +e read at

    http'//#oca#host'8080/t$tan[*sd#

    The eBa&p#e starts *$th use of "=-GC, *h$ch &eans that the "=-GC &app$ng f$#e $s used) "ctua##., *hat theauthor $s do$ng $s sho*$ng ho* to use "=-% on the server s$de, and sho*$ng co&pat$+$#$t. *$th the ear#$er "=-GC +. ca##$ng the server *$th a "=-GC c#$ent) Th$s d$dn?t *or due to so&e d$fference +et*een !oss 4)0)B and4)2)B, so *e converted the c#$ent over to us$ng "=-%, as *e## as updat$ng to "=-% 2)0)3)

    age 32 of 35

    http://localhost:8080/titan?wsdlhttp://localhost:8080/titan?wsdl
  • 7/25/2019 Jboss-ejb

    33/35

    Gunn$ng the c#$ent code produces the sa&e output as $n eBerc$se 4)1, +ecause $t $s a#so sett$ng up one ca+$n ro* $nthe data+ase) :o*ever, the c#$ent $s runn$ng a *e+ serv$ce $nvocat$on to tr$gger the process$ng) There are t*opr$&ar. f$#es that &ae up the c#$ent) The f$rst def$nes the serv$ce, *$th annotat$ons'

    packagecomtitantra"ela$ent;

    import2a"ax2/sIebetod;import2a"ax2/sIeb&aram;import2a"ax2/sIebResult;import2a"ax2/sIebSer"ice;

    importcomtitandomainCabin;

    @IebSer"ice(name-

    packagecomtitanclients;

    import2a"anetER*;

    import2a"axxmlnamespace9ame;import2a"axxml/sSer"ice;

    importcomtitandomainCabin;import comtitantra"ela$ent!ra"el#$entSer"ice;

    publicclassClient {

    publicvoiddo!est(Strin$GH ar$s) { try{ ER* /sdlER* - newER*(??localost>PFPF?titanJ/sdl< );

    9ame ser"ice9ame - new9ame(??localost Ser"ice remoteSer"ice - Ser"icecreate(/sdlER*, ser"ice9ame);

    !ra"el#$entSer"ice a$ent -(!ra"el#$entSer"ice) remoteSer"ice$et&ort(!ra"el#$entSer"iceclass);

    Cabin cabinM= - newCabin(); cabinM=set%d(=); cabinM=setame(

  • 7/25/2019 Jboss-ejb

    34/35

    } catch('xception e) { eprintStac.!race(); } }

    publicstaticvoidmain(Strin$GH ar$s) { Client client - newClient(); clientdo!est(ar$s);

    }}

    19)2' Us$ng a )J(T c#$entJot re#evant

    /or(oo 162 the class loader!oss? c#ass #oad$ng sche&e $s s$&$#ar to the strateg. used +. other 2(( app#$cat$on servers, as sho*n +e#o*'

    Co&&on c#ass #oader $ssues'

    Jot enough v$s$+$#$t. Q a >ar or d$rector. $s not ava$#a+#e

    age 34 of 35

  • 7/25/2019 Jboss-ejb

    35/35

    JoC#assefound(rror Q one add$t$ona# reason $s that a c#ass can depend upon another c#ass *h$ch cannot

    +e found

    Too &uch v$s$+$#$t. Q there &a. +e dup#$cate c#asses

    %o#v$ng $ssues'

    Kno* the +as$cs of C#assAoaders and ho* .our part$cu#ar dep#o.&ent env$ron&ent uses the&)

    ae sure there are no dup#$cate c#asses or "G f$#es (ncapsu#ate/$n$&$Fe D$s$+$#$t. to avo$d C#assCast(Bcept$on

    Use the correct C#assAoader) Use the ThreadConteBt C#assAoader)

    /or(oo 182 JBoss Data(ase "onnection SetupTh$s sect$on descr$+es the organ$Fat$on of connect$on $nfor&at$on ver. s$&$#ar to :$+ernate conf$gurat$on f$#es and*here to store $t $n the !oss env$ron&ent)

    O$en Issues"ssur$ng sca#a+$#$t. th$s $&p#$es sett$ng up eff$c$ent data+ase sche&a, us$ng &u#t$p#e servers, &anag$ng c#usters $son#. part$a##. covered $n the &ater$a# *e have read so far)

    "ssur$ng fau#t-to#erance th$s $&p#$es sett$ng up hot stand+. severs $s not covered at a## $n the &ater$a# read so far)e th$n that $t $s $n the !oss $n "ct$on +oo)