Distributed Computing:- Different applications belong to different organizations may want to communicate and share the information among them. This kind of cases need Distributed environment.
With Distributed applications, one organization can expose remote
business service to their business partners and other organizations acting as business partners can consume the remote business services. To develop the Distributed applications, you need distributed technology like R!, "#$%, "#$&, W"$ '"R(!)"' etc. Distributed applications can be of two types. *+ omogeneous applications %+ eterogeneous applications
Homogeneous applications:- When two applications which are implemented in same Technology Technology standards then that applications is called as omogeneous applications. "x- communication between two /ava based applications )ommunication between two .net based applications Heterogeneous applications:- When two applications which are implemented in different Technology Technology standards then that applications is called as eterogeneous applications. "0-communication between /ava based application and dot net based application. EJB1.0 EJB1.1 EJB2.0-J2EE1.3 EJB2.1-J2EE1.4 EJB3.0-JEE-J!"1.2 EJB3.1-JEE#-J!"2.0
EJB $Enterprise Ja%a Beans&:-"#$ Beans&:-"#$ is a distributed technology which allows you to do two things mainly. *+ Developing and exposing remote business service. %+ )onsuming remote business service. !n addition to the Remote 'ervices, "#$ offers many middle level services as shown below. !er%ices 'ame
R!
"#$%.1
"#$%.*
"#$&.1
"#$&.*
()* (E+E( !E,+CE! %
2etworking
yes
yes
yes
yes
yes
&
'treams
yes
yes
yes
yes
yes
3
Threads
yes
yes
yes
yes
4es
iddle-le%el ser%ices 5
Remoting services
yes
yes
yes
yes
4es
6
Transaction 'ervices 7#T'+
2o
yes
yes
yes
yes
8
'ecurity anagement 7#99'+
2o
4es
4es
4es
4es
:
;ersistence anagement
2o
4es
4es
4es
4es
*1
essaging7#',D$+
2o
4es
4es
4es
4es
**
Timer 'ervice
2o
2o
4es
4es
4es
*%
Resource anagement
2o
4es
4es
4es
4es
*&
2o
4es
4es
4es
4es
*3
Web 'ervice 'upport
2o
2o
2o
4es
4es
*=
Dependency !n/ection
2o
2o
2o
4es
4es
*5
!nterceptors
2o
2o
2o
4es
4es
/pes ) EJB!:1& !ession Beans 2& essage Dri%en Beans 3& Entit Beans $J in EJB3&
1& !ession Beans:Beans:- 'ession $eans are designed to implement the business logic of the application. 'ession $eans sit in business layer of 2tier 9rchitecture. /5ere are t6o tpes o !essions Beans 1& !tateless !ession Beans$!(!B& 2& !tateul !ession Beans$!"!B& •
EJB2 !tateless !ession Beans $!(!B&:- "#$% $!(!B&:- "#$% 'tateless 'ession
•
$eans are designed to implement single re>uestresponse re>uirements and not suitable for managing clients conversational state. 'ingle re>uestresponse means single method call7'<'$ instance is e>uivalent to ttp'essionRe>uest in web application i.e ttp'esion in servlet+
*5en ou de%elop t5e EJB2 !(!B7 ou need to c5ec8 t5e ollo6ing. 9+ ome ome !nte !nterfa rface ce *+ ome interface interface can extend extend "#$ome "#$ome or "#$
3+ !f $usiness $usiness interface interface is extending extending "#$ob/ect "#$ob/ect then 2o need to throw throw Remote"xcepion only. )+ $e $ean an )las )lass s *+ $ean class has to implement implement /avax.e/b.'es /avax.e/b.'ession$ sion$ean ean interface interface %+ $ean class class has to override override all the abstract abstract methods methods of 'ession$ean 'ession$ean interface 9+ set'es set'essio sion)o n)onte ntext7 xt7++ $+ e/bRem e/bRemove ove7+ 7+ )+ e/b9ctiv e/b9ctivate ate7+ 7+ D+ e/b;assiva e/b;assivate7+ te7+ &+ $ean class class has to implement implement home interface interface create7+ create7+ method method as e/b)reate7+ with void as return type. 3+ $ean class class has to implement implement all the the business business logic method method of $usiness interface. =+ 'pecify 'pecify the following following in e/b/ar e/b/ar..xml @sessiontypeA'tateless@BsessiontypeA D+ '<'$Cs '<'$Cs lifecyle lifecyle methods methods are 9+ set'es set'essio sion)o n)onte ntext7 xt7++ $+ e/b)r e/b)rea eate te7+ 7+ )+ e/bRem e/bRemove ove7+ 7+ D+ e/b9ctiv e/b9ctivate ate7+ 7+ "+ e/b;as e/b;assiva sivate7 te7++ 2ote-
e/b9ctivate7+ and e/b;assivate7+ methods will not be called in the
'<'$Cs life. '<'$Cs resource management techni>ue is instance pooling.
9999999999999EJB2 !(!B (ieccle diagram999999999
!(!B $ "or client /as8s and Container /as8s&
Client /as8s
Containser /as8s
'o ctions
1&9t )ontainer startup,"#$ container creates the pool of '<'$ instances and places them in the pool. To create one '<'$ instance, )ontainer will do the following. & )reates the empty instance by calling new!nstance7+ method. B& )reates the 'ession)ontext ob/ect C& !nvokes the set'ession)ontext7+ method by passing 'ession)ontext ob/ect as parameter to initialize the bean instance with resource called 'ession)ontext. D& !nvokes e/b)reate7+ method to initialize the bean instance with re>uired resource. E& ully initialized bean instance will be placed in pool.
2&Client in%o8es t5e Business operation
2& When client invokes the $usiness operation on business interface ob/ect then container will do the following. 9+;icks the instance from pool $+ invokes the same business operation on that bean instance. )+ 9fter finishing the execution of business operation, bean instance will be placed in pool.
3&'o ction
3& 9t )ontainer shutdown time,"#$ container destroys the pool of instances by doing the following tasks. 9+ ;icks the instance from pool. $+ invokes e/bRemove7+ on that bean instance to clean the resources, which is initialized by e/b)reate7+ )+ destroys the bean instance.
•
•
EJB3 !tateless !ession Beans $!(!B&:- "#$& 'tateless 'ession $eans are designed to implement single re>uestresponse conversational state. 'ingle re>uestresponse means single method call *5en ou de%elop t5e EJB3 !(!B7 ou need to c5ec8 t5e ollo6ing. 9+ $usiness interface *+ $usiness interface must be marked with either ERemote or E
$+ $ean )lass *+ $ean class has to marked with E'tateless annotations. %+ $ean class has to implement $usiness interface directly and has to override all the business logic method of $usiness interface. &+ !f you want to initialize the $ean interface with resources like 'ession)ontext , FserTransaction etc then mark them with EResource annotation. 3+ !f you want to initialize the $ean instance with any other resources then write your own method with initialization code and mark that with E;ost)onstruct annotation. =+ !f you want to clean the resources then write your own method with cleanup code and mark that with E;reDestroy annotation.
)+ "#$& '<'$Cs resource management techni>ue is instance pooling.
99999999999EJB3 !(!B (ieccle diagram99999999999
Client /as8s
Containser /as8s
'o ctions
1&9t )ontainer startup,"#$ container creates the pool of '<'$ instances and places them in the pool. To create one '<'$ instance, )ontainer will do the following. & )reates the empty instance by calling new!nstance7+ method. B& Dependent Resources which are marked with EResource will be detected and will be in/ected. C& !nvokes the method which is marked with E;ost)onstruct to initialize the bean instance with any other re>uired resources. D& ully initialized bean instance will be placed in pool.
2&Client in%o8es t5e Business operation
2& When client invokes the $usiness operation on business interface ob/ect then container will do the following. 9+;icks the instance from pool $+ invokes the same business operation on that bean instance. )+ 9fter finishing the execution of business operation, bean instance will be placed in pool.
3&'o ction
3& 9t )ontainer shutdown time, "#$ container destroys the pool of instances by doing the following tasks 9+ ;icks the instance from pool. $+ invokes method which is marked with E;reDestory to clean the resources. )+ destroys the bean instance.
Interview Questions 1). What are the Differences between EJB 3.0 and EJB 2.1?
Differences are: 1) EJB 3.0 allows developers to program EJB components as ordinary Java objects wit ordinary Java b!siness interfaces rater tan as eavy weigt components li"e EJB # $ome% remote). #) &n EJB 3.0 yo! can !se annotation or deployment descriptors b!t in EJB # yo! ave to !se deployment descriptors. 3) EJB 3 introd!ced persistence '(& for database access. &n EJB # yo! can !se Entity bean. ) EJB 3.0 is m!c faster te EJB# 2). What are the e! features of the EJB techno"o#!?
1. EJB components are server*side components written entirely in te Java programming lang!age #. EJB components contain b!siness logic only * no system*level programming + services% s!c as transactions% sec!rity% life*cycle% treading% persistence% etc. are a!tomatically managed for te EJB component by te EJB server. 3. EJB arcitect!re is inerently transactional% distrib!ted% portable m!lti* tier% scalable and sec!re. . EJB components are f!lly portable across any EJB server and any ,-. . EJB arcitect!re is wire*protocol ne!tral/any protocol can be !tilied li"e &&,(%J2(% 44(% D5,2%etc. 3). What is the difference between EJB and $%I
Bot of tem are java sol!tion for distrib!ted comp!ting. 2& offers remote access to an object r!nning in anoter J62 and no oter services. B!t EJB offers far more services tan 2& apart from remote metod calling. EJB leverages tis remote*object feat!re of 2& and ,B $2&*&&,() wic can be
called by any 5,B' client% b!t also provides oter services s!c as persistence% transaction management% sec!rity% reso!rce management% object pooling and messaging. &).What are the wa!s for a c"ient a''"ication to #et an EJB ob(ect?
1. 4e client as te J7D& name of te EJB object8 tis name is !sed to get te EJB object. #) 4e client as te J7D& name of te ome object% tis is a more !s!al case8 tis name is !sed to get te ome object% ten a finder metod is invo"ed on tis ome to obtain one or several entity bean objects. 4e client may also invo"e a 9create9 metod on te ome object to create a new EJB object $session or entity). 3) 4e client as got a andle on an EJB object. ' andle is an object tat identifies an EJB object8 it may be serialied% stored% and !sed later by a client to obtain a reference to te EJB ,bject% !sing te getEJB,bject metod$). ) 4e client as got a reference to an EJB object% and some metods defined on te remote interface of tis Enterprise Bean ret!rn EJB objects. ). What are the different inds of enter'rise beans? -tateless session bean* 'n instance of tese non*persistent EJBs provides a service wito!t storing an interaction or conversation state between metods. 'ny instance can be !sed for any client.
-tatef!l session bean* 'n instance of tese non*persistent EJBs maintains state across metods and transactions. Eac instance is associated wit a partic!lar client. Entity bean* 'n instance of tese persistent EJBs represents an object view of te data% !s!ally rows in a database. 4ey ave a primary "ey as a !ni!e identifier. Entity bean persistence can be eiter container*managed or bean*managed. 2essage*driven bean* 'n instance of tese EJBs is integrated wit te Java 2essage -ervice $J2-) to provide te ability for message*driven beans to act as a standard J2- message cons!mer and perform asyncrono!s processing between te server and te J2- message prod!cer.
*). What is Entit! Bean? 4e entity bean is !sed to represent data in te database. &t provides an object* oriented interface to data tat wo!ld normally be accessed by te JDB5 or some oter bac"*end '(&. 2ore tan tat% entity beans provide a component model tat allows bean developers to foc!s teir attention on te b!siness logic of te bean% wile te container ta"es care of managing persistence%transactions% and access control.
4ere are two basic "inds of entity beans: container*managed ersistence $52() andbean*managed persistence $B2(). 5ontainer*managed persistence beans are te simplest for te bean developer to create and te most diffic!lt for te EJB server to s!pport. 4is is beca!se all te logic for syncroniing te bean;s state wit te database is andled a!tomatically by te container. 4is means tat te bean developer doesn;t need to write any data access logic% wile te EJB server is s!pposed to ta"e care of all te persistence needs a!tomatically.
4ere is a p!re division of roles between te two . ome &nterface is te way to comm!nicate wit te container wic is responsible for creating % locating and removing beans and emote &nterface is te lin" to te bean tat allows access to all metods and members /).What is an EJB ontet?
EJB5onte=t is an interface tat is implemented by te container% and it is also a part of te bean*container contract. Entity beans !se a s!bclass of EJB5onte=t
called Entity5onte=t. -ession beans !se a s!bclass called -ession5onte=t. 4ese EJB5onte=t objects provide te bean class wit information abo!t its container% te client !sing te bean and te bean itself. 4ey also provide oter f!nctions. -ee te '(& docs and te spec for more details
). Does the container create a se'arate instance of the #enerated EJB-oe and EJBb(ect c"asses? 4e EJB container maintains an instance pool. 4e container !ses tese instances for te EJB ome reference irrespective of te client re!est. <ile referring te EJB ,bject classes te container creates a separate instance for eac client re!est. 4e instance pool maintenance is !p to te implementation of te container. &f te container provides one% it is available oterwise it is not mandatory for te provider to implement it. aving said tat% yes most of te container providers implement te pooling f!nctionality to increase te performance of te application server. 4e way it is implemented is again !p to te implementer. 10). What4s difference between htt'session and EJB session bean ?
' session in a -ervlet% is maintained by te -ervlet 5ontainer tro!g te ttp-ession object% tat is ac!ired tro!g te re!est object. >o! cannot really instantiate a new ttp-ession object% and it doesn;t contains any b!siness logic% b!t is more of a place were tostoreobjects. ' session in EJB is maintained !sing te -essionBeans. >o! design beans tat can contain b!siness logic% and tat can be !sed by te clients. >o! ave two different session beans: -tatef!l and -tateless. 4e first one is someow connected wit a single client. &t maintains te state for tat client% can be !sed only by tat client and wen te client 9dies9 ten te session bean is 9lost9. ' -tateless -ession Bean doesn;t maintain any state and tere is no g!arantee tat te same client will !se te same stateless bean% even for two calls one after te oter. 4e lifecycle of a -tateless -ession EJB is sligtly different from te one of a -tatef!l -ession EJB. &s EJB 5ontainer;s responsibility to ta"e care of "nowing e=actly ow to trac" eac session and redirect te re!est from a client to te correct instance of a -ession Bean. 4e way tis is done is vendor dependant% and is part of te contract.
11). What are the e! benefits of the EJB techno"o#!? 1. apid application development #. Broad ind!stry adoption 3. 'pplication portability . (rotection of &4 investment 12). Wh! do we have a reove ethod in both EJB-oe and EJBb(ect?
14). Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a vale in the HttpSession !ro" inside an EJB? >o! can pass te ttp-ession as parameter to an EJB metod% only if all objects in session are serialiable. 4is as to be consider as Apassed*by*val!eA% tat means tat its read*only in te EJB. &f anyting is altered from inside te EJB% it wont be reflected bac" to te ttp-ession of te -ervlet 5ontainer.4e Apass*by*referenceA can be !sed between EJBs emote &nterfaces% as tey are remote references. <ile it &- possible to pass an ttp-ession as a parameter to an EJB object% it is
considered to be Abad practice $1)A in terms of object oriented design. 4is is beca!se yo! are creating an !nnecessary co!pling between bac"*end objects $ejbs) and front*end objects $ttp-ession). 5reate a iger*level of abstraction for yo!r ejbs api. ater tan passing te wole% fat% ttp-ession $wic carries wit it a b!nc of ttp semantics)% create a class tat acts as a val!e object $or str!ct!re) tat olds all te data yo! need to pass bac" and fort between front*endCbac"*end
1#). What is the di$erence between a %oarse &rained? Entit' Bean and a (ine &rained? Entit' Bean? ' Afine grainedA entity bean is pretty m!c directly mapped to one relational table% in tird normal form. ' Acoarse grainedA entity bean is larger and more comple=% eiter beca!se its attrib!tes incl!de val!es or lists from oter tables% or beca!se it A ownsA one or more sets of dependent objects. 7ote tat te coarse grained bean migt be mapped to a single table or flat file% b!t tat single table is going to be pretty !gly% wit data copied from oter tables% repeated field gro!ps% col!mns tat are dependent on non*"ey fields% etc. ine grained entities are generally considered a liability in large systems beca!se tey will tend to increase te load on several of te EJB serverAs s!bsystems $tere will be more objects e=ported tro!g te distrib!tion layer% more objects participating in transactions% more s"eletons in memory% more EJB ,bjects in memory% etc.) 1*). Does 5tatefu" 5ession bean su''ort instance 'oo"in#? -tatef!l -ession Bean concept!ally doesn;t ave instance pooling. <at is te difference between JavaBean and EJBA ' Java Bean is a software component written in te Java programming lang!age tat conforms to te JavaBeans component specification. 4e JavaBeans '(&s became part of te 9core9 Java '(&s as of te 1.1 release of te JD?. 4e JavaBeans specification defines a Java*based software component model tat adds a n!mber of feat!res to te Java programming lang!age. -ome of tese feat!res incl!de: introspection c!stomiation events
properties persistence Enterprise JavaBeans $EJBs) are Java*based software components tat are b!ilt to comply wit Java;s EJB specification and r!n inside of an EJB container s!pplied by a J#EE provider. 'n EJB container provides distrib!ted application f!nctionality s!c as transaction s!pport%
1). What are the Inter!aces need to create to i"ple"ent Session Bean with E*"aple? -ession bean class $5artBean) ome interface $5artome) emote interface $5art) -ession bean class $5artBean) : p!blic class 5artBean implements -essionBean F -tring c!stomer7ame8 -tring c!stomer&d8 6ector contents8 p!blic void ejb5reate$-tring person) trows 5reateE=ception F if $person GG n!ll) F trow new 5reateE=ception$97!ll person not allowed.9)8 H else F c!stomer7ame G person8 H c!stomer&d G 9098 contents G new 6ector$)8 H
p!blic void ejb5reate$-tring person% -tring id) trows 5reateE=ception F if $person GG n!ll) F trow new 5reateE=ception$97!ll person not allowed.9)8 H else F c!stomer7ame G person8 H &d6erifier id5ec"er G new &d6erifier$)8 if $id5ec"er.validate$id)) F c!stomer&d G id8 H else F trow new 5reateE=ception$9&nvalid id: 9I id)8 H contents G new 6ector$)8 H p!blic void addBoo"$-tring title) F contents.addElement$title)8 H p!blic void removeBoo"$-tring title) trows Boo"E=ception F boolean res!lt G contents.removeElement$title)8 if $res!lt GG false) F trow new Boo"E=ception$title I 9not in cart.9)8 H H p!blic 6ector get5ontents$) F
ret!rn contents8 H p!blic 5artBean$) FH p!blic void ejbemove$) FH p!blic void ejb'ctivate$) FH p!blic void ejb(assivate$) FH p!blic void set-ession5onte=t$-ession5onte=t sc) FH H
ome &nterface: p!blic interface 5artome e=tends EJBome F 5art create$-tring person) trows emoteE=ception% 5reateE=ception8 5art create$-tring person% -tring id) trows emoteE=ception% 5reateE=ception8 H 4e signat!res of te ejb5reate and create metods are similar% b!t differ in important ways. 4e r!les for defining te signat!res of te cr eate metods of a ome interface follow. 4e n!mber and types of arg!ments in a create metod m!st matc tose of its corresponding ejb5reate metod. 4e arg!ments and ret!rn type of te create metod m!st be valid 2& types. ' create metod ret!rns te remote interface type of te enterprise bean. $B!t an ejb5reate metod ret!rns void.) 4e trows cla!se of te create metod m!st incl!de te java.rmi.emoteE=ception and te java=.ejb.5reateE=ception emote &nterface : p!blic interface 5art e=tends EJB,bject F
p!blic void addBoo"$-tring title) trows emoteE=ception8 p!blic void removeBoo"$-tring title) trows Boo"E=ception% emoteE=ception8 p!blic 6ector get5ontents$) trows emoteE=ception8 H 4e metod definitions in a remote interface m!st follow tese r!les: Eac metod in te remote interface m!st matc a metod implemented in te enterprise bean class. 4e signat!res of te metods in te remote interface m!st be identical to te signat!res of te corresponding metods in te enterprise bean class. 4e arg!ments and ret!rn val!es m!st be valid 2& types. 4e trows cla!se m!st incl!de te java.rmi.emoteE= 1/). -ow an! EJB b(ects are created for a Bean?
or a -ession bean * one EJB object for one bean instance. or entity bean it depends% if # !sers are accessing one row at time ten one EJB object is !sed for bot te beans oter wise for eac bean one EJB object. 1). What are the 'araeters ust fo""ow for 5ession Bean ?
&t implements te -essionBean interface. 4e class is defined as p!blic. 4e class cannot be defined as abstract or final. &t implements one or more ejb5reate metods. &t implements te b!siness metods. &t contains a p!blic constr!ctor wit no parameters. &t m!st not define te finalie metod. 20).When !ou wi"" chose 5tatefu" session bean and 5tate"ess session bean?
-tatef!l session beans are !sed wen tere is converstional state and wen tere is a need of temporary storage
-tateless session bean are !sed wen tere is no conversational state and wen session bean as to be !sed only for database access 21). What is the difference between 5tatefu" session bean and 5tate"ess session bean?
1. ' statef!l session beans can "eep data between client accesses. weras a stateless session bean cannot. #) ' statef!l seesion bean contain te state of client after seesion is e=pired. wereas a stateless bwan cnanot. 3) ' statef!l session beans !se te bean of pools for client application n after !se tem it ret!rn te bean in te pool. wereas a stateless session bean cannot. 22). What are the ca""bacs ethod in 5ession Bean ?
p!blic void ejb5reate$) FH p!blic void ejbemove$) FH p!blic void ejb'ctivate$) FH p!blic void ejb(assivate$) FH p!blic void set-ession5onte=t$-ession5onte=t sc) FH 23). -ow is 5tatefu" 5ession bean aintain their states with c"ient?
<en a client refers to a -tatef!l -ession object reference% all calls are directed to te same object on te EJB container . 4e container does not re!ire client identity information or any coo"ie object to !se te correct object. 4is means tat for a client to ens!re tat calls are directed to te same object on te container% all it as to do is to !se same reference for every call. or e=ample te following olds for all statef!l session beans:
-tatef!lome sf G ...CCget ome interface for statef!l bean -tatef!l bean1 G sf.create$)8 -tatef!l bean# G sf.create$)8 if $bean1.is&dentical$bean1))FH CCtis is tr!e if $bean1.is&dentical$bean#))FH CCtis is false CC7ote tat te second test wo!ld eval!ate to tr!e for stateless beans
4!s% if yo!;re calling a -tatef!l -ession Bean from a servlet% yo!r servlet need to "eep te reference to te remote object in te ttp-ession object between client calls for yo! to be able to direct calls to te same object on te container. @i"ewise% if yo!;re calling from an application% yo! only obtain te reference to te bean once and re!se te object tro!go!t te application session. 2&). What is the free 'oo"? 4e free pool is a data str!ct!re te EJB container !ses to cace anonymo!s instances of a given bean type. 4e free pool improves performance by re!sing objects and s"ipping container callbac"s wen it can. 2). Without hoe and reote interfaces cant we i'"eent e(b?
-tateless ejbs are never passivated. -ince stateless ejbs do not ave state% tere is no need to passivate tem. 4ey are p!t bac" into te free pool after eac metod call so tey will be available to service oter re!ests. 2+). Is ethod over"oadin# a""owed in EJB? >es yo! can overload metods -o!ld syncroniation primitives be !sed on bean metodsA * 7o. 4e EJB specification specifically states tat te enterprise bean is not allowed to !se tread primitives. 4e container is responsible for managing conc!rrent access to beans at r!ntime. 2/). What is hand"e and wh! it is used in EJB?
4e andle mecanism allows a client application to maintain a reference to an EJB object. ' andle object may be obtained by calling te getandle$) metod on te reference to an EJB object. 4e main interest is tat te andle class implements java.io.serialiable interface% wic means tat a andle may be serialied. 4is allows te client to store te andle% or to pass it to anoter process. 4e andle may ten be deserialied and !sed to obtain te reference to te EJB object% by calling te getEJB,bject$) metod. andles on session bean objects are valid !ntil te session bean object e=ists% i.e.
teir life time is limited to tat of te client. andles on entity bean objects are valid d!ring te complete life time of te entity bean object8 tis means tat s!c andles may be !sed by different clients and stored for a long time8 te EJB server olding te entity bean objects may be stopped and restarted% te andle will still be valid.
&f we consider te entity bean object of te e=ample above $a#)% te way to obtain a andle on tis object is te following $te andle class is defined in te java=.ejb pac"age): andle G a#.getandle$)84e andle object may ten be serialied and stored in a file: ,bject,!tp!t-tream o!t G new ,bject,!tp!t-tream$new ile,!tp!t-tream$9andlefile9))8 o!t.write,bject$)8 o!t.close$)8 4en% a client can read te andle% and retrieve te referenced object: ,bject&np!t-tream in G new ,bject&np!t-tream$new ile&np!t-tream$9andlefile9))8 andle G $andle) in.read,bject$)8 'cco!nt a G $'cco!nt)(ortableemote,bject.narrow$.getEJB,bject$)% 'cco!nt.class)8 4e EJB -pecification allows te client to obtain a andle for te ome interface. 4is allows te client to store a reference to an entity bean;s ome interface in stable storage. 4e client code m!st !se te java=.rmi.(ortableemote,bject.narrow$...) metod to convert te res!lt of te getEJBome$) metod invo"ed on a andle to te ome interface type 2). I'"eent 6oca" and $eote Interfaces in EJB?
emote Beans4e EJB 1.1 specification defines all EJBs as remote objects. 4is means tat every time yo! ma"e a call to an EJB% yo! are ma"ing a remote call. 4is means tat tere is considerable overead to eac EJB call% and ence performance implications. 4o combat tis% server vendors invented a way of circ!mventing te remote calls to some degree. ,racle;s sol!tion wit ,5J was te pass*by*reference setting% wic determined weter EJB objects were comm!nicated by reference to te object% or weter te wole object ad to be
passed to te client. 'n EJB as a remote interface and a ome interface% wit te e=ception of 2essageDrivenBeans. 4e remote interface e=tends te interface java=.ejb.EJB,bject and te ome interface e=tends te interface java=.ejb.EJBome. 4e EJB is accessible from any client% in any J62% provided tey ave te proper a!toriation. or e=ample% te ome and emote interfaces of an EJB called E2( may loo" li"e tis. emote: p!blic interface Emp e=tends EJB,bject F long getEmpno$) trows emoteE=ception8 void setEmpno$long newDeptno) trows emoteE=ception8 -tring getEname$) trows emoteE=ception8 void setEname$-tring newDname) trows emoteE=ception8 ome: p!blic interface Deptome e=tends EJBome F p!blic Emp create$) trows emoteE=ception% 5reateE=ception8 p!blic Dept findBy(rimary?ey$Dept(? primary?ey) trows emoteE=ception% inderE=ception8 7ote tat bot te ome and te remote interface trow a emoteE=ception in all of teir metod definitions. 4e ejb*jar.=ml deployment descriptor for tese EJBs wo!ld loo" someting li"e te snippets below: KentityL Kejb*nameLEmpKCejb*nameL KomeLejb.cmplocal.EmpomeKComeL KremoteLejb.cmplocal.EmpKCremoteL Kejb*classLejb.cmplocal.EmpBeanKCejb*classL . . .
@ocal Beans4e EJB #.0 specification standardie a means of ma"ing local connections to EJBs wit @ocal &nterfaces. or an EJB to be classed as a local EJB% it m!st implement te local versions of te ome and remote interfaces% java=.ejb.EJB@ocal,bject for te ome interface% and java=.ejb.EJB@ocalome. or a client to call te @ocal interface% tey m!st be r!nning in te same J62 as te J62 tat te EJB e=ists in. 4is means tat not only an EJB can call a local EJB % -ervlets or J-(s can also call te EJB via it;s local interface if tey are pac"aged togeter as part of same application. or e=ample% te @ocalome and @ocal interfaces of an EJB called E2( may loo" li"e tis. @ocal: p!blic interface Emp e=tends EJB@ocal,bject F long getEmpno$)8 void setEmpno$long newEmpno)8 -tring getEname$)8 void setEname$-tring newEname)8 @ocalome: p!blic interface Empome e=tends EJB@ocalome F p!blic Emp create$) trows 5reateE=ception8 p!blic Emp findBy(rimary?ey$Emp(? primary?ey) trows inderE=ception8 4e ejb*jar.=ml deployment descriptor for tese EJBs wo!ld loo" someting li"e te snippets below: KentityL Kejb*nameLEmpKCejb*nameL Klocal*omeLejb.cmplocal.EmpomeKClocal*omeL KlocalLejb.cmplocal.EmpKClocalL Kejb*classLejb.cmplocal.EmpBeanKCejb*classL Kcmp*versionL#.=KCcmp*versionL Kabstract*scema*nameLEmpKCabstract*scema*nameL . .
. 7ote tat now te local interfaces no longer trow te emoteE=ception% sowing tat tey are not remotely called metods. 'lso% te M2@ contains different elements. 4ere is now a local*ome and a local tag. 'lso we are declaring tat tis is an EJB #.= bean% !sing te cmp*version tag. 5alling @ocal Beans5alling a local bean from Java code is very simple% and very similar to !sing a remote bean. 4e code to call a remote bean is sown below. try F 5onte=t ct= G new &nitial5onte=t$)8 ,bject o G ct=.loo"!p$9Emp9)8 Empome empome G (ortableemote,bject.narrow$o% Empome.class) ret!rn empome.findByDeptno$getDeptno$))8 H catc $emoteE=ception r) F -ystem.err.println$9Error loading Employees$emote): 9 I r.get2essage$))8 ret!rn n!ll8 H catc $7amingE=ception n) F -ystem.err.println$9Error loading Employees$7aming): 9 I n.get2essage$))8 ret!rn n!ll8 H catc $inderE=ception f) F -ystem.err.println$9Error loading Employees$inder): 9 I f.get2essage$))8 ret!rn n!ll8 H 4e code for a local bean is similar% b!t we no longer ave to worry abo!t te (ortableemote,bject% as te bean is no longer remote. try F 5onte=t ct= G new &nitial5onte=t$)8 ,bject o G ct=.loo"!p$9java:compCenvC@ocalEmp9)8 Empome empome G $Empome)o8
ret!rn empome.findByDeptno$getDeptno$))8 H catc $7amingE=ception n) F -ystem.err.println$9Error loading Employees$7aming): 9 I n.get2essage$))8 ret!rn n!ll8 H catc $inderE=ception f) F -ystem.err.println$9Error loading Employees$inder): 9 I f.get2essage$))8 ret!rn n!ll8 H 's yo! can see% te local bean as to loo"!p te EJB sligtly differently% even to!g tey are r!nning in te same container. 'lso% tere is no emoteE=ception trown by te find or te create metods% so te e=ception does not ave to be ca!gt. 4ere is one more difference% and tat is in te ejb*jar.=ml deployment descriptor. or an EJB to loo" !p a local EJB% it m!st point to te correct location !sing an Kejb*local*refL tag. &f tis is not !sed% te container will not be able to find te bean. or eac EJB tat needs to !se te local EJB% te M2@ below m!st be in te deployment descriptor. KentityL Kejb*nameLDeptKCejb*nameL . . . Kejb*local*refL Kejb*ref*nameL@ocalEmpKCejb*ref*nameL Kejb*ref*typeLEntityKCejb*ref*typeL Klocal*omeLejb.cmplocal.EmpomeKClocal*omeL KlocalLejb.cmplocal.EmpKClocalL Kejb*lin"LEmpKCejb*lin"L KCejb*local*refL KCentityL 4is e=ample will allow te EJB Dept to call te local EJB Emp !sing te name @ocalEmp. 4is is re!ired beca!se EJBs can ave bot local and remote interfaces% and to call te EJB Emp via it;s remote interface te EJB Dept wo!ld
loo" !p te name Emp rater tan te local reference @ocalome. 30). -ow can I ca"" one EJB fro inside of another EJB? &n case of emote : EJBs can be clients of oter EJBs. &t j!st wor"s. eally. Nse J7D& to locate te ome &nterface of te oter bean% ten ac!ire an instance reference. or E=ample : 5onte=t ct= G new &nitial5onte=t$)8 CCget ome interface of bean CCnarrow *retype Empome lome G $Empome ) java=.rmi.(ortableemote,bject.narrow$ct=.loo"!p$9java:compCenvC@ocalEmp9)% Empome .class)8
CCget remote interface Emplbean G lome.create$)8 CCnow yo! can call b!ssiness metod on remote interface li"e lbean.do-ometing$) &ncase of @ocal : b!t we no longer ave to worry abo!t te (ortableemote,bject% as te bean is no longer remote 5onte=t ct= G new &nitial5onte=t$)8 ,bject o G ct=.loo"!p$9java:compCenvC@ocalEmp9)8 Empome empome G $Empome)o8 31). What is the difference between %essa#e Driven Beans and 5tate"ess 5ession beans
&n several ways% te dynamic creation and allocation of message*driven bean instances mimics te beavior of stateless session EJB instances% wic e=ist only for te d!ration of a partic!lar metod call. owever% message*driven beans are different from stateless session EJBs $and oter types of EJBs) in several significant ways: 2essage*driven beans process m!ltiple J2- messages asyncrono!sly% rater tan processing a serialied se!ence of metod calls. 2essage*driven beans ave no ome or remote interface% and terefore cannot be directly accessed by internal or e=ternal clients. 5lients interact wit message*
driven beans only indirectly% by sending a message to a J2- O!e!e or 4opic. 7ote: ,nly te container directly interacts wit a message*driven bean by creating bean instances and passing J2- messages to tose instances as necessary. 4e 5ontainer maintains te entire lifecycle of a message*driven bean8 instances cannot be created or removed as a res!lt of client re!ests or oter '(& calls 32). an !ou contro" when 'assivation occurs?
4e developer% according to te specification% cannot directly control wen passivation occ!rs. 'lto!g for -tatef!l -ession Beans% te container cannot passivate an instance tat is inside a transaction. -o !sing transactions can be a a strategy to control passivation. 4e ejb(assivate$) metod is called d!ring passivation% so te developer as control over wat to do d!ring tis e=ercise and can implement te re!ire optimied logic. -ome EJB containers% s!c as BE'
5onte=t ct= G new &nitial5onte=t$)8 CCget ome interface of bean CCnarrow *retype Beanome lome G $Beanome) java=.rmi.(ortableemote,bject.narrow$ct=.loo"!p$9c.train.Bean9)% Beanome.class)8 CCget remote interface Bean lbean G lome.create$)8
CCnow yo! can call b!ssiness metod on remote interface li"e lbean.do-ometing$) 3&). an the 'riar! e! in the entit! bean be a Java 'riitive t!'e such as int?
4e primary "ey can;t be a primitive type**!se te primitive wrapper classes% instead. or e=ample% yo! can !se java.lang.&nteger as te primary "ey class% b!t not int $it as to be a class% not a primitive) 3). What are the ethods of Entit! Bean?
'n entity bean consists of gro!ps of metods: 1. create metods: 4o create a new instance of a 52( entity bean% and terefore insert data into te database% te create$) metod on te bean;s ome interface m!st be invo"ed. 4ey loo" li"e tis: EntityBean5lass ejb5reateMMM$parameters)% were EntityBean5lass is an Entity Bean yo! are trying to instantiate% ejb5reateMMM$parameters) metods are !sed for creating Entity Bean instances according to te parameters specified and to some programmer*defined conditions. ' bean;s ome interface may declare ero or more create$) metods% eac of wic m!st ave corresponding ejb5reate$) and ejb(ost5reate$) metods in te bean class. 4ese creation metods are lin"ed at r!n time% so tat wen a create$) metod is invo"ed on te ome interface% te container delegates te invocation to te corresponding ejb5reate$) and ejb(ost5reate$) metods on te bean class. #. finder metods: 4e metods in te ome interface tat begin wit 9find9 are called te find metods. 4ese are !sed to !ery te EJB server for specific entity beans% based on te name of te metod and arg!ments passed. Nnfort!nately% tere is no standard !ery lang!age defined for find metods% so eac vendor will implement te find metod differently. &n 52( entity beans% te find metods are not implemented wit matcing metods in te bean class8 containers implement tem wen te bean is deployed in a vendor specific manner. 4e deployer will !se vendor specific tools to tell te container ow a partic!lar find metod so!ld beave. -ome vendors will !se object*relational mapping tools to define te beavior of a find metod wile oters will simply re!ire te deployer to enter te appropriate -O@ command.
4ere are two basic "inds of find metods: single*entity and m!lti*entity. -ingle* entity find metods ret!rn a remote reference to te one specific entity bean tat matces te find re!est. &f no entity beans are fo!nd% te metod trows an ,bject7oto!ndE=ception . Every entity bean m!st define te single*entity find metod wit te metod name findBy(rimary?ey$)% wic ta"es te bean;s primary "ey type as an arg!ment. 4e m!lti*entity find metods ret!rn a collection $ En!meration or 5ollection type) of entities tat matc te find re!est. &f no entities are fo!nd% te m!lti*entity find ret!rns an empty collection. 3. remove metods: 4ese metods $yo! may ave !p to # remove metods% or don;t ave tem at all) allow te client to pysically remove Entity beans by specifying eiter andle or a (rimary ?ey for te Entity Bean. . ome metods: 4ese metods are designed and implemented by a developer% and EJB specification doesn;t ave any re!irements for tem e=cept te need to trow a emoteE=ception is eac ome metod. 3*). What is the difference between ontainer9%ana#ed 8ersistent ,%8) bean and Bean9%ana#ed 8ersistent,B%8) ?
5ontainer*managed persistence$52() beans are te simplest for te bean developer to create and te most diffic!lt for te EJB server to s!pport. 4is is beca!se all te logic for syncroniing te bean;s state wit te database is andled a!tomatically by te container. 4is means tat te bean developer doesn;t need to write any data access logic% wile te EJB server is s!pposed to ta"e care of all te persistence needs a!tomatically.
syncroniation operation and manages te transactions for te bean a!tomatically. Bean*managed persistence gives te bean developer te fle=ibility to perform persistence operations tat are too complicated for te container or to !se a data so!rce tat is not s!pported by te container.B2( beans are not 100Q database* independent% beca!se tey may contain database*specific code% b!t 52( beans are !nable to perform complicated D2@ $data manip!lation lang!age) statements. EJB #.0 specification introd!ced some new ways of !erying database $by !sing te EJB O@ * !ery lang!age). 3+).an Entit! Beans have no create,) ethods?
>es. &n some cases te data is inserted 7,4 !sing Java application% so yo! may only need to retrieve te information% perform its processing% b!t not create yo!r own information of tis "ind 3/). What is bean ana#ed transaction? &f a developer doesn;t want a 5ontainer to manage transactions% it;s possible to implement all database operations man!ally by writing te appropriate JDB5 code. 4is often leads to prod!ctivity increase% b!t it ma"es an Entity Bean incompatible wit some databases and it enlarges te amo!nt of code to be written. 'll transaction management is e=plicitly performed by a developer. 3). What are transaction iso"ation "eve"s in EJB?
1. 4ransactionRreadR!ncommitted* 'llows a metod to read !ncommitted data from a DB$fast b!t not wise). #. 4ransactionRreadRcommitted* S!arantees tat te data yo! are getting as been committed. 3. 4ransactionRrepeatableRread * S!arantees tat all reads of te database will be te same d!ring te transaction $good for read and !pdate operations). . 4ransactionRserialiable* 'll te transactions for reso!rce are performed serial. &0). What is the difference between e(breate,) and e(b8ostreate
4e p!rpose of ejb(ost5reate$) is to perform clean*!p database operations after -O@ &7-E4s $wic occ!r wen ejb5reate$) is called) wen wor"ing wit 52(
entity beans. ejb5reate$) is called before database &7-E4 operations. >o! need to !se ejb(ost5reate$) to define operations% li"e set a flag% after &7-E4 completes s!ccessf!lly. <en wor"ing wit B2( entity beans% tis is not necessary. >o! ave f!ll control over te entire process% so yo! can place all te necessary logic s!rro!nding yo!r &7-E4 statement directly in te ejb5reate$) metod. Even if yo! are creating B2( entity beans% te recommendation wo!ld still be to incl!de an empty ejb(ost5reate$) metod. 'lto!g some application servers will not enforce it% te spec indicates tat tis placeolder so!ld be tere. &1).What is the difference between sessioncontet and entit!contet?
-ince EnterpriseBeans live in a managed container%te container is free to call yo!r EJB components metods at its leis!re. 4e container o!ses te information li"e c!rrent stat!s of bean%sec!rity credentials of te !ser c!rrently accessing te bean in one object is called EJB5onte=t ,bject. ' conte=t represents a way for beans to perform callbac"s and modify teir c!rrent stat!s -ession5onte=t is EJB conte=t for session bean Entity5onte=t is EJB conte=t for entity bean 2essage driven conte=t is EJB conte=t for message driven bean &2). What is the difference between e(b5tore,) and e(b6oad,)?
ejb-tore$) will be called before ejb(assivate$) and is !sed to store te object to persistent database. ejb@oad$) will be called before ejb'ctivate$) and is !sed to retrieve te object from persistence datastore. &3). What is the difference between E:$; J:$ and W:$ fi"e? J2EE defines three t!'es of archives<
1. Java 'rcives $J')T' J' file encaps!lates one or more Java classes% a manifest% and a descriptor. J' files are te lowest level of arcive. J' files are !sed in J#EE for pac"aging EJBs and client*side Java 'pplications. #.
4e EJB # -pec $10.U.3 * -pecial case: Nn"nown primary "ey class) says tat in cases were te (rimary?eys are generated a!tomatically by te !nderlying database% tebean provider m!st declare te findBy(rimary?ey metod to ret!rn java.lang.,bject and specify te (rimary ?ey 5lass as java.lang.,bject in te Deployment Descriptor. <en defining te (rimary ?ey for te Enterprise Bean% te Deployer !sing te 5ontainer (rovider;s tools will typically add additional container*managed fields to te concrete s!bclass of teentity bean class. &n tis case% te 5ontainer m!st generate te (rimary ?ey val!e wen te entity bean instance is created $and before ejb(ost5reate is invo"ed on te instance.) &). Is Decorator an EJB desi#n 'attern?
7o. Decorator design pattern% is te one wic e=ibits very low level r!ntime polymorpism% for te specific and single object $&nstance of te class)% b!t not for atleast for a class. &t is te st!ff to add specific f!nctionality to a single + pointed
object and leaves oters li"e it !nmodified. &t is aving close similarities li"e aspect st!ff% b!t not wit EJB st!ff. &*). What is "a>! "oadin#? @ay loading means not creating an object !ntil te first time it is accessed. @ay loading typically loo"s li"e tis:
p!blic class E=ample F private 6ector data G n!ll8 p!blic 6ector getData$) F if $tis.data GG n!ll) F tis.data G new 6ector$)8 CC @oad data into vector ... H ret!rn tis.data8 H H 4is tecni!e is most !sef!l wen yo! ave large ierarcies of objects $s!c as a prod!ct catalog). >o! can lay*load s!bordinate objects as yo! navigate down te ierarcy% and tereby only creat objects wen yo! need tem. &+). What is %essa#e Driven Bean?
'n 2DB is essentially a message cons!mer tat can listen to a message destination or a message endpoint and gets activated wen a message arrives. By design% 2DBs are anonymo!s in nat!re and ence cannot be directly invo"ed by a client. 4e only way to invo"e an 2DB is to send a message to te destination or endpoint to wic it is listening. 's 2DBs are stateless in nat!re and are not related to any specific client% tey can be pooled for conc!rrent processing of messages. &/). What is %$?
52 is an acronym for 5ontainer 2anaged elation*sips. 52% represented by te cmr fields in te deployment descriptor% wic represents te relationsip e=ists between different entities $entity beans)% wic are in t!rn e=ibiting te database to te real world. 4e relationsips are one*one% one*many% + many*many.
'll te relationsC referential integrities will be managed by container% ten te definition;s in te deployment descriptor;s are called as 5ontainer 2anaged elationsips $52).. &). an a 5ession Bean be defined without e(breate,) ethod?
4e ejb5reate$) metods is part of te bean;s lifecycle% so% te compiler will not ret!rn an error beca!se tere is no ejb5reate$) metod. owever% te J#EE spec is e=plicit: te ome interface of a -tateless -ession Bean m!st ave a single create$) metod wit no arg!ments% wile te session bean class m!st contain e=actly one ejb5reate$) metod% also wito!t arg!ments. -tatef!l -ession Beans can ave arg!ments $more tan one create metod) 0). What are the o'tiona" c"auses in EJB Q6?
<EE and ,DEB> cla!ses are optional in EJB O@ were as -E@E54 and ,2 are re!ired cla!ses. 1). an I use session beans and hibernate ,instead of entit! beans) for 'ersitance?
>es% we can. &t;s same as B2(. 2). If session has thrown :''"icaitonEce'tion wou"d !ou use EJBontet. set$o""Bacn"! ethod?
'ccording to te EJB specification% wen te 'pplicationE=ception is trown% te EJB5onte=t.setollBac",nly metod is not called. 4ypically% an enterprise bean mar"s a transaction for rollbac" to protect data integrity before trowing an application e=ception% beca!se application e=ceptions do not a!tomatically ca!se te 5ontainer to rollbac" tetransaction. or e=ample% an 'cco!nt4ransfer bean wic debits one acco!nt and credits anoter acco!nt co!ld mar" a transaction for rollbac" if it s!ccessf!lly performs te debit operation% b!t enco!nters a fail!re d!ring te credit operation.
3). What is the difference between activation and 'assivation?
4is wo!ld be te difference between 'ctivation and (assivation: <ile te bean is in te ready stage% te EJB container may decide to deactivate% or passivate% te bean by moving it from memory to secondary storage. $4ypically% te EJB container !ses a least*recently*!sed algoritm to select a bean for passivation.) 4e EJB container invo"es te bean;s ejb(assivate metod immediately before passivating it. &f a client invo"es a b!siness metod on te bean wile it is in te passive stage% te EJB container activates te bean% moving it bac" to te ready stage% and ten calls te bean;s ejb'ctivate metod. &). -ow do !ou chec whether the session is active in 5tatefu" session bean ?
&n -tatef!l session bean session is not itself a separate entity. it is contained in te bean it self. -o in order to cec" tt we need te cec" weter te -tatef!l session bean is present or not wic is done by j!st invo"ing te ome interface wit te jndi ). What is the difference between find and se"ect ethods in EJB? V ' select metod can ret!rn a persistent field $or a collection tereof) of a related entity bean. ' finder metod can ret!rn only a local or remote interface $or a collection of interfaces).
V Beca!se it is not e=posed in any of te local or remote interfaces% a select metod cannot be invo"ed by a client. &t can be invo"ed only by te metods implemented witin te entity bean class. ' select metod is !s!ally invo"ed by eiter a b!siness or a ome metod. V ' select metod is defined in te entity bean class. or bean*managed persistence% a finder metod is defined in te entity bean class% b!t for container* managed persistence it is not. *). What is the difference between "oca" interface and reote interface?
Nse remote client view wen yo! need to be s!re tat parameters passed between yo!r EJB and te client $andCor oter enterprise beans) so!ld be passed 9by val!e9 instead of 9by reference.9
-cema is !sed by te container for persistence management and relationsip management. *0). What is c"usterin#. What are the different a"#oriths used for c"usterin#?
5l!stering is te !se of m!ltiple comp!ters and storage devices to create wat seems to be a single system. 5l!stering is often !sed to increase a system;s availability and for load balancing on igly*traffic"ed
'. <en yo! get a @oc"4imed,!tE=ception wile invo"ing a statef!l session EJB% one of two tings as occ!rred: >o! ave Kallow*conc!rrent*callsL set to tr!e in yo!r weblogic*ejb* jar .=ml descriptor and yo!r call timed o!t wile waiting to be processed. 4e timeo!t !sed in tis case is te val!e Ktrans*timeo!t*secondsL element of te weblogic*ejb*jar.=ml descriptor or its defa!lt val!e of 30 seconds. >o! do not ave Kallow*conc!rrent*callsL set to tr!e and yo! attempt to invo"e a statef!l session bean tat is already b!sy processing anoter re!est. &n tis case% te second metod call will not bloc" and a @oc"4imed,!tE=ception will be trown immediately. *2). What is the "ife c!c"e of %DB? 4e lifetime of an 2DB instance is controlled by te container. ,nly two states e=ist: Does not e=ist and eady % as ill!strated in te following fig!re: 4e life of an 2DB instance starts wen te container invo"es new&nstance$) on
te 2DB class to create a new instance. 7e=t% te container calls set2essageDriven5onte=t$) followed by ejb5reate$) on te instance. 4e bean ten enters te eady state and is ready to cons!me messages. <en a message arrives for te bean% te container invo"es te on2essage$) metod of one of te available instances% passing a 2essage object in arg!ment. 2essage s can be cons!med and processed conc!rrently by !sing m!ltiple instances of te same type. 4e container invo"es ejbemove$) on te bean instance wen it no longer needs te instance. 4e bean instance can perform clean !p operations ere. *3). an an entit! bean be a "istener for J%5 essa#es? 7o. 2essage driven beans so!ld be !sed to cons!me J2- messages. *&). What is Entit! Bean. What are the various t!'es of Entit! Bean?
Entity bean represents te real data wic is stored in te persistent storage li"e Database or file system. or e=ample% 4ere is a table in Database called 5reditRcard. 4is table contains creditRcardRno%firstRname% lastRname% ssn as col!ms and tere are 100 rows in te table. ere eac row is represented by one instance of te entity bean and it is fo!nd by an !ni!e "ey $primary "ey) creditRcardRno. 4ere are two types of entity beans. 1) 5ontainer 2anaged (ersistence$52() #) Bean 2anaged (resistence$B2() *). What is II8 ? &t is &nternet &nter ,bject eso!rce Bro"er (rotocl **). Wh! don4t statefu" session beans have a 'oo"? -tatef!l session beans get instantiated once for eac seperate client re!est and it stores te client information in it% tere is no treading concept in EJB ence if tere will be an instance pool will e=ist ten tere is a possiblity of information lea" between different session objects. terefore tere is no concept of instance pooling in statef!l session bean. *+). Without usin# entit! beans can we do database transactions?
access to yo!r logic !sing 2& or web services% mailing facilities and vario!s options in persisting yo!r data to a database */). What is the use of usin# session facade desi#n 'attern in EJB45?
4ere are many !ses% important one is to red!ce networ" traffic & yo! are calling many EJB from yo!r -ervlet ten tis is not advised% beca!se it as to ma"e many networ" trips% so wat yo! do yo! call a -tateless session bean and tis in t!rn calls oter EJB% since tey are in same container tere is less networ" calls oter ting yo! can do now is yo! can convert tem to @,5'@ EJB wic as not networ" calls. 4is increases yo!r server bandwidtJ. (roblem solver tis is good for a igly available system. *). What is the difference between session and entit! beans? When shou"d I use one or the other?
'n entity bean represents persistent global data from te database8 a session bean represents transient !ser*specific data tat will die wen te !ser disconnects $ends is session). Senerally% te session beans implement b!siness metods $e.g. Ban".transfer!nds) tat call entity beans $e.g. 'cco!nt.deposit% 'cco!nt.witdraw) +0). Is it 'ossib"e to share an -tt'5ession between a J58 and EJB? What ha''ens when I chan#e a va"ue in the -tt'5ession fro inside an EJB? /
>o! can pass te ttp-ession as parameter to an EJB metod% only if all objects in session are serialiable.4is as to be consider as passed*by*val!e% tat means tat its read*only in te EJB. &f anyting is altered from inside te EJB% it wont be reflected bac" to te ttp-ession of te -ervlet 5ontainer.4e pass*by*reference can be !sed between EJBs emote &nterfaces% as tey are remote references. <ile it is possible to pass an ttp-ession as a parameter to an EJB object% it is considered to be bad practice in terms of object*oriented design. 4is is beca!se yo! are creating an !nnecessary co!pling between bac"*end objects $EJBs) and front*end objects $ttp-ession). 5reate a iger*level of abstraction for yo!r EJBs '(&. ater tan passing te wole% fat% ttp-ession $wic carries wit it a b!nc of ttp semantics)% create a class tat acts as a val!e object $or str!ct!re) tat olds all te data yo! need to pass bac" and fort between front*endCbac"*end. 5onsider te case were yo!r EJB needs to s!pport a non 44(*based client. 4is iger level of abstraction will be fle=ible eno!g to s!pport it.
+1).What is EJB ro"e in J2EE? EJB tecnology is te core of J#EE. &t enables developers to write re!sable and portable server*side b!siness logic for te J#EE platform.
+).what are %ontainer,-anaged ransactional attribtes ? 7ot -!pported 4e bean is not involved in a transaction. &f te bean invo"er calls te bean wile involved in a transaction% te invo"er;s transaction is s!spended% te bean e=ec!tes% and wen te bean ret!rns% te invo"er;s transaction is res!med. e!ired 4e bean m!st be involved in a transaction. &f te invo"er is involved in a transaction% te bean !ses te invo"er;s transaction. &f te invo"er is not involved in a transaction% te container starts a new transaction for te bean. -!pports <atever transactional state tat te invo"er is involved in is !sed for te bean. &f te invo"er as beg!n a transaction% te invo"er;s transaction conte=t is !sed by te bean. &f te invo"er is not involved in a transaction% neiter is te bean. e!ires7ew <eter or not te invo"er is involved in a transaction% tis bean starts a new transaction tat e=ists only for itself. &f te invo"er calls wile involved in a transaction% te invo"er;s transaction is s!spended !ntil te bean completes. 2andatory 4e invo"er m!st be involved in a transaction before invo"ing tis bean. 4e bean !ses te invo"er;s transaction conte=t. 7ever
4e bean is not involved in a transaction. !rtermore% te invo"er cannot be involved in a transaction wen calling te bean. &f te invo"er is involved in a transaction% a emoteE=ception is trown +3).-ow is 'ersistence i'"eented in enter'rise beans? (ersistence in EJB is ta"en care of in two ways% depending on ow yo! implement yo!r beans: container managed persistence $52() or bean managed persistence $B2() or 52(% te EJB container wic yo!r beans r!n !nder ta"es care of te persistence of te fields yo! ave declared to be persisted wit te database * tis declaration is in te deployment descriptor. -o% anytime yo! modify a field in a 52( bean% as soon as te metod yo! ave e=ec!ted is finised% te new data is persisted to te database by te container. or B2(% te EJB bean developer is responsible for defining te persistence ro!tines in te proper places in te bean% for instance% te ejb5reate$)% ejb-tore$)% ejbemove$) metods wo!ld be developed by te bean developer to ma"e calls to te database. 4e container is responsible% in B2(% to call te appropriate metod on te bean. -o% if te bean is being loo"ed !p% wen te create$) metod is called on te ome interface% ten te container is responsible for calling te ejb5reate$) metod in te bean% wic so!ld ave f!nctionality inside for going to te database and loo"ing !p te data. +&). :re we a""owed to chan#e the transaction iso"ation 'ro'ert! in idd"e of a transaction? 7o. >o! cannot cange te transaction isolation level in te middle of transaction. +). @or Entit! Beans; What ha''ens to an instance fie"d not a''ed to an! 'ersistent stora#e; when the bean is 'assivated? 4e specification infers tat te container never serialies an instance of an Entity bean $!nli"e statef!l session beans). 4!s passivation simply involves moving te bean from te ready to te pooled bin. -o wat appens to te contents of an instance variable is controlled by te programmer. emember tat wen an entity bean is passivated te instance gets logically disassociated from its remote object. Be caref!l ere% as te f!nctionality of passivationCactivation for -tateless -ession% -tatef!l -ession and Entity beans is completely different. or entity beans te ejb(assivate metod notifies te entity bean tat it is being disassociated wit a partic!lar entity prior to re!se or for dereference. +*). What is a %essa#e Driven Bean; what functions does a essa#e driven bean have and how do the! wor in co""aboration with J%5? 2essage driven beans are te latest addition to te family of component bean types
defined by te EJB specification. 4e original bean types incl!de session beans% wic contain b!siness logic and maintain a state associated wit client sessions% and entity beans% wic map objects to persistent data. 2essage driven beans will provide asyncrony to EJB based applications by acting as J2- message cons!mers. ' message bean is associated wit a J2- topic or !e!e and receives J2- messages sent by EJB clients or oter beans. Nnli"e entity beans and session beans% message beans do not ave ome or remote interfaces. &nstead% message driven beans are instantiated by te container as re!ired. @i"e stateless session beans% message beans maintain no client*specific state% allowing te container to optimally manage a pool of message*bean instances. 5lients send J2- messages to message beans in e=actly te same manner as tey wo!ld send messages to any oter J2- destination. 4is similarity is a f!ndamental design goal of te J2capabilities of te new specification. 4o receive J2- messages% message driven beans implement te java=.jms.2essage@istener interface% wic defines a single on2essage$) metod. <en a message arrives% te container ens!res tat a message bean corresponding to te message topicC!e!e e=ists $instantiating it if necessary)% and calls its on2essage metod passing te clients message as te single arg!ment. 4e message beans implementation of tis metod contains te b!siness logic re!ired to process te message. 7ote tat session beans and entity beans are not allowed to f!nction as message beans. ++).What is the advanta#e of 'uttin# an Entit! Bean instance fro the $ead! 5tate to 8oo"ed state? 4e idea of te (ooled -tate is to allow a container to maintain a pool of entity beans tat as been created% b!t as not been yet syncronied or assigned to an EJB,bject. 4is mean tat te instances do represent entity beans% b!t tey can be !sed only for serving ome metods $create or findBy)% since tose metods do not relay on te specific val!es of te bean. 'll tese instances are% in fact% e=actly te same% so% tey do not ave meaningf!l state. Jon 4orarinsson as also added: &t can be loo"ed at it tis way: &f no client is !sing an entity bean of a partic!lar type tere is no need for cacig it $te data is persisted in te database). 4erefore% in s!c cases% te container will% after some time% move te entity bean from te eady -tate to te (ooled state to save memory. 4en% to save additional memory% te container may begin moving entity beans from te (ooled -tate to te Does 7ot E=ist -tate% beca!se even to!g te beans cace as been cleared% te bean still ta"es !p some memory j!st being in te (ooled -tate.
/). What is Session Bean? 4e entity bean is !sed to represent data in te database. &t provides an object* oriented interface to data tat wo!ld normally be accessed by te JDB5 or some oter bac"*end '(&. 2ore tan tat% entity beans provide a component model tat allows bean developers to foc!s teir attention on te b!siness logic of te bean% wile te container ta"es care of managing persistence%transactions% and access control. 4ere are two basic "inds of entity beans: container*managed ersistence $52() andbean*managed persistence $B2(). 5ontainer*managed persistence beans are te simplest for te bean developer to create and te most diffic!lt for te EJB server to s!pport. 4is is beca!se all te logic for syncroniing te bean;s state wit te database is andled a!tomatically by te container. 4is means tat te bean developer doesn;t need to write any data access logic% wile te EJB server is s!pposed to ta"e care of all te persistence needs a!tomatically.
&t depends on te transaction attrib!te of te session bean also. >o! ave to set te transaction attrib!te of te session bean eiter to e!ired or e!ires7ew.