SlideShare uma empresa Scribd logo
1 de 53
EJB Technology


Pre-assessment Questions
    1.   Which statement defines a presentation tier?
             a)    Provides transparent access to the persistent store in order to
                   retrieve and store data.
             b) Provides various business services required by an application client
                   in an enterprise application.
             c)    Contains the logic that is required for presenting the information to
                   an end user.
             d) Communicates with the external resources, such as data stores
                   and applications.




 ©NIIT                  Architecting J2EE Solutions           Lesson 2B / Slide 1 of 53
EJB Technology


Pre-assessment Questions
    1.   Select the design pattern that allows you to implement synchronous
         messaging in EJB components.
         a)   Service activator
         b) Value list handler
         c)   Transfer object assembler
         d) Transfer object

    3.   Which option identifies the pattern of integration tier?
         a)  Business delegate
         b) Service locator
         c)  Session facade
         d) Data access object




 ©NIIT                   Architecting J2EE Solutions           Lesson 2B / Slide 2 of 53
EJB Technology


Pre-assessment Questions
    1.   Consider the statements:
          Statement A: The Value list handler pattern accesses the persistent data
         store.
          Statement B: The Transfer object assembler pattern is used to create
         complex transfer objects from various data sources.
         Identify the correct option.
         a)   Statement A is True and statement B is False.
         b) Statement A is False and statement B is True.
         c)   Both, statements A and B, are True.
         d) Both, statements A and B, are false.




 ©NIIT                  Architecting J2EE Solutions         Lesson 2B / Slide 3 of 53
EJB Technology


Pre-assessment Questions
    1.   Which pattern is used to carry multiple data elements in a single structure
         across tiers?
         a)   Transfer object assembler
         b) Transfer object
         c)   Data access object
         d) Service activator




 ©NIIT                  Architecting J2EE Solutions          Lesson 2B / Slide 4 of 53
EJB Technology

Solutions to Pre-assessment Questions

    1.   c) Contains the logic that is required for presenting the information to an end
         user
    2.   b) Value list handler
    3.   d) Data access object
    4.   b) Statement A is false and statement B is true
    5.   b) Transfer object




 ©NIIT                  Architecting J2EE Solutions           Lesson 2B / Slide 5 of 53
EJB Technology


Objectives

    In this lesson you will learn to:


         •   Identify features of EJB container
         •   Identify the various states during the lifecycle of various types of EJBs
         •   Compare stateful and stateless session beans
         •   Compare session and entity beans
         •   Use DAO with entity beans
         •   Identify container managed transactions and bean managed transactions




 ©NIIT                   Architecting J2EE Solutions          Lesson 2B / Slide 6 of 53
EJB Technology


EJB Container Model

    •    EJB container
           • Is a part of the application server in which an enterprise bean is created
             and destroyed.
           • Provides runtime environment for an enterprise bean and pools resources
             such as socket connections and database connections in order to reduce
             memory consumption and processing time.
           • Is also responsible for managing the lifecycle of an enterprise bean.




 ©NIIT                    Architecting J2EE Solutions         Lesson 2B / Slide 7 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Features of EJB Container (Contd.)
           • Passivation
                • Pooling stateless session bean and message driven bean instances is
                  quite easy, as they do not hold conversational state.
                • A stateful session bean’s processing spans to multiple client requests,
                  therefore, you cannot assign a stateful bean instance to another
                  client request that is already servicing a client request.
                • EJB container saves the conversational state of the bean instance into
                  a file.
                • EJB container then sends the bean instance into the pool and assigns
                  the bean instance to another client request.
                • The process of saving the conversational state is called passivation.
                • When the original client request is encountered, the saved
                  conversational state read into a bean instance.
                • Passivation of bean instances releases the resources acquired by the
                  bean.
 ©NIIT                    Architecting J2EE Solutions          Lesson 2B / Slide 8 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Features of EJB Container (Contd.)
           • Security
                • Is important to distributed applications such as railway reservation
                  system, online shopping, and banking applications where an
                  unauthorized access can result in heavy loss of money.
                • Ensures that the clients are authenticated before they are allowed to
                  perform certain operations.
                • Features are defined in the deployment descriptor, which are read by
                  container




 ©NIIT                    Architecting J2EE Solutions         Lesson 2B / Slide 9 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Features of EJB Container (Contd.)
           • Distributed Remote Access
                • Since your enterprise does not know how to receive calls from a
                   client, a client cannot directly invoke methods on the enterprise
                   bean.
                • EJB container intercepts the calls and delegates them to the
                   enterprise bean.
                • This adds great value to EJB applications, as you do not have to care
                   about networking issues.
           • EJB Lifecycle Management
                • EJB container manages the lifecycle of an enterprise bean.
                • An enterprise beans passes through various stages in its lifecycle,
                   such as does not exist, pooled, and ready.
                • EJB container is responsible for switching the state of the enterprise
                   bean between the states.


 ©NIIT                    Architecting J2EE Solutions         Lesson 2B / Slide 10 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs
           • All the beans has three states i.e. does not exist, pooled, and running.
           • An EJB container is responsible for switching a bean between these states.
           • However, the switching of a bean, between these states is entirely bean
              specific.
    •    Lifecycle of stateless session beans
           • The lifecycle of a stateless session bean consists of two stages:
                 • Ready Stage
                 • Does Not Exist Stage
           • When a client uses an EJBHome to obtain a remote interface to a bean,
              the container responds by creating an EJB Object, this is the ready state.
           • Every method invocation on stateless session bean operates
              independently, since it does not maintain a state.




 ©NIIT                    Architecting J2EE Solutions        Lesson 2B / Slide 11 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
           • Lifecycle stages of stateless session beans




 ©NIIT                    Architecting J2EE Solutions      Lesson 2B / Slide 12 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
           • Lifecycle stages of a stateful session bean




 ©NIIT                    Architecting J2EE Solutions      Lesson 2B / Slide 13 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
         • In the lifecycle of a stateful session bean:
              • The client initiates the lifecycle by invoking the create() method.
              •      The EJB container instantiates the bean and then invokes the
                    setSessionContext() and ejbCreate() methods in the session
                    bean.
              • While in the ready state, EJB container may decide to passivate,
                    the bean by moving it from memory to secondary storage.
              • If a client invokes a business method on the bean while it is in the
                    passive stage, the EJB container activates the bean, moving it
                    back to the ready stage.
              • At the end of the lifecycle, the EJB container calls the bean’s
                    ejbRemove() method. The bean instance is ready for garbage
                    collection.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 14 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
         • Lifecycle of entity beans
              • The three stages in the lifecycle of an entity bean are:
                    • Pooled: In this stage, the EJB container creates the instances
                         of an entity bean.
                    • Ready: In this stage, an entity bean is activated.
                    • Does Not Exist: In this stage, an entity bean is
                         uninstantiated.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 15 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
         • Lifecycle of an entity bean




 ©NIIT                 Architecting J2EE Solutions   Lesson 2B / Slide 16 of 53
EJB Technology


EJB Container Model (Contd.)

    •    Lifecycle of EJBs (Contd.)
         • In the lifecycle of entity bean:
              • EJB container creates the instance, and it calls the
                    setEntityContext() method of the entity bean class to set the
                    context of the bean.
              • After instantiation, the entity bean moves to a pool of available
                    instances.
              • There are two ways to move from pooled stage to ready stage:
                    • Client invokes the create() method, causing EJB container to
                         call the ejbCreate() method.
                    • EJB container may call the ejbActivate() method.


 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 17 of 53
EJB Technology


EJB Container Model (Contd.)

   •     Lifecycle of EJBs (Contd.)
              • Business methods are invoked in the ready stage.
              • EJB container invokes ejbPassivate() method in order to move
                    the bean instance to Pooled state.
              • At the end of the lifecycle, EJB container removes the instance
                    from the pool and invokes the unsetEntityContext() method.




 ©NIIT                  Architecting J2EE Solutions      Lesson 2B / Slide 18 of 53
EJB Technology


EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
           • Lifecycle of message driven beans
                • Message-driven beans have a shorter lifespan as compared to the
                   session and entity beans because they remain active only while
                   receiving messages.
                • The lifecycle of a message-driven bean contains two stages, Ready
                   and Does Not Exist.
                • The message-driven beans are similar to stateless session beans in
                   that they do not have a passive stage.




 ©NIIT                   Architecting J2EE Solutions        Lesson 2B / Slide 19 of 53
EJB Technology

EJB Container Model (Contd.)
    •    Lifecycle of EJBs (Contd.)
           • Lifecycle of a message-driven bean




 ©NIIT                   Architecting J2EE Solutions   Lesson 2B / Slide 20 of 53
EJB Technology


EJB Container Model (Contd.)

    •    Lifecycle of EJBs (Contd.)
          • In the lifecycle of message-driven bean:
                 • At the start of its lifecycle, a message-driven bean is in the
                       Ready stage.
                 • In this stage, the message-driven bean instance remains in
                       pool to service the messages sent by clients.
                 • A single instance of a message-driven bean can only process
                       one message at a time.




 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 21 of 53
EJB Technology


EJB Container Model (Contd.)

    •    Lifecycle of EJBs (Contd.)
                 • To add a new message-driven bean instance to the pool, EJB
                       container performs the following steps:
                         •     Call the setMessageDrivenContext() method to pass
                               the context object to a message-driven bean instance.
                         •     Call the ejbCreate() method of the instance to initialize
                               the message-driven bean.
                 • In the message-driven bean lifecycle, the onMessage() method
                       is called whenever a message arrives from the client.
                 • The EJB container invokes the ejbRemove() method to remove
                       a message-driven bean instance from the pool.




 ©NIIT                  Architecting J2EE Solutions          Lesson 2B / Slide 22 of 53
EJB Technology


More on EJBs
    •    Stateful Session Beans Vs Stateless Session Beans
         • Session beans can be used as a façade of entity beans.
         • The following list shows the comparison between stateless and stateful
              session beans:
              • Client state: Stateless session beans cannot retain client state
                   between various client requests. Whereas, a stateful session bean
                   can retain client state between various client requests. Both,
                   stateless and stateful session beans remain in use as long as the
                   client session continues.
              • Pooling: Since stateless session beans do not retain client state, an
                   EJB container takes advantage of this fact and pre-creates the
                   instances of a stateless session beans. A single stateless session
                   bean can process multiple client requests. Stateful session beans
                   retain client state therefore, it is difficult to pool a stateful session
                   bean. The EJB container saves the conversational state in a file
                   and later on retrieves the information from the file.

 ©NIIT                   Architecting J2EE Solutions           Lesson 2B / Slide 23 of 53
EJB Technology


More on EJBs (Contd.)

    •    Stateful Session Beans Vs Stateless Session Beans (Contd.)
         • Lifecycle: Stateless session beans have only two states; does not exist
              and ready. Whereas, a stateful session bean has three states does not
              exist, pooled and ready. An EJB container is responsible for switching a
              bean between various states.
         • Activation and Passivation: An EJB container does not invoke
              ejbActivate() and ejbPassivate() methods on a stateless session
              bean. On the other hand, ejbActivate() and ejbPassivate() methods
              are called by EJB container on a stateful session bean, for saving and
              retrieving conversational state respectively from a file.




 ©NIIT                  Architecting J2EE Solutions        Lesson 2B / Slide 24 of 53
EJB Technology


More on EJBs (Contd.)
    •    Stateful Session Beans Vs Stateless Session Beans (Contd.)
         • ejbCreate(): There is only one ejbCreate() method in stateless
              session bean that does not take any parameter. The reason is, if you
              pass parameters to ejbCreate() method, the parameter values would
              not be available to other methods. Therefore, it is of no use to pass
              parameters to stateless session beans ejbCreate() method. On the
              other hand, in stateful session bean you can declare as many
              ejbCreate() methods as you want. You can pass any number of
              parameters in ejbCreate() method, because the parameter values are
              also available to other methods depending on the scope of the
              parameters.
         • Application: A stateless session can be used where a client session
              spans up to a single method call. For, example verifying a customers
              credit card. You can use a stateful session bean, where a client session
              spans more than a single method call. For example, verifying customers
              bank account number and process other transactions.


 ©NIIT                  Architecting J2EE Solutions        Lesson 2B / Slide 25 of 53
EJB Technology


More on EJBs (Contd.)
    •    Session Beans Vs Entity Beans
         • Session beans are processes that handle client request, whereas entity
              beans contain data information.
         • You can use a session beans to process the compression of a video file.
              Whereas, an entity bean can be used to represent the bank account
              information.
         • The following list shows the comparison between session beans and
              entity beans:
              • Session beans represent a process that performs some business
                   logic for the client. Whereas, an entity bean represents core
                   business data.
              • Entity beans are persistent objects that are stored in a permanent
                   storage such as a database. Whereas, session beans lifetime spans
                   as long as the client’s session is in continuation.



 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 26 of 53
EJB Technology


More on EJBs (Contd.)
    •    Session Beans Vs Entity Beans (Contd.)
         • A session bean is coupled with a particular client while multiple clients
              can share entity beans.
         • Session beans do not survive system failure whereas an entity bean can
              survive system failure.
         • A session bean is created when a client request is encountered and
              destroyed when the client request ends. Whereas entity beans are
              persisted across multiple systems.
         • An entity bean can be found if it already exists, whereas a session
              cannot be found.
         • You can modify an entity bean by directly accessing and updating the
              underlying database. However, you cannot do the same with session
              beans.




 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 27 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         • The various EJB components are session beans, entity beans, and
              message driven beans.
         • Session Beans
              • Session Beans are used whenever you want to write business logic
                    in an enterprise bean.
              • Session beans always hide their identity and remain anonymous
                    unlike entity beans, which expose their identity by using primary
                    keys.
              • A session bean can be used as a client of an entity bean.
              • A session bean shields the complexity of an entity bean and
                    increases the response time of the application by locally accessing
                    the entity bean.
              • In a session-entity façade scenario, the session bean can execute
                    create, read, update, and delete operations on behalf of remote
                    clients.

 ©NIIT                  Architecting J2EE Solutions         Lesson 2B / Slide 28 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Entity Beans
              • Are used when you want to represent a database record in an
                    enterprise bean object.
              • It is always a better approach to wrap a database record in an
                    entity bean object and perform database operations on the object,
                    instead of directly working on the database records.
              • Are deployed in an application server, which provides transaction
                    services to the entity beans, in order to execute database
                    operations in a reliable manner.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 29 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Message Driven Beans
              • Message driven bean are message listeners.
              • A message driven bean asynchronously receives messages from a
                    Messaging server.
              • The supplier of the message can be a JMS application, an
                    enterprise bean, or a standalone application.
              • You can use message driven beans to automatically deliver
                    messages in order to avoid polling of messages.
              • A message driven bean can be used as a trigger, as you can create
                    message selectors in a message driven bean to handle only
                    specific messages.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 30 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Maintaining Client Session State
              • Maintaining client session state refers to the saving of client state
                    between various client requests.
              • In EJB, a stateful session bean is used to maintain client session
                    state.
              • A session bean uses serialization the client’s state in a file.
              • Whenever another client request is encountered, the session bean
                    instance retrieves the state from the file and processes the client
                    request.




 ©NIIT                  Architecting J2EE Solutions         Lesson 2B / Slide 31 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Maintaining Presentation Process
              • A presentation process is a method of handling the data retrieved
                    from a business process and transforming that data in a form that
                    is suitable for rendering at the client end.
              • In J2EE, the presentation logic is handled by servlets and JSPs.
              • In situations, where there is simple presentation logic and Web
                    interface is absent, a session bean can be used for handling
                    presentation process.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 32 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Maintaining Business Process
              • A business process consists of computational business logics. In
                    J2EE, session beans are used to handle business processes.
              • Servlets, JSPs, and entity beans are not suited for performing
                    business processes.
              • Both, stateless and stateful session beans, are able to handle
                    business processes.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 33 of 53
EJB Technology


More on EJBs (Contd.)
    •    Applicability, Maintaining Business Process, and States of EJB Components
         (Contd.)
         • Performing Asynchronous Communication
              • There are situations, when a client does not require a quick action
                    on its request.
              • In such applications, you can use asynchronous messaging to
                    invoke methods on a remote object.
              • Asynchronous messages do not block network traffic by waiting for
                    a response.




 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 34 of 53
EJB Technology


More on EJBs (Contd.)
    •    Using DAO for Entity Beans
         • Data Access Objects (DAO) contain the logic to access a data source,
              such as a database.
         • They are used to separate the business logic from data access logic.
         • The applications that use DAO objects are flexible, as they do not
              directly access the data source for performing regular database
              operations.
         • Advantages of Using DAO with BMP Entity Beans are:
              • Encapsulates the functionality of accessing, inserting, and updating
                   a database.
              • Stores data retrieved by DAO in a value object, which is used to
                   hold values.




 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 35 of 53
EJB Technology


More on EJBs (Contd.)
    •    Using DAO for Entity Beans (Contd.)
         • The various advantages of using DAO with an enterprise component
              are:
              • Making your BMP entity beans independent of database, as all the
                   database access logics reside in the DAO. In future, if there is a
                   change in database, you do not have to rewrite your entity bean
                   code to accommodate the changes.
              • Separating business logic from data logic, thereby increasing the
                   readability and portability of the application.
              • Centralizing the data access logic in a DAO so that it can be used
                   with different enterprise components.
              • Providing easier migration from Bean Managed Persistence to
                   Container Managed Persistence, as data access logic is handled by
                   DAO.



 ©NIIT                  Architecting J2EE Solutions        Lesson 2B / Slide 36 of 53
EJB Technology


More on EJBs (Contd.)
    •    EJB Local and Remote Models
         • With EJB 2.0 specification, Sun has introduced local interfaces that can
              be used instead of remote interfaces.
         • The reason behind introducing local interfaces is that sometimes there
              is no requirement for remotely invoking an enterprise component using
              RMI/RMI-IIOP calls.
         • Applicability of Both Models
              • In EJB, both remote and local models can be used simultaneously.
              • The remote model is used, where enterprise components are
                    placed in different application servers.
              • The remote model enables you to remotely call methods on an
                    enterprise component.
              • The local model can be used in situations where both the
                    enterprise component and their clients are to be placed in the
                    same address space.


 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 37 of 53
EJB Technology


More on EJBs (Contd.)
    •    EJB Local and Remote Models (Contd.)
         • Comparison of Both Models
              • The various advantages of local model are:
                   • Reduces the requirement of marshalling and unmarshalling of
                        data
                   • Reduces the requirement of stub and skeleton objects.
                   • Increases the response time of the application.
              • The various disadvantages of local model are:
                   • They do not allow parameters to be passed by value.
                   • You cannot remotely invoke the methods of an enterprise
                        component that is using the local model.




 ©NIIT                 Architecting J2EE Solutions     Lesson 2B / Slide 38 of 53
EJB Technology


More on EJBs (Contd.)
    •    EJB Local and Remote Models (Contd.)
         • The various advantages of remote model are:
              • Enabling an enterprise component to communicate with remote
                   clients.
              • Enabling you to pass parameters by value.
              • Enabling you to develop clients without the need to know where
                   the bean is located.
         • The various disadvantages of remote model are:
              • It is very slow to create beans through home interface, and calling
                   beans through interface is very slow.
              • It is also necessary to generate stub and skeleton objects.
              • Invoking remote object through stub is comparatively slow and it
                   increases the response time of the application.




 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 39 of 53
EJB Technology


EJB Transaction Management
    •    A transaction is a single unit of work that consists of a sequence of
         operations.
    •    There are two approaches of controlling transactions in EJB
         • Distributed transactions: Spans over a multi-tier deployment, which
              involves various participants. In a distributed transaction, if any
              operation fails even on a single machine, the whole transaction is
              aborted.
         • Local transactions: Are performed within an application server.
    •    A transaction management system is responsible for managing transactions.
    •    The various components of the transaction management system are the
         business logic components, transaction manager, and application server.




 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 40 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Container Managed Transactions
         • EJB container is responsible for handling all method invocations in a
             transaction.
         • EJB container starts a transaction as soon as a bean method starts
             executing. The EJB container commits or aborts the transaction just
             before bean method completes execution.
         • A transaction attribute in a container-managed transaction defines,
             which methods are part of a transaction.
         • Advantage of using a container-managed transaction is that you are not
             required to write the complex transaction management code.
         • You only need to specify transaction attributes in the deployment
             descriptor file, in order to start, commit, or rollback a transaction.
         • You can change the way transactions are handled by the container,
             without accessing the source code.



 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 41 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Container Managed Transactions (Contd.)
         • Transaction Attributes
             • With container-managed transactions, we specify transaction
                   attributes for our enterprise bean in a file.
             • EJB container uses transaction attributes to control transaction
                   processing.
             • Transaction attributes specify, which methods can be part of a
                   transaction and how to control their execution.




 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 42 of 53
EJB Technology


EJB Transaction Management (Contd.)

    •    Container Managed Transactions (Contd.)
         • The various transaction attributes are:
             • Required: Joins an exiting transaction, if any, otherwise creates a
                   new transaction.
             • RequiresNew: Always creates a new transaction for an enterprise
                   bean.
             • Mandatory: Mandates that a transaction must be running before an
                   enterprise bean’s method is invoked.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 43 of 53
EJB Technology


EJB Transaction Management (Contd.)

    •    Container Managed Transactions (Contd.)
         • Supports: Joins an already running transaction. If no transaction is
             running, the container does not start a new transaction before running
             the method.
         • NotSupported: Does not allow a bean to run under a transaction.
             Suspends an already running transaction and resumes it only after
             enterprise bean’s method completes execution.
         • Never: Does not allow a bean to run under a transaction. Throws
             exception, if a transaction is already running.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 44 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Container Managed Transactions (Contd.)
         • Rolling Back a Transaction Using CMT
             • Rolling back a transaction refers to aborting a transaction before
                   completion and undoing all the changes made by successful
                   operations in the transaction.
             • A transaction can be rolled back in two ways.
                   • First, by explicitly calling setRollbackOnly()method.
                   • Second, container will roll back a transaction, whenever an
                        exception is thrown.
             • However, if a bean throws an application exception then the
                   container does not rollback the transaction.
             • In such situation you need to explicitly call the setRollbackOnly()
                   method to rollback the transaction.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 45 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Container Managed Transactions (Contd.)
         • The javax.ejb.SessionSynchronization Interface
             • You can implement the javax.ejb.SessionSynchronization
                   interface only for stateful session beans, which use declarative
                   transactions for demarcating transactional boundaries.
             • Stateful session beans use this interface to rollback a transaction,
                   when the transaction fails.
             • The javax.ejb.SessionSynchronization interface has following
                   methods:
                   • afterBegin(): Indicates that a new transaction has started.
                   • beforeCompletion(): Is invoked before the transaction is
                         committed and after the operations in the current transaction
                         has completed.
                   • afterCompletion(): Indicates that the current transaction
                         has been completed.


 ©NIIT                  Architecting J2EE Solutions         Lesson 2B / Slide 46 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Bean Managed Transactions
         • With bean managed transactions you have to explicitly code transaction
             for your bean. For that you will have to use Java Transaction API (JTA)
             and JDBC.
         • You can use bean-managed transactions in a session bean or message
             driven bean only.
         • Transactions in a message-driven bean should end before the
             onMessage() method finishes execution.
         • Entity beans do not have choice to use bean-managed transactions.
         • In a stateful session bean, a bean-managed transaction can maintain
             the transactional state across several method invocations.
         • A bean-managed transaction can be used to support compensating
             transactions.
         • A compensating transaction is a transaction, which can be rolled back
             even after being committed.


 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 47 of 53
EJB Technology


EJB Transaction Management (Contd.)

    •    Bean Managed Transactions (Contd.)
         • JDBC Transactions
             • JDBC enables you to implement a bean-managed transaction.
             • The JDBC transactions defined in an enterprise bean are controlled
                 by DBMS transaction manager.
             • It implicitly starts a new transaction for the first SQL statement
                 that follows the commit, rollback, or connect statement.
             • The transaction methods used in JDBC transactions are defined in
                 the javax.sql.Connection interface.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 48 of 53
EJB Technology


EJB Transaction Management (Contd.)

    •    Bean Managed Transactions (Contd.)
             • A bean method invokes the commit() or rollback() method of
                 the javax.sql.Connection interface to commit or rollback a JDBC
                 transaction.
             • In JDBC, all the transactions are auto-committed. If at any stage,
                 you want to rollback a transaction, then you have to set the auto-
                 commit property of a transaction to false.
             • You can do that by invoking setAutoCommit(boolean
                 autoCommit) of javax.sql.Connection interface and passing
                 false to the method as a parameter.




 ©NIIT                 Architecting J2EE Solutions       Lesson 2B / Slide 49 of 53
EJB Technology


EJB Transaction Management (Contd.)
    •    Bean Managed Transactions (Contd.)
         • JTA Transactions
             • J2EE transaction manager enables you to use JTA in an enterprise
                 bean or client code, for controlling transaction boundaries
                 programmatically.
             • JTA enables you to start a transaction in a bean code, make other
                 beans part of the transaction, and commit or abort the transaction.
             • JTA contains the javax.transaction.UserTransaction interface
                 that is used to programmatically control transactions.
             • The getUserTransaction() method returns a reference of the
                 javax.transaction.UserTransaction interface. The various
                 methods in the javax.transaction.UserTransaction interface are:
                 • begin(): Starts a new transaction.
                 • commit(): Saves the changes made by a transaction.
                 • rollback(): Undoes the changes made by a transaction.
                 • setRollbackOnly(): Marks the current transaction for roll back.
 ©NIIT                 Architecting J2EE Solutions        Lesson 2B / Slide 50 of 53
EJB Technology


EJB Transaction Management (Contd.)

    •    Bean Managed Transactions (Contd.)
         • You need to adhere to the following rules in implementing the
             javax.transaction.UserTransaction interface:
             • Transactions in an entity bean do not use the
                 javax.transaction.UserTransaction interface.
             • Transactions in a stateless session bean method should end before
                 the method finishes execution. Failure in ending a transaction
                 leads to transaction rollback and the exceptions, RemoteException
                 or EJBException, are thrown.




 ©NIIT                 Architecting J2EE Solutions      Lesson 2B / Slide 51 of 53
EJB Technology


Summary
    In this lesson, you learned:
         • Services provided by an EJB container are bean pooling, security,
               passivation, and bean lifecycle management in order to maximize the
               use of resources.
         • The container calls various methods of an enterprise bean in order to
               service client requests and switch a bean among various states of bean
               lifecycle, such as does not exists, pooled, and running.
         • Stateless session beans do not hold conversational state on behalf of a
               client. Whereas, stateful session bean retain client state between
               various client requests.
         • Session beans are business workflow applications whereas entity beans
               are data objects.




 ©NIIT                  Architecting J2EE Solutions       Lesson 2B / Slide 52 of 53
EJB Technology


Summary (Contd.)

    •    Data Access Objects encapsulates the logic to insert, delete, or update the
         database. The DAO provides a clear separation between data logic and
         business logic. Using DAO you can make your BMP entity bean database
         independent.
    •    Container Managed Transactions are used to declaratively, handle the
         transactional boundaries of a transaction. You declare a bean’s transactional
         requirements in the deployment descriptor file. The container reads the file
         and provides the required transaction services.
    •    Bean Managed Transactions are used to programmatically, handle the
         transactional boundaries of a transaction. You use JDBC and JTA to
         programmatically handle the transactions of a bean.




 ©NIIT                  Architecting J2EE Solutions        Lesson 2B / Slide 53 of 53

Mais conteúdo relacionado

Mais procurados

Vb net xp_10
Vb net xp_10Vb net xp_10
Vb net xp_10Niit Care
 
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...ijma
 
4 - Architetture Software - Architecture Portfolio
4 - Architetture Software - Architecture Portfolio4 - Architetture Software - Architecture Portfolio
4 - Architetture Software - Architecture PortfolioMajong DevJfu
 
8 - Architetture Software - Architecture centric processes
8 - Architetture Software - Architecture centric processes8 - Architetture Software - Architecture centric processes
8 - Architetture Software - Architecture centric processesMajong DevJfu
 
Software Architecture: Design Decisions
Software Architecture: Design DecisionsSoftware Architecture: Design Decisions
Software Architecture: Design DecisionsHenry Muccini
 
2 - Architetture Software - Software architecture
2 - Architetture Software - Software architecture2 - Architetture Software - Software architecture
2 - Architetture Software - Software architectureMajong DevJfu
 
3 f6 9_distributed_systems
3 f6 9_distributed_systems3 f6 9_distributed_systems
3 f6 9_distributed_systemsop205
 
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...MediaEval2012
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programmingkim.mens
 
Eight deadly defects in systems engineering and how to fix them
Eight deadly defects in systems engineering and how to fix themEight deadly defects in systems engineering and how to fix them
Eight deadly defects in systems engineering and how to fix themJoseph KAsser
 
3 f6 8_databases
3 f6 8_databases3 f6 8_databases
3 f6 8_databasesop205
 
Pitfalls of software product development
Pitfalls of software product developmentPitfalls of software product development
Pitfalls of software product developmentSofismo AG
 
Software Patterns
Software PatternsSoftware Patterns
Software Patternskim.mens
 
Chapter 2 slides
Chapter 2 slidesChapter 2 slides
Chapter 2 slideslara_ays
 
Java Optimization For Faster Code & Better Results | J Optimizer
Java Optimization For Faster Code & Better Results | J OptimizerJava Optimization For Faster Code & Better Results | J Optimizer
Java Optimization For Faster Code & Better Results | J OptimizerMichael Findling
 
Cipt1 implementing cisco unified communications ip telephony part 1
Cipt1   implementing cisco unified communications ip telephony part 1Cipt1   implementing cisco unified communications ip telephony part 1
Cipt1 implementing cisco unified communications ip telephony part 1bestip
 

Mais procurados (20)

Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Vb net xp_10
Vb net xp_10Vb net xp_10
Vb net xp_10
 
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...
Subjective Quality Evaluation of H.264 and H.265 Encoded Video Sequences Stre...
 
4 - Architetture Software - Architecture Portfolio
4 - Architetture Software - Architecture Portfolio4 - Architetture Software - Architecture Portfolio
4 - Architetture Software - Architecture Portfolio
 
Mcq m4
Mcq m4Mcq m4
Mcq m4
 
8 - Architetture Software - Architecture centric processes
8 - Architetture Software - Architecture centric processes8 - Architetture Software - Architecture centric processes
8 - Architetture Software - Architecture centric processes
 
Software Architecture: Design Decisions
Software Architecture: Design DecisionsSoftware Architecture: Design Decisions
Software Architecture: Design Decisions
 
2 - Architetture Software - Software architecture
2 - Architetture Software - Software architecture2 - Architetture Software - Software architecture
2 - Architetture Software - Software architecture
 
3 f6 9_distributed_systems
3 f6 9_distributed_systems3 f6 9_distributed_systems
3 f6 9_distributed_systems
 
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...
TUD at MediaEval 2012 genre tagging task: Multi-modality video categorization...
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programming
 
Eight deadly defects in systems engineering and how to fix them
Eight deadly defects in systems engineering and how to fix themEight deadly defects in systems engineering and how to fix them
Eight deadly defects in systems engineering and how to fix them
 
3 f6 8_databases
3 f6 8_databases3 f6 8_databases
3 f6 8_databases
 
Pitfalls of software product development
Pitfalls of software product developmentPitfalls of software product development
Pitfalls of software product development
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Chapter 2 slides
Chapter 2 slidesChapter 2 slides
Chapter 2 slides
 
Java Optimization For Faster Code & Better Results | J Optimizer
Java Optimization For Faster Code & Better Results | J OptimizerJava Optimization For Faster Code & Better Results | J Optimizer
Java Optimization For Faster Code & Better Results | J Optimizer
 
Paper 55 final
Paper 55 finalPaper 55 final
Paper 55 final
 
Cipt1 implementing cisco unified communications ip telephony part 1
Cipt1   implementing cisco unified communications ip telephony part 1Cipt1   implementing cisco unified communications ip telephony part 1
Cipt1 implementing cisco unified communications ip telephony part 1
 

Semelhante a EJB Technology Presentation

Semelhante a EJB Technology Presentation (20)

Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another Introduction
 
Connecting ejb with mule
Connecting ejb with muleConnecting ejb with mule
Connecting ejb with mule
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
 
Ejb training institute in navi-mumbai
Ejb training institute in navi-mumbaiEjb training institute in navi-mumbai
Ejb training institute in navi-mumbai
 
Aravind vinnakota ejb_architecture
Aravind vinnakota ejb_architectureAravind vinnakota ejb_architecture
Aravind vinnakota ejb_architecture
 
Ejb
EjbEjb
Ejb
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Ejbe slides
Ejbe  slidesEjbe  slides
Ejbe slides
 
EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questions
 
Vb.net session 03
Vb.net session 03Vb.net session 03
Vb.net session 03
 
Ejb (1)
Ejb (1)Ejb (1)
Ejb (1)
 
Introcution to EJB
Introcution to EJBIntrocution to EJB
Introcution to EJB
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
Mulesoft torronto meetup_16
Mulesoft torronto meetup_16Mulesoft torronto meetup_16
Mulesoft torronto meetup_16
 
EJB 3.0 and J2EE
EJB 3.0 and J2EEEJB 3.0 and J2EE
EJB 3.0 and J2EE
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 

Mais de Niit Care

Mais de Niit Care (20)

Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 
Dacj 1-3 a
Dacj 1-3 aDacj 1-3 a
Dacj 1-3 a
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 
Dacj 1-2 a
Dacj 1-2 aDacj 1-2 a
Dacj 1-2 a
 
Dacj 1-1 c
Dacj 1-1 cDacj 1-1 c
Dacj 1-1 c
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
Dacj 1-1 a
Dacj 1-1 aDacj 1-1 a
Dacj 1-1 a
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
 
Dacj 2-2 a
Dacj 2-2 aDacj 2-2 a
Dacj 2-2 a
 
Dacj 2-1 c
Dacj 2-1 cDacj 2-1 c
Dacj 2-1 c
 
Dacj 2-1 b
Dacj 2-1 bDacj 2-1 b
Dacj 2-1 b
 

Último

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Último (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

EJB Technology Presentation

  • 1. EJB Technology Pre-assessment Questions 1. Which statement defines a presentation tier? a) Provides transparent access to the persistent store in order to retrieve and store data. b) Provides various business services required by an application client in an enterprise application. c) Contains the logic that is required for presenting the information to an end user. d) Communicates with the external resources, such as data stores and applications. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 1 of 53
  • 2. EJB Technology Pre-assessment Questions 1. Select the design pattern that allows you to implement synchronous messaging in EJB components. a) Service activator b) Value list handler c) Transfer object assembler d) Transfer object 3. Which option identifies the pattern of integration tier? a) Business delegate b) Service locator c) Session facade d) Data access object ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 2 of 53
  • 3. EJB Technology Pre-assessment Questions 1. Consider the statements: Statement A: The Value list handler pattern accesses the persistent data store. Statement B: The Transfer object assembler pattern is used to create complex transfer objects from various data sources. Identify the correct option. a) Statement A is True and statement B is False. b) Statement A is False and statement B is True. c) Both, statements A and B, are True. d) Both, statements A and B, are false. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 3 of 53
  • 4. EJB Technology Pre-assessment Questions 1. Which pattern is used to carry multiple data elements in a single structure across tiers? a) Transfer object assembler b) Transfer object c) Data access object d) Service activator ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 4 of 53
  • 5. EJB Technology Solutions to Pre-assessment Questions 1. c) Contains the logic that is required for presenting the information to an end user 2. b) Value list handler 3. d) Data access object 4. b) Statement A is false and statement B is true 5. b) Transfer object ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 5 of 53
  • 6. EJB Technology Objectives In this lesson you will learn to: • Identify features of EJB container • Identify the various states during the lifecycle of various types of EJBs • Compare stateful and stateless session beans • Compare session and entity beans • Use DAO with entity beans • Identify container managed transactions and bean managed transactions ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 6 of 53
  • 7. EJB Technology EJB Container Model • EJB container • Is a part of the application server in which an enterprise bean is created and destroyed. • Provides runtime environment for an enterprise bean and pools resources such as socket connections and database connections in order to reduce memory consumption and processing time. • Is also responsible for managing the lifecycle of an enterprise bean. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 7 of 53
  • 8. EJB Technology EJB Container Model (Contd.) • Features of EJB Container (Contd.) • Passivation • Pooling stateless session bean and message driven bean instances is quite easy, as they do not hold conversational state. • A stateful session bean’s processing spans to multiple client requests, therefore, you cannot assign a stateful bean instance to another client request that is already servicing a client request. • EJB container saves the conversational state of the bean instance into a file. • EJB container then sends the bean instance into the pool and assigns the bean instance to another client request. • The process of saving the conversational state is called passivation. • When the original client request is encountered, the saved conversational state read into a bean instance. • Passivation of bean instances releases the resources acquired by the bean. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 8 of 53
  • 9. EJB Technology EJB Container Model (Contd.) • Features of EJB Container (Contd.) • Security • Is important to distributed applications such as railway reservation system, online shopping, and banking applications where an unauthorized access can result in heavy loss of money. • Ensures that the clients are authenticated before they are allowed to perform certain operations. • Features are defined in the deployment descriptor, which are read by container ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 9 of 53
  • 10. EJB Technology EJB Container Model (Contd.) • Features of EJB Container (Contd.) • Distributed Remote Access • Since your enterprise does not know how to receive calls from a client, a client cannot directly invoke methods on the enterprise bean. • EJB container intercepts the calls and delegates them to the enterprise bean. • This adds great value to EJB applications, as you do not have to care about networking issues. • EJB Lifecycle Management • EJB container manages the lifecycle of an enterprise bean. • An enterprise beans passes through various stages in its lifecycle, such as does not exist, pooled, and ready. • EJB container is responsible for switching the state of the enterprise bean between the states. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 10 of 53
  • 11. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs • All the beans has three states i.e. does not exist, pooled, and running. • An EJB container is responsible for switching a bean between these states. • However, the switching of a bean, between these states is entirely bean specific. • Lifecycle of stateless session beans • The lifecycle of a stateless session bean consists of two stages: • Ready Stage • Does Not Exist Stage • When a client uses an EJBHome to obtain a remote interface to a bean, the container responds by creating an EJB Object, this is the ready state. • Every method invocation on stateless session bean operates independently, since it does not maintain a state. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 11 of 53
  • 12. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle stages of stateless session beans ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 12 of 53
  • 13. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle stages of a stateful session bean ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 13 of 53
  • 14. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • In the lifecycle of a stateful session bean: • The client initiates the lifecycle by invoking the create() method. • The EJB container instantiates the bean and then invokes the setSessionContext() and ejbCreate() methods in the session bean. • While in the ready state, EJB container may decide to passivate, the bean by moving it from memory to secondary storage. • If a client invokes a business method on the bean while it is in the passive stage, the EJB container activates the bean, moving it back to the ready stage. • At the end of the lifecycle, the EJB container calls the bean’s ejbRemove() method. The bean instance is ready for garbage collection. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 14 of 53
  • 15. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle of entity beans • The three stages in the lifecycle of an entity bean are: • Pooled: In this stage, the EJB container creates the instances of an entity bean. • Ready: In this stage, an entity bean is activated. • Does Not Exist: In this stage, an entity bean is uninstantiated. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 15 of 53
  • 16. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle of an entity bean ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 16 of 53
  • 17. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • In the lifecycle of entity bean: • EJB container creates the instance, and it calls the setEntityContext() method of the entity bean class to set the context of the bean. • After instantiation, the entity bean moves to a pool of available instances. • There are two ways to move from pooled stage to ready stage: • Client invokes the create() method, causing EJB container to call the ejbCreate() method. • EJB container may call the ejbActivate() method. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 17 of 53
  • 18. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Business methods are invoked in the ready stage. • EJB container invokes ejbPassivate() method in order to move the bean instance to Pooled state. • At the end of the lifecycle, EJB container removes the instance from the pool and invokes the unsetEntityContext() method. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 18 of 53
  • 19. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle of message driven beans • Message-driven beans have a shorter lifespan as compared to the session and entity beans because they remain active only while receiving messages. • The lifecycle of a message-driven bean contains two stages, Ready and Does Not Exist. • The message-driven beans are similar to stateless session beans in that they do not have a passive stage. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 19 of 53
  • 20. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • Lifecycle of a message-driven bean ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 20 of 53
  • 21. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • In the lifecycle of message-driven bean: • At the start of its lifecycle, a message-driven bean is in the Ready stage. • In this stage, the message-driven bean instance remains in pool to service the messages sent by clients. • A single instance of a message-driven bean can only process one message at a time. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 21 of 53
  • 22. EJB Technology EJB Container Model (Contd.) • Lifecycle of EJBs (Contd.) • To add a new message-driven bean instance to the pool, EJB container performs the following steps: • Call the setMessageDrivenContext() method to pass the context object to a message-driven bean instance. • Call the ejbCreate() method of the instance to initialize the message-driven bean. • In the message-driven bean lifecycle, the onMessage() method is called whenever a message arrives from the client. • The EJB container invokes the ejbRemove() method to remove a message-driven bean instance from the pool. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 22 of 53
  • 23. EJB Technology More on EJBs • Stateful Session Beans Vs Stateless Session Beans • Session beans can be used as a façade of entity beans. • The following list shows the comparison between stateless and stateful session beans: • Client state: Stateless session beans cannot retain client state between various client requests. Whereas, a stateful session bean can retain client state between various client requests. Both, stateless and stateful session beans remain in use as long as the client session continues. • Pooling: Since stateless session beans do not retain client state, an EJB container takes advantage of this fact and pre-creates the instances of a stateless session beans. A single stateless session bean can process multiple client requests. Stateful session beans retain client state therefore, it is difficult to pool a stateful session bean. The EJB container saves the conversational state in a file and later on retrieves the information from the file. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 23 of 53
  • 24. EJB Technology More on EJBs (Contd.) • Stateful Session Beans Vs Stateless Session Beans (Contd.) • Lifecycle: Stateless session beans have only two states; does not exist and ready. Whereas, a stateful session bean has three states does not exist, pooled and ready. An EJB container is responsible for switching a bean between various states. • Activation and Passivation: An EJB container does not invoke ejbActivate() and ejbPassivate() methods on a stateless session bean. On the other hand, ejbActivate() and ejbPassivate() methods are called by EJB container on a stateful session bean, for saving and retrieving conversational state respectively from a file. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 24 of 53
  • 25. EJB Technology More on EJBs (Contd.) • Stateful Session Beans Vs Stateless Session Beans (Contd.) • ejbCreate(): There is only one ejbCreate() method in stateless session bean that does not take any parameter. The reason is, if you pass parameters to ejbCreate() method, the parameter values would not be available to other methods. Therefore, it is of no use to pass parameters to stateless session beans ejbCreate() method. On the other hand, in stateful session bean you can declare as many ejbCreate() methods as you want. You can pass any number of parameters in ejbCreate() method, because the parameter values are also available to other methods depending on the scope of the parameters. • Application: A stateless session can be used where a client session spans up to a single method call. For, example verifying a customers credit card. You can use a stateful session bean, where a client session spans more than a single method call. For example, verifying customers bank account number and process other transactions. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 25 of 53
  • 26. EJB Technology More on EJBs (Contd.) • Session Beans Vs Entity Beans • Session beans are processes that handle client request, whereas entity beans contain data information. • You can use a session beans to process the compression of a video file. Whereas, an entity bean can be used to represent the bank account information. • The following list shows the comparison between session beans and entity beans: • Session beans represent a process that performs some business logic for the client. Whereas, an entity bean represents core business data. • Entity beans are persistent objects that are stored in a permanent storage such as a database. Whereas, session beans lifetime spans as long as the client’s session is in continuation. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 26 of 53
  • 27. EJB Technology More on EJBs (Contd.) • Session Beans Vs Entity Beans (Contd.) • A session bean is coupled with a particular client while multiple clients can share entity beans. • Session beans do not survive system failure whereas an entity bean can survive system failure. • A session bean is created when a client request is encountered and destroyed when the client request ends. Whereas entity beans are persisted across multiple systems. • An entity bean can be found if it already exists, whereas a session cannot be found. • You can modify an entity bean by directly accessing and updating the underlying database. However, you cannot do the same with session beans. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 27 of 53
  • 28. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components • The various EJB components are session beans, entity beans, and message driven beans. • Session Beans • Session Beans are used whenever you want to write business logic in an enterprise bean. • Session beans always hide their identity and remain anonymous unlike entity beans, which expose their identity by using primary keys. • A session bean can be used as a client of an entity bean. • A session bean shields the complexity of an entity bean and increases the response time of the application by locally accessing the entity bean. • In a session-entity façade scenario, the session bean can execute create, read, update, and delete operations on behalf of remote clients. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 28 of 53
  • 29. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Entity Beans • Are used when you want to represent a database record in an enterprise bean object. • It is always a better approach to wrap a database record in an entity bean object and perform database operations on the object, instead of directly working on the database records. • Are deployed in an application server, which provides transaction services to the entity beans, in order to execute database operations in a reliable manner. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 29 of 53
  • 30. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Message Driven Beans • Message driven bean are message listeners. • A message driven bean asynchronously receives messages from a Messaging server. • The supplier of the message can be a JMS application, an enterprise bean, or a standalone application. • You can use message driven beans to automatically deliver messages in order to avoid polling of messages. • A message driven bean can be used as a trigger, as you can create message selectors in a message driven bean to handle only specific messages. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 30 of 53
  • 31. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Maintaining Client Session State • Maintaining client session state refers to the saving of client state between various client requests. • In EJB, a stateful session bean is used to maintain client session state. • A session bean uses serialization the client’s state in a file. • Whenever another client request is encountered, the session bean instance retrieves the state from the file and processes the client request. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 31 of 53
  • 32. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Maintaining Presentation Process • A presentation process is a method of handling the data retrieved from a business process and transforming that data in a form that is suitable for rendering at the client end. • In J2EE, the presentation logic is handled by servlets and JSPs. • In situations, where there is simple presentation logic and Web interface is absent, a session bean can be used for handling presentation process. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 32 of 53
  • 33. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Maintaining Business Process • A business process consists of computational business logics. In J2EE, session beans are used to handle business processes. • Servlets, JSPs, and entity beans are not suited for performing business processes. • Both, stateless and stateful session beans, are able to handle business processes. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 33 of 53
  • 34. EJB Technology More on EJBs (Contd.) • Applicability, Maintaining Business Process, and States of EJB Components (Contd.) • Performing Asynchronous Communication • There are situations, when a client does not require a quick action on its request. • In such applications, you can use asynchronous messaging to invoke methods on a remote object. • Asynchronous messages do not block network traffic by waiting for a response. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 34 of 53
  • 35. EJB Technology More on EJBs (Contd.) • Using DAO for Entity Beans • Data Access Objects (DAO) contain the logic to access a data source, such as a database. • They are used to separate the business logic from data access logic. • The applications that use DAO objects are flexible, as they do not directly access the data source for performing regular database operations. • Advantages of Using DAO with BMP Entity Beans are: • Encapsulates the functionality of accessing, inserting, and updating a database. • Stores data retrieved by DAO in a value object, which is used to hold values. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 35 of 53
  • 36. EJB Technology More on EJBs (Contd.) • Using DAO for Entity Beans (Contd.) • The various advantages of using DAO with an enterprise component are: • Making your BMP entity beans independent of database, as all the database access logics reside in the DAO. In future, if there is a change in database, you do not have to rewrite your entity bean code to accommodate the changes. • Separating business logic from data logic, thereby increasing the readability and portability of the application. • Centralizing the data access logic in a DAO so that it can be used with different enterprise components. • Providing easier migration from Bean Managed Persistence to Container Managed Persistence, as data access logic is handled by DAO. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 36 of 53
  • 37. EJB Technology More on EJBs (Contd.) • EJB Local and Remote Models • With EJB 2.0 specification, Sun has introduced local interfaces that can be used instead of remote interfaces. • The reason behind introducing local interfaces is that sometimes there is no requirement for remotely invoking an enterprise component using RMI/RMI-IIOP calls. • Applicability of Both Models • In EJB, both remote and local models can be used simultaneously. • The remote model is used, where enterprise components are placed in different application servers. • The remote model enables you to remotely call methods on an enterprise component. • The local model can be used in situations where both the enterprise component and their clients are to be placed in the same address space. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 37 of 53
  • 38. EJB Technology More on EJBs (Contd.) • EJB Local and Remote Models (Contd.) • Comparison of Both Models • The various advantages of local model are: • Reduces the requirement of marshalling and unmarshalling of data • Reduces the requirement of stub and skeleton objects. • Increases the response time of the application. • The various disadvantages of local model are: • They do not allow parameters to be passed by value. • You cannot remotely invoke the methods of an enterprise component that is using the local model. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 38 of 53
  • 39. EJB Technology More on EJBs (Contd.) • EJB Local and Remote Models (Contd.) • The various advantages of remote model are: • Enabling an enterprise component to communicate with remote clients. • Enabling you to pass parameters by value. • Enabling you to develop clients without the need to know where the bean is located. • The various disadvantages of remote model are: • It is very slow to create beans through home interface, and calling beans through interface is very slow. • It is also necessary to generate stub and skeleton objects. • Invoking remote object through stub is comparatively slow and it increases the response time of the application. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 39 of 53
  • 40. EJB Technology EJB Transaction Management • A transaction is a single unit of work that consists of a sequence of operations. • There are two approaches of controlling transactions in EJB • Distributed transactions: Spans over a multi-tier deployment, which involves various participants. In a distributed transaction, if any operation fails even on a single machine, the whole transaction is aborted. • Local transactions: Are performed within an application server. • A transaction management system is responsible for managing transactions. • The various components of the transaction management system are the business logic components, transaction manager, and application server. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 40 of 53
  • 41. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions • EJB container is responsible for handling all method invocations in a transaction. • EJB container starts a transaction as soon as a bean method starts executing. The EJB container commits or aborts the transaction just before bean method completes execution. • A transaction attribute in a container-managed transaction defines, which methods are part of a transaction. • Advantage of using a container-managed transaction is that you are not required to write the complex transaction management code. • You only need to specify transaction attributes in the deployment descriptor file, in order to start, commit, or rollback a transaction. • You can change the way transactions are handled by the container, without accessing the source code. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 41 of 53
  • 42. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions (Contd.) • Transaction Attributes • With container-managed transactions, we specify transaction attributes for our enterprise bean in a file. • EJB container uses transaction attributes to control transaction processing. • Transaction attributes specify, which methods can be part of a transaction and how to control their execution. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 42 of 53
  • 43. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions (Contd.) • The various transaction attributes are: • Required: Joins an exiting transaction, if any, otherwise creates a new transaction. • RequiresNew: Always creates a new transaction for an enterprise bean. • Mandatory: Mandates that a transaction must be running before an enterprise bean’s method is invoked. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 43 of 53
  • 44. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions (Contd.) • Supports: Joins an already running transaction. If no transaction is running, the container does not start a new transaction before running the method. • NotSupported: Does not allow a bean to run under a transaction. Suspends an already running transaction and resumes it only after enterprise bean’s method completes execution. • Never: Does not allow a bean to run under a transaction. Throws exception, if a transaction is already running. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 44 of 53
  • 45. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions (Contd.) • Rolling Back a Transaction Using CMT • Rolling back a transaction refers to aborting a transaction before completion and undoing all the changes made by successful operations in the transaction. • A transaction can be rolled back in two ways. • First, by explicitly calling setRollbackOnly()method. • Second, container will roll back a transaction, whenever an exception is thrown. • However, if a bean throws an application exception then the container does not rollback the transaction. • In such situation you need to explicitly call the setRollbackOnly() method to rollback the transaction. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 45 of 53
  • 46. EJB Technology EJB Transaction Management (Contd.) • Container Managed Transactions (Contd.) • The javax.ejb.SessionSynchronization Interface • You can implement the javax.ejb.SessionSynchronization interface only for stateful session beans, which use declarative transactions for demarcating transactional boundaries. • Stateful session beans use this interface to rollback a transaction, when the transaction fails. • The javax.ejb.SessionSynchronization interface has following methods: • afterBegin(): Indicates that a new transaction has started. • beforeCompletion(): Is invoked before the transaction is committed and after the operations in the current transaction has completed. • afterCompletion(): Indicates that the current transaction has been completed. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 46 of 53
  • 47. EJB Technology EJB Transaction Management (Contd.) • Bean Managed Transactions • With bean managed transactions you have to explicitly code transaction for your bean. For that you will have to use Java Transaction API (JTA) and JDBC. • You can use bean-managed transactions in a session bean or message driven bean only. • Transactions in a message-driven bean should end before the onMessage() method finishes execution. • Entity beans do not have choice to use bean-managed transactions. • In a stateful session bean, a bean-managed transaction can maintain the transactional state across several method invocations. • A bean-managed transaction can be used to support compensating transactions. • A compensating transaction is a transaction, which can be rolled back even after being committed. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 47 of 53
  • 48. EJB Technology EJB Transaction Management (Contd.) • Bean Managed Transactions (Contd.) • JDBC Transactions • JDBC enables you to implement a bean-managed transaction. • The JDBC transactions defined in an enterprise bean are controlled by DBMS transaction manager. • It implicitly starts a new transaction for the first SQL statement that follows the commit, rollback, or connect statement. • The transaction methods used in JDBC transactions are defined in the javax.sql.Connection interface. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 48 of 53
  • 49. EJB Technology EJB Transaction Management (Contd.) • Bean Managed Transactions (Contd.) • A bean method invokes the commit() or rollback() method of the javax.sql.Connection interface to commit or rollback a JDBC transaction. • In JDBC, all the transactions are auto-committed. If at any stage, you want to rollback a transaction, then you have to set the auto- commit property of a transaction to false. • You can do that by invoking setAutoCommit(boolean autoCommit) of javax.sql.Connection interface and passing false to the method as a parameter. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 49 of 53
  • 50. EJB Technology EJB Transaction Management (Contd.) • Bean Managed Transactions (Contd.) • JTA Transactions • J2EE transaction manager enables you to use JTA in an enterprise bean or client code, for controlling transaction boundaries programmatically. • JTA enables you to start a transaction in a bean code, make other beans part of the transaction, and commit or abort the transaction. • JTA contains the javax.transaction.UserTransaction interface that is used to programmatically control transactions. • The getUserTransaction() method returns a reference of the javax.transaction.UserTransaction interface. The various methods in the javax.transaction.UserTransaction interface are: • begin(): Starts a new transaction. • commit(): Saves the changes made by a transaction. • rollback(): Undoes the changes made by a transaction. • setRollbackOnly(): Marks the current transaction for roll back. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 50 of 53
  • 51. EJB Technology EJB Transaction Management (Contd.) • Bean Managed Transactions (Contd.) • You need to adhere to the following rules in implementing the javax.transaction.UserTransaction interface: • Transactions in an entity bean do not use the javax.transaction.UserTransaction interface. • Transactions in a stateless session bean method should end before the method finishes execution. Failure in ending a transaction leads to transaction rollback and the exceptions, RemoteException or EJBException, are thrown. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 51 of 53
  • 52. EJB Technology Summary In this lesson, you learned: • Services provided by an EJB container are bean pooling, security, passivation, and bean lifecycle management in order to maximize the use of resources. • The container calls various methods of an enterprise bean in order to service client requests and switch a bean among various states of bean lifecycle, such as does not exists, pooled, and running. • Stateless session beans do not hold conversational state on behalf of a client. Whereas, stateful session bean retain client state between various client requests. • Session beans are business workflow applications whereas entity beans are data objects. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 52 of 53
  • 53. EJB Technology Summary (Contd.) • Data Access Objects encapsulates the logic to insert, delete, or update the database. The DAO provides a clear separation between data logic and business logic. Using DAO you can make your BMP entity bean database independent. • Container Managed Transactions are used to declaratively, handle the transactional boundaries of a transaction. You declare a bean’s transactional requirements in the deployment descriptor file. The container reads the file and provides the required transaction services. • Bean Managed Transactions are used to programmatically, handle the transactional boundaries of a transaction. You use JDBC and JTA to programmatically handle the transactions of a bean. ©NIIT Architecting J2EE Solutions Lesson 2B / Slide 53 of 53