RE: Comparison Between Ejb2.0 and Ejb3.0
RE: Comparison Between Ejb2.0 and Ejb3.0
com/panda
1). Removal of home interface enabled simple lookup process in ejb 3.0
4). EJB 3.0 entity beans don’t have home and remote interfaces.
5). EJB 3.0 entity beans/JPA becomes local. Remote annotations are not at all supported for entity
beans.
6). EJB 3.0 beans don’t implement the standard interfaces like javax.ejb.SessionBean and hence no
need to implement the container call back methods like ejbActivate() etc
7). Query is very flexible. Multiple levels of joins are enabled through the refined EJB-QL
9). Security can be provided either through annotations or through deployment descriptors
10). POJO like EJB 3.0 entities become lightweight and easy to convert from a DAO to Entity bean
or vice versa. Since EJB 3 entities don’t need implement any interface.
1). Performance efficiency achieved by the removal of home interface deployment descriptor etc
2). container call back methods like ejbCreate() etc are eliminated. Then what about the
customised implementation possibility of session bean in ejb3.0?
ccvy
3). Is all the transaction attributes in ejb 3.0 are supported in ejb2.1?
SUPPORTS - Don’t start a new transaction but use one if exists REQUIRESNEW – Always start a new
transaction suspending the old one MANDATORY - Require the caller to have started a transaction
NEVER - Forbid the caller to have started a transaction NOTSUPPORTED – Suspend any active
transaction
The
enterprise bean is a component that implements a business
task or business entity and is of two types- entity bean or
a session bean.
EJB vs hibernate
1)No container needed
EJBs are server-side components. EJB are used to develop the distributed transactional and secure
applications based on Java technology.
EJB ia a server-side component architecture that simplifies the process of building enterprise-class
distributed application components in java.
Session bean is encapsulates the business logic. and carry out the task for behalf of the client. It
have two types.one is stateful session bean. this is maintain the convesional state.another one is
stateless session bean. donot maintain the conversional.state.
Stateless stateful
Scalable
Maintain state
<html:select property="address.state">
<html:options collection="STRUTS_EXAMPLE_STATES"
property="value"
labelProperty="label" />
</html:select>
Implicit services are provided to EJb by the Container in which it si deployed according to EJB
contract but in JavaBean we need to code all these services
What is EJB
EJBs are ServerSide Business Components Used to encapsulate business logic without having to
worry about Application Developer
Enterprise Java Bean is a specification for server-side scalable transactional and multi-user
secure enterprise-level applications. It provides a consistant component architecture for creating
distributed n-tier middleware
Session bean is encapsulates the business logic. and carry out the task for behalf of the client. It
have two types.one is stateful session bean. this is maintain the convesional state.another one is
stateless session bean. donot maintain the conversional.state
SFSB SLSB
Not scalable
No pooling
e) client specific data has to be pushed to the bean for each method invocation which result in
increase of network traffic.
Is
EB represent real data that stored in persistence store. Its objectoriented view of
persistencestore
More than that entity beans provide a component model that allows bean developers to focus their
attention on the business logic of the bean while the container takes care of managing
persistence transactions and access control
Session beans are functional beans entity beans are value Beans
sjbStore update the persistent storate with the updated values of the bean while ejbLoad get the
values from the database and set the properties of a bean. Container calls them intermediately to
keep the database consistent.
We need to call explicitly.setRollback() Method to roll back the transaction in this. Container calls
this automatically in case of system exception or system crashes.
< The transaction attribute specifies how the Container must manage transactions for a
method when a client invokes the method via the enterprise bean?s home or component interface or
when the method is invoked as the result of the arrival of a JMS message. (Sun's EJB Specification)
Below is a list of transactional attributes:
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
LoginHome
home=(LoginHome)Context.lookup("java:comp/env/ejb/LoginEJB");
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SessionFacadeEJB</ejb-name>
<home>SessionFacade.SessionFacadeHome</home>
<remote>SessionFacade.SessionFacadeRemote</remote>
<ejb-class>SessionFacade.SessionFacadeBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
<ejb-ref>
<ejb-ref-name>ejb/SavingsEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>savings.SavingsHome</home>
<remote>savings.SavingsRemote</remote>
</ejb-ref>
<ejb-ref>
<ejb-ref-name>ejb/CurrentEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>current.CurrentHome</home>
<remote>current.CurrentRemote</remote>
</ejb-ref>
<ejb-ref>
<ejb-ref-name>ejb/RecurringEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>recurring.RecurringHome</home>
<remote>recurring.RecurringRemote</remote>
</ejb-ref>
<ejb-ref>
<ejb-ref-name>ejb/LoginEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>login.LoginHome</home>
<remote>login.LoginRemote</remote>
</ejb-ref>
<ejb-ref>
<ejb-ref-name>ejb/TransEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>trans.TransHome</home>
<remote>trans.TransRemote</remote>
</ejb-ref>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>SessionFacadeEJB</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
JavaBean :
1. Low-level approach for developing re-usable components for building different Java applications
(Applets stand-alone applications etc.).
2. Java Beans runs in a JVM just like other normal java class
EJB :
1. High-level approach to build Distributed java applications & its an Re-usable server-side
component
JavaBean define a model for creating general-purpose components that are typically used on the
client side. Those components may or may not contain a user interface.
EJBs define a server-side component fro creating highly specialized business logic components.
EJB components don't have GUI.
weblogic-cmp-rdbms-jar.xml
weblogic-ejb-jar.xml
<session>
<ejb-name>SessionFacadeEJB</ejb-name>
<home>SessionFacade.SessionFacadeHome</home>
<remote>SessionFacade.SessionFacadeRemote</remote>
<ejb-class>SessionFacade.SessionFacadeBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
- <ejb-ref>
<ejb-ref-name>ejb/SavingsEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>savings.SavingsHome</home>
<remote>savings.SavingsRemote</remote>
</ejb-ref>
+ <ejb-ref>
+ <ejb-ref>
+ <ejb-ref>
+ <ejb-ref>
</session>
Ejb-jar.xml
- <ejb-jar>
- <enterprise-beans>
- <entity>
<ejb-name>SavingsEJB</ejb-name>
<home>savings.SavingsHome</home>
<remote>savings.SavingsRemote</remote>
<ejb-class>savings.SavingsBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
- <cmp-field>
<field-name>accno</field-name>
</cmp-field>
- <cmp-field>
<field-name>acctype</field-name>
<query>
<query-method>
<method-name>findBigClubs</method-name>
<method-params>
<method-param>int</method-param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
<ejb-ref>
<ejb-ref-name>ejb/SavingsEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>savings.SavingsHome</home>
<remote>savings.SavingsRemote</remote>
</ejb-ref>
<ejb-ref>
<ejb-ref-name>ejb/CurrentEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>current.CurrentHome</home>
<remote>current.CurrentRemote</remote>
</ejb-ref>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Band-Recording</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
Band-Has-Recordings
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<relationship-role-source>
<ejb-name>BandEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>recordings</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
Recording-Has-Band
</ejb-relationship-role-name>
<multiplicity>many</multiplicity>
<relationship-role-source>
<ejb-name>RecordingEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>band</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
Weblogic-cmp-rdbms-jar.sml
weblogic-rdbms-jar>
- <weblogic-rdbms-bean>
<ejb-name>SavingsEJB</ejb-name>
<data-source-name>AdiPool</data-source-name>
- <table-map>
<table-name>savings</table-name>
<- <field-map>
< cmp-field>accno</cmp-field>
<dbms-column>accno</dbms-column>
</field-map>
Transaction
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Converter</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
</assembly-descriptor>
CMP VS BMP
3. application server, Database server independence, but in BMP jdbc specific code is written
4. provides relationship b/w entity beans, instead of manullay wrote spegate code
Complex queries might not be possible with the basic EJBQL for CMPs.So prefer BMPs for complex
queries.
Container will tyr to optimize the SQL code for the CMPs so they may be scalable entity beans than
the BMPs.
Advantages:
1)Easy to develop and maintain.
2)Relationships can be maintained between different entities.
3)Optimization of SQL code will be done.
4)Larger and more performance applications can be done.
Disadvantages:
1)Will not support for some nonJDBC data sources i.e CICS.
2)Complex queries cannot be developed with EJBQL.
BMP::
Advantages:
1)Support for nonJDBC data sources.
2)Complex queries can be build.
Disadvantages:
CMP VS BMP
1)JdbcCode writing
4) For CMP beans you can map only one table at a time. But for BMP u can map more that one
table at a time.
<relationships>
<ejb-relationship-role>
<multiplicity>
< ejb-relationship-role-spurce>
<ejb-name>
</ ejb-relationship-role-spurce>
<cmp-field>
<cmp-field-name>
</cmp-field-type>
</cmp-field>
</ejb-relationship-role>
A design pattern describes a recurring desing problem and a core solution to it.
Select->ejjbSelect
Update->get,set methods
Delete-> remove()\
Insert-> ejbCreate
Objects returned by looking up a JNDI Name are returned by RMI-IIOP protocol. Since this
protocol is language independent objects returned by it should be typecasted using RMI-IIOP
compliant method. Therefore we use PortableRemoteObject.narrow()
Messsagedrivansen bean
<enterprise-beans>
<messaage-driven-destination>
<destination-type>javax.jms.Topic
</message-driven-destination>
<message-drinven>
</message-friv
<?xml version="1.0"?>
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>MusicLibraryEJB</ejb-name>
<home>examples.ejb20.relationships.bands.MusicLibraryHome</home>
<remote>examples.ejb20.relationships.bands.MusicLibrary</remote>
<ejb-class>examples.ejb20.relationships.bands.MusicLibraryBean</ejb-
class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<entity>
<ejb-name>BandEJB</ejb-name>
<local-home>examples.ejb20.relationships.bands.BandHome</local-home>
<local>examples.ejb20.relationships.bands.Band</local>
<ejb-class>examples.ejb20.relationships.bands.BandBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>examples.ejb20.relationships.bands.BandPK</prim-key-
class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>BandEJB</abstract-schema-name>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>founder</field-name>
</cmp-field>
<cmp-field>
<field-name>startDate</field-name>
</cmp-field>
<query>
<query-method>
<method-name>findByName</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
<query>
<query-method>
<method-name>findAll</method-name>
<method-params>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>RecordingEJB</ejb-name>
<local-home>examples.ejb20.relationships.bands.RecordingHome</local-
home>
<local>examples.ejb20.relationships.bands.Recording</local>
<ejb-class>examples.ejb20.relationships.bands.RecordingBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>examples.ejb20.relationships.bands.RecordingPK</prim-
key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>RecordingEJB</abstract-schema-name>
<cmp-field>
<field-name>title</field-name>
</cmp-field>
<cmp-field>
<field-name>bandName</field-name>
</cmp-field>
<cmp-field>
<field-name>bandFounder</field-name>
</cmp-field>
<cmp-field>
<field-name>sales</field-name>
</cmp-field>
<cmp-field>
<field-name>numberSold</field-name>
</cmp-field>
<cmp-field>
<field-name>recordingDate</field-name>
</cmp-field>
<query>
<query-method>
<method-name>findBigRecordings</method-name>
<method-params>
<method-param>long</method-param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
<query>
<query-method>
<method-name>findByTitle</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
<query>
<query-method>
<method-name>findNotByBand</method-name>
<method-params>
<method-param>examples.ejb20.relationships.bands.Band</method-
param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>FanClubEJB</ejb-name>
<local-home>examples.ejb20.relationships.bands.FanClubHome</local-
home>
<local>examples.ejb20.relationships.bands.FanClub</local>
<ejb-class>examples.ejb20.relationships.bands.FanClubBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>examples.ejb20.relationships.bands.FanClubPK</prim-key-
class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>FanClubEJB</abstract-schema-name>
<cmp-field>
<field-name>bandName</field-name>
</cmp-field>
<cmp-field>
<field-name>bandFounder</field-name>
</cmp-field>
<cmp-field>
<field-name>text</field-name>
</cmp-field>
<cmp-field>
<field-name>memberCount</field-name>
</cmp-field>
<query>
<query-method>
<method-name>findBigClubs</method-name>
<method-params>
<method-param>int</method-param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
<query>
<query-method>
<method-name>ejbSelectAfterDateExcludeBand</method-name>
<method-params>
<method-param>java.sql.Date</method-param>
<method-param>examples.ejb20.relationships.bands.Band</method-
param>
</method-params>
</query-method>
<ejb-ql>
</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>ArtistEJB</ejb-name>
<local-home>examples.ejb20.relationships.bands.ArtistHome</local-home>
<local>examples.ejb20.relationships.bands.Artist</local>
<ejb-class>examples.ejb20.relationships.bands.ArtistBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<cmp-field>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Band-Recording</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
Band-Has-Recordings
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<relationship-role-source>
<ejb-name>BandEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>recordings</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
Recording-Has-Band
</ejb-relationship-role-name>
<multiplicity>many</multiplicity>
<relationship-role-source>
<ejb-name>RecordingEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>band</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
<ejb-relation>
<ejb-relation-name>Band-FanClub</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
Band-Has-FanClub
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<relationship-role-source>
<ejb-name>BandEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>fanClub</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
FanClub-Has-Band
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<cascade-delete/>
<relationship-role-source>
<ejb-name>FanClubEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>band</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
<ejb-relation>
<ejb-relation-name>Band-Artist</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
Bands-Have-Artists
</ejb-relationship-role-name>
<multiplicity>many</multiplicity>
<relationship-role-source>
<ejb-name>BandEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>artists</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
Artists-Have-Bands
</ejb-relationship-role-name>
<multiplicity>many</multiplicity>
<relationship-role-source>
<ejb-name>ArtistEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>bands</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MusicLibraryEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>BandEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>FanClubEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>ArtistEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>RecordingEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<ejb-client-jar>ejb20_relationships_bands_client.jar</ejb-client-jar>
</ejb-jar>
http://www.gorecroot.com/Questionpaper/javaejb.aspx
1. What is EJB?
a)tight coupling, client needs to directly interact with business beans. AS client
requirement grows client need to take care of
b) network traffic, too many remote calls if we allow direct interaction with Ejb
business beans
d) Direct dependence means that the client must represent and implement the complex
interactions regarding business object lookups
and creations, and must manage the relationships between the participating business
objects as well as understand the
• Provide a simpler interface to the clients by hiding all the complex interactions
between business components.
• Reduce the number of business objects that are exposed to the client across the service
layer over the network.
• Hide from the client the underlying interactions and interdependencies between
business components. This provides better manageability, centralization of
interactions (responsibility), greater flexibility, and greater ability to cope with
changes.
• Provide a uniform coarse-grained service layer to separate business object
implementation from business service abstraction.
• Avoid exposing the underlying business objects directly to the client to keep tight
coupling between the two tiers to a minimum.
11. Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the
HttpSession from inside an EJB?
12. The EJB container implements the EJBHome and EJBObject classes. For every request from a unique client,
does the container create a separate instance of the generated EJBHome and EJBObject classes?
13. Can the primary key in the entity bean be a Java primitive type such as int?
18. What are the special design cares that must be taken when you work with local interfaces?
20. What is the difference between Message Driven Beans and Stateless Session beans?
22. How can I call one EJB from inside of another EJB?
24. Is it possible for an EJB client to marshal an object of class java.lang.Class to an EJB?
28. What is the difference between session and entity beans? When should I use one or the other?
29. Is there any default cache management system with Entity beans?
32. How can I call one EJB from inside of another EJB?=
37. Are we allowed to change the transaction isolation property in middle of a transaction?
38. For Entity Beans, What happens to an instance field not mapped to any persistent storage, when the bean is
passivated?
39. What is a Message Driven Bean, what functions does a message driven bean have and how do they work in
collaboration with JMS?
40. Does RMI-IIOP support code downloading for Java objects sent by value across an IIOP connection in the same
way as RMI does across a JRMP connection?
41. Does the container create a separate instance of the generated EJBHome and EJBObject classes?
42. What is the advantage of putting an Entity Bean instance from the Ready State to Pooled State?
43. What is the need of Remote and Home interface. Why can’t it be in one?
44. Can I develop an Entity Bean without implementing the create() method in the home interface?
46. Why are ejbActivate() and ejbPassivate() included for stateless session bean even though they are never required
as it is a no conversational bean?
47. Static variables in EJB should not be relied upon as they may break in clusters. Why?
48. If I throw a custom ApplicationException from a business method in Entity bean which is participating in a
transaction, would the transaction be rolled back by container?
53. How to implement an entity bean which the PrimaryKey is an auto numeric field?
54. When defining the Primary Key for the Enterprise Bean, the Deployer using the Container Provider's tools will
typically add additional container-managed fields to the concrete subclass of the entity bean class.
57. If my session bean with single method insert record into 2 entity beans, how can know that the process is done in
same transaction (the attributes for these beans are Required)?
58. When should I adopt BMP and when I should use CMP?
11) How does an enterprise bean that uses container-managed transactions obtain a JTA UserTransaction object?
12) Is it possible for a stateless session bean to employ a JTA UserTransaction object?
15) Is it necessary for an entity bean to protect itself against concurrent access from multiple transactions?
22) While deploying CMP entity beans, which fields in the bean are container-managed and how are they identified?
26) Why are beans not allowed to create their own threads?
29) What is an EJB primary key? How is it implemented when the database doesn't have a primary key?
35) How does Container Managed Persistence work with automatically generated database
36) Let's assume I use a JavaBean as a go-between a JSP and an EJB, and have, say, 50 concurrent clients that need
to access the EJB functionality. Will the JSP container actually instantiate 50 instances of the bean, or can it reuse a
single instance to access the EJB?
37) What happens when two users access an Entity Bean concurrently?
38) What's the reason for having two interfaces -- EJBHome for creating, finding & removing and EJBObject for
implementing business methods. Why not have an single interface which supports both areas of functionality?
41) When should I use bean-managed transactions instead of specifying transaction information in the deployment
descriptor?
44) To complete a transaction, which Transaction Attributes or Isolation Level should be used for a stateless session
bean?
45) Explain the different Transaction Attributes and Isolation Levels with reference to a scenario.
46) What is the most efficient approach for integrating EJB with JSP? Should the EJBs be invoked directly from
within JSP scriptlets? Should the access take place from within Java beans? Or is it best to use custom tags for this
purpose?
47) How do you get a JDBC database registered with a JNDI name so that it can be accessed from an EJB?
48) How to manage fields that can have null values in a container-managed Entity bean?
51) What is the best way of implementing a web application that uses JSP, servlet and EJB technologies all together
following a Model View Controller (MVC) architecture?
52) When does the container call my bean's ejbCreate / ejbPostCreate / ejbStore / ejbPassivate / ejbActivate /
ejbLoad / ejbPassivate method? And what should I do inside it?
53) What's the difference between EJBHome, EJB Home, EJB Object, EJBObject and EJB (not to mention Home
Interface and Remote Interface)?
54) Is there a difference between container managed and bean managed persistence in terms of performance?
55) Given that RMI-IIOP does not support distributed garbage collection (unlike RMI-JRMP), do I need to do
something explicitly to GC beans, or is it magically handled by the EJB framework?
56) OK, so EJB doesn't support user-created threads. So how do I perform tasks asynchronously?
58) Can I deploy a new EJB without restarting my server? (I'm using Weblogic.)
59) How to setup access control in an EJB such that different application clients have different rights to invoke
different methods in one EJB?
61) Can the primary key in the entity bean be a Java primitive type such as int?
62) How do I map a Date/Time field to an Oracle database with CMP?
66) Can I specify specific WHERE clauses for a find method in a CMP Entity Bean?
67) When using a stateful session bean with an idle timeout set, how can the bean receive notification from the
container that it is being removed due to timeout?
68) I have created a remote reference to an EJB in FirstServlet. Can I put the reference in a servlet session and use
that in SecondServlet?
69) What is the difference between a Component Transaction Monitor (CTM) and an Application Server?
70) How can I call one EJB from inside of another EJB?
71) When using Primary Keys, why do I have to implement the hashCode() and equals() method in my bean?
72) Can I deploy two beans in a single jar file? If so, how?
73) Why use EJB when we can do the same thing with servlets?
74) What restrictions are imposed on an EJB? That is, what can't an EJB do?
79) Should I use CMP or BMP for an application with complex data manipulation & relations?
80) For session beans, we can use the SessionSynchronization interface. For entity beans, how do we have control
over a transaction?
2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the
Hashtable isn't.
Fail-safe - “if the Hashtable is structurally modified at any time after the iterator is
created, in any way except through the iterator's own remove method, the iterator will throw a
ConcurrentModificationException†(Runtime exception )
3. HashMap permits null values and only one null key, while Hashtable doesn't allow key or
value as null.
Handling Exceptions
The exceptions thrown by enterprise beans fall into two categories: system and application.
A system exception indicates a problem with the services that support an application.
Examples of these problems include the following: a database connection cannot be obtained,
a SQL insert fails because the database is full, or a lookup method cannot find the desired
object. If your enterprise bean encounters a system-level problem, it should throw a
javax.ejb.EJBException. The container will wrap the EJBException in a
RemoteException, which it passes back to the client. Because the EJBException is a
subclass of the RuntimeException, you do not have to specify it in the throws clause of the
method declaration. If a system exception is thrown, the EJB container might destroy the
bean instance. Therefore, a system exception cannot be handled by the bean's client program;
it requires intervention by a system administrator.
An application exception signals an error in the business logic of an enterprise bean. There
are two types of application exceptions: customized and predefined. A customized exception
is one that you've coded yourself, such as the InsufficentBalanceException thrown by the
debit business method of the SavingsAccountEJB example. The javax.ejb package
includes several predefined exceptions that are designed to handle common problems. For
example, an ejbCreate method should throw a CreateException to indicate an invalid
input parameter. When an enterprise bean throws an application exception, the container does
not wrap it in another exception. The client should be able to handle any application
exception it receives.
If a system exception occurs within a transaction, the EJB container rolls back the
transaction. However, if an application exception is thrown within a transaction, the container
does not roll back the transaction.
Table 5-2 summarizes the exceptions of the javax.ejb package. All of these exceptions are
application exceptions, except for the NoSuchEntityException and the EJBException,
which are system exceptions.
Logging
http://www.ibm.com/developerworks/java/library/j-ejbexcept.html
And while System.out can be redirected, this redirection tends to be fairly limited; System.out
can be redirected to a file or to a pipe (to another program), but you wouldn't be able to
redirect it to a URL, for example; by contrast, it would be very easy to create an appender
that transmits logging messages using HTTP POST.
Trace< DEBUG < INFO < WARN < ERROR < FATAL.
Currently, appenders exist for the console, files, GUI components, remote socket servers,
JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log
asynchronously.
http://logging.apache.org/log4j/1.2/manual.html
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
// Now set its level. Normally you do not need to set the
// level of a logger programmatically. This is usually done
// in configuration files.
logger.setLevel(Level.INFO);
Suppose we are no longer interested in seeing the output of any component belonging to the
com.foo package. The following configuration file shows one possible way of achieving this.
For SingleThreadModel servlets, containers may follow one of the following approaches to ensure that each
sevlet instance is invoked in a separate thread:
Instance Pooling :
in this approach, the container maintains a pool of servlet instances. For each incoming request, the
container allocates a servlet instance from the pool, and upon completion of the service, the container
returns the instance to the pool.
Request Serialization :
in this approach, the container maintains a single instance of the servlet. However since the container
cannot send multiple requests to the instance at the same time, the container serialize the requests. This
means that new requests will be kept waiting while the current request is being served.
a combination of these two approaches is more pragmatic, so that the container could maintain a
reasonable member of instances in the pool, while still serializing requests if the number of requests
exceeds the number of instance in the pool.
CMP::
Advantages:
1)Easy to develop and maintain.
2)Relationships can be maintained between different entities.
3) Optimization of SQL code will be done.
4)Larger and more performance applications can be done.
Disadvantages:
BMP::
Advantages:
1)Support for nonJDBC data sources.
2)Complex queries can be build.
Disadvantages:
Optimistic Locking
Is based on the assumption that it is unlikely that separate users will access the same object
simultaneously. The EJB does not hold a lock for the duration of the transaction just for the small
period of time the EJB interacts with the DB. If db detects a collision the transaction rolls back.
Suitable for large systems requiring significant concurrent access.
Pessimistic locking:
The EJB holds a lock for the duration of the transaction not allowing any other updates to the data.
Suitable for small scale system where concurrent access is
rare.
http://www.1000projects.com/software/java-related/ejb-interview-questions.htm
http://www.1000projects.com/software/java-related/hibernate-interview-questions.htm
Is there a way to get the original exception object from inside a nested or
wrapped Exception (for example an EJBException or RemoteException
Article Number: 21575 | Last Updated: Sat, Dec 26, 2009 at 11:23 AM
Yes.
The way to get, depends on the Exception, since there are no standards. Some
examples:
Entity beans are the only one bean that is reentrant. Neither Session bean
nor Message Driven Bean are reentrant. When Entity bean, we have to declare in the
deployment descriptor whether it is reentrant ( true or false).
Can I develop an Entity Bean without implementing the create() method in the
home interface
Article Number: 21597 | Last Updated: Sat, Dec 26, 2009 at 11:30 AM
Yes, you can create entity bean without impelementing the create() method. The ejbs will be instantiated
by findByPK or any finder methods
http://www.coolinterview.com/type.asp?iType=32
Although for Stateful Session Beans, the container cannot passivate an instance that is
inside a transaction. So using transactions can be a a strategy to control passivation.
What is the lifecycle of Entity Bean
Answers:
The following steps describe the life cycle of an entity bean instance
An entity bean instances life starts when the container creates the instance using
newInstance and then initialises it using setEntityContext.
The instance enters the pool of available instances. Each entity bean has its own pool.
While the instance is in the available pool, the instance is not associated with any
particular entity object identity. Any of these pooled instances may be used to
execute finder (ejbFind) or home (ejbHome) methods.
An instance transitions from the pooled state to the ready state when the container
selects that instance to service a client call to an entity object. There are two possible
transitions from the pooled to the ready state: through the creation of an entity
(ejbCreate and ejbPostCreate) or through the activation of an entity (ejbActivate).
When an entity bean instance is in the ready state, the instance is associated with a
specific entity object identity. While the instance is in the ready state, the container
can synchronize the instance with its representation in the underlying data source
whenever it determines the need to using ejbLoad and ejbStore methods. Business
methods can also be invoked zero or more times on an instance. An ejbSelect method
can be called by a business method, ejbLoad or ejbStore method.
The container can choose to passivate an entity bean instance within a transaction. To
passivate an instance, the container first invokes the ejbStore method to allow the
instance to prepare itself for the synchronization of the database state with the
instance?s state, and then the container invokes the ejbPassivate method to return
the instance to the pooled state.
There are three possible transitions from the ready to the pooled state: through the
ejbPassivate method, through the ejbRemove method (when the entity is removed),
and because of a transaction rollback for ejbCreate, ejbPostCreate,or ejbRemove.
The container can remove an instance in the pool by calling the unsetEntityContext()
method on the instance.
What is the difference between ejbCreate() and ejbPostCreate()
Session and Message Driven Bean will have only ejbCreate() method and no
ejbPostCreate() method. Entity bean will have both ejbCreate() and ejbPostCreate()
methods.
The ejbPostCreate method returns void, and it has the same input parameters as the
ejbCreate method. If we want to set a relationship field to initialize the bean instance,
we should do so in the ejbPostCreate method. we cannot set a
relationship field in the ejbCreate method.
The ejbPostCreate() allows the bean to do any post-create processing before it begins
serving client requests. For every ejbCreate() there must be a matching (matching
arguments) ejbPostCreate() method.
What is the difference between find and select methods in EJB
Answers:
A select method is similar to a finder method for Entity Beans, they both use EJB-QL
to define the semantics of the method.
They differ in that an ejbSelect method(s) are not exposed to the client and the
ejbSelect method(s) can return values that are defined as cmp-types or cmr-types.
What is handle in EJB
Answers:
Two Entity Beans are said to be Identical,if they have the same home inteface and
their primary keys are the same.To test for this ,you must use the component
inteface's isIdentical() method.
ejbCreate() is called before the state of the bean is written to the persistence storage
(database). After this method is completed, a new record (based on the persistence
fields) is created and written. If the Entity EJB is BMP, then this method must contain the
code for writing the new record to the persistence storage.
If you are developing an EJB following 2.0 specs, you can have overloading methods in
the form of ejbCreateXXX(). This will improve the development so you can have
different behaviour for creating a bean, if the parameters differs. The only requirement is
that for each ejbCreateXXX() you need to have corrisponding createXXX() methods in
the home or local interface.
ejbPostCreate() is called after the bean has been written to the database and the bean
data has been assigned to an EJB object, so when the bean is available.
In an CMP Entity EJB, this method is normally used to manage the beans' container-
managed relationship fields.
Life cycle of entity bean works this way. When the client calls the craete
method on entity home interface, Container calls the ejbCreate() and
ejbPostCreate() method and that time Container associate the bean
instance with Entity Object and moved the bean instance from Pooled
State to Ready State. So That time no record is inserted in the database
Only bean instance is associated with Entity Object. After this Container
calls the ejbStore method to synchronize the bean instance with database
so It writes the bean instance to database. This way the container calls
the method in this sequence
ejbCreate()
ejbPostCreate()
ejbStore()