You are on page 1of 6

Assignment - 4

Q.1. What is EJB (Enterprise Java Bean)? Explore the EJB life cycle. What exactly is
an EJB container?
Explain the uses of EJB as well as its benefits and drawbacks

A.1.
Enterprise JavaBeans (EJB) is a server-side component architecture for building
distributed, scalable, and transactional enterprise applications using Java. EJB
provides a set of specifications that define a standardized way to develop reusable
and interoperable business components.
The life cycle of an EJB involves several stages:

1. Creation: When an EJB instance is created, it goes through initialization and


dependency injection. The container manages the creation process, ensuring
that all required resources are available.

2. Activation: Once the EJB instance is created, it becomes active and ready to
process requests from clients.

3. Passivation: When the EJB instance is idle or the container needs to reclaim
resources, it may be passivated. In this state, the instance's state is stored and
can be activated again when needed.

4. Removal: At some point, an EJB instance may be removed from the container,
typically due to explicit removal by the client or container shutdown.

The EJB container plays a crucial role in managing the execution and life cycle of
EJB components. It provides services such as transaction management, security,
concurrency control, and resource pooling. The container is responsible for creating
and destroying EJB instances, handling concurrency, managing transactions, and
providing access to external resources like databases or messaging systems. It
shields the developer from low-level concerns, allowing them to focus on business
logic implementation.
The uses of EJB include:

1. Business Logic: EJB components are well-suited for implementing complex


business logic, such as workflows, business rules, and calculations.

2. Distributed Applications: EJB facilitates the development of distributed


applications by providing mechanisms for remote method invocations, allowing

Assignment - 4 1
components to be deployed on different machines.

3. Scalability: EJB components can be deployed in a clustered environment,


enabling horizontal scalability to handle increased load and provide high
availability.

4. Transaction Management: EJB supports declarative transaction management,


ensuring ACID (Atomicity, Consistency, Isolation, Durability) properties for critical
operations.

5. Security: EJB provides security mechanisms like authentication and


authorization, allowing developers to enforce access control policies.

Benefits of EJB:

1. Reusability: EJB components can be developed as reusable building blocks,


promoting code reuse and modular design.

2. Standardization: EJB is a standard Java EE specification, ensuring


interoperability and compatibility across different application servers.

3. Scalability and Performance: EJB's container-managed services handle


scalability and resource pooling, optimizing performance and allowing
applications to handle high loads.

4. Transactional Support: EJB provides built-in transaction management,


simplifying the development of robust and consistent applications.

Drawbacks of EJB:

1. Complexity: EJB has historically been criticized for its complexity, steep learning
curve, and verbose APIs, although newer versions have addressed some of
these concerns.

2. Performance Overhead: The EJB container adds an additional layer of


abstraction, which may introduce some performance overhead compared to
simpler frameworks.

3. Deployment and Configuration: Deploying and configuring EJB components and


their associated resources can be more involved compared to lightweight
frameworks.

Q.3. Explain type of beans


a.Session Bean

Assignment - 4 2
b.Entity Bean
c.Message Driven Bean
A.3.

a. Session Bean:
A session bean represents a single client's interaction with the EJB container. It
encapsulates business logic and represents a transient conversation or session
between a client and the server. Session beans can be of two types:

i. Stateful Session Bean (SFSB): A stateful session bean maintains conversational


state with the client across multiple method invocations. It preserves client-specific
data for the duration of a session or until explicitly removed. SFSBs are useful when
maintaining client-specific contexts or managing shopping carts.

ii. Stateless Session Bean (SLSB): A stateless session bean doesn't preserve
conversational state between method invocations. It is designed to be stateless,
allowing multiple clients to access the bean concurrently. SLSBs are commonly used
to encapsulate business logic and perform operations that don't require maintaining
client-specific state.

b. Entity Bean:
Entity beans represent persistent data in a database and provide an object-relational
mapping. However, starting from EJB 3.0, the entity bean model has been
deprecated, and the Java Persistence API (JPA) is now the preferred approach for
dealing with persistence. In earlier versions, there were two types of entity beans:

i. Container-Managed Persistence (CMP) Entity Bean: The container manages the


persistence and mapping of CMP entity beans to the database. Developers define
the entity's attributes and relationships, and the container takes care of the
underlying database operations.

ii. Bean-Managed Persistence (BMP) Entity Bean: Developers are responsible for
managing the persistence and mapping of BMP entity beans. They explicitly define
the database operations and queries required for managing the entity's state.

c. Message-Driven Bean (MDB):


A message-driven bean is an EJB component designed to consume messages
asynchronously from a messaging system, such as Java Message Service (JMS).
MDBs are event-driven and react to messages received on specific destinations.
They are typically used for implementing asynchronous processing, event-driven
architectures, or integrating with message-oriented middleware.

Assignment - 4 3
Q.5. What is Java Database Connectivity (JDBC)? Explain all four types of JDBC
driver connectivity and provide a comparative table.

A.5.
Java Database Connectivity (JDBC) is a Java API that provides a standard way for
Java applications to interact with databases. It enables developers to execute SQL
statements, retrieve and manipulate data, and manage database connections.

JDBC drivers are responsible for establishing the connection between Java
applications and databases. There are four types of JDBC drivers, each with its own
connectivity mechanism:

1. Type 1: JDBC-ODBC Bridge Driver:

Description: This driver uses the Open Database Connectivity (ODBC) API
to connect to databases. It translates JDBC calls into ODBC calls, allowing
Java applications to interact with databases that support ODBC.

Advantages: Easy to install and configure, as it relies on the existing ODBC


drivers. Can be used to connect to any database with an ODBC driver
available.

Disadvantages: Performance can be slower due to the additional layer of


translation. Requires ODBC driver installation and configuration.

2. Type 2: Native API Partly Java Driver:

Description: This driver uses a database-specific native client library to


connect to the database. It communicates with the database using the
vendor's proprietary network protocol.

Advantages: Improved performance compared to the Type 1 driver. Direct


communication with the database using native APIs.

Disadvantages: Not platform-independent, as it relies on the native client


library. Requires separate installation and configuration for each supported
database.

3. Type 3: Network Protocol Driver:

Description: This driver communicates with a middleware server or gateway


through a network protocol, which then translates the requests into
database-specific calls.

Advantages: Platform-independent, as the client communicates with a


middleware server. Simplifies deployment and maintenance as the

Assignment - 4 4
middleware handles the translation.

Disadvantages: Additional overhead due to the translation layer in the


middleware. Requires installation and configuration of the middleware
server.

4. Type 4: Thin Driver (Pure Java Driver):

Description: This driver is a pure Java implementation that communicates


directly with the database using the database's native protocol. It doesn't rely
on additional native libraries.

Advantages: Platform-independent, as it is entirely written in Java. Good


performance due to direct communication with the database. No external
dependencies or additional installations.

Disadvantages: May require a separate driver for each database vendor.


Less common databases may have limited or no Type 4 drivers available.

Q.7. What do you mean by Transaction Management in JDBC?


A.7.

Transaction management in JDBC refers to the ability to group multiple database


operations into a single logical unit called a transaction. A transaction ensures
that all the operations within it either succeed and commit their changes to the
database or fail and rollback the changes, ensuring data consistency and
integrity.

JDBC provides transaction management capabilities through the following


methods:

1. Beginning a Transaction: The setAutoCommit(false) method is used to disable


the default auto-commit behavior of JDBC. By calling this method, you
inform the database that you want to start a transaction explicitly.

2. Committing a Transaction: The commit() method is used to commit the


changes made during a transaction. When this method is called, all the
changes made in the transaction are permanently saved to the database.

3. Rolling Back a Transaction: The rollback() method is used to roll back the
changes made during a transaction. If an error occurs or if you decide to
discard the changes made within the transaction, calling this method will
revert the database to its previous state.

Assignment - 4 5
By using transaction management in JDBC, you can ensure the following:

Atomicity: Transactions are treated as a single unit of work. All the


operations within a transaction are either committed together or rolled back
together. If any operation fails, the entire transaction is rolled back, and no
changes are persisted.

Consistency: Transactions enforce data consistency rules. Database


constraints and validations are applied during the transaction, ensuring that
the data remains in a valid and consistent state.

Isolation: Transactions provide isolation between concurrent operations.


When multiple transactions are executed concurrently, each transaction
sees a consistent snapshot of the data, preventing interference between
transactions.

Durability: Once a transaction is committed, the changes made within it are


permanently stored in the database and can survive system failures or
restarts.

Assignment - 4 6

You might also like