Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives In this lesson, you will learn about:

Similar presentations


Presentation on theme: "Objectives In this lesson, you will learn about:"— Presentation transcript:

1 Objectives In this lesson, you will learn about:
Identify need of EJB transactions Identify different types of EJB transactions Create EJB applications using different types of transactions J2EE Server Components

2 Pre-assessment Questions
Which expression of EJB QL is used to find whether the value of an expression is within a specified range or not? Path BETWEEN IN LIKE J2EE Server Components

3 Pre-assessment Questions (Contd.)
What is the return type of the LIKE expression of EJB QL? boolean int String char Which symbol is used to specify zero or more characters in the LIKE expression of EJB QL query to match the sequence of characters? ? _(underscore) % # J2EE Server Components

4 Pre-assessment Questions (Contd.)
Which symbol is used to write comment in EJB QL? // /* / Cannot write comment in EJB QL. Which BNF symbol specifies the exclusive OR operation between two constructs? ::=: |: *: {..}: J2EE Server Components

5 Solutions to Pre-assessment Questions
b. BETWEEN a. boolean c. % d. Cannot write comment in EJB QL. b. |: J2EE Server Components

6 Overview of EJB Transactions
A transaction: Is a single unit of work that consists of one or more operations that are interconnected. This means that each operation in a transaction must execute successfully in order for the transaction to complete. Is used by an enterprise bean to maintain the accuracy and consistency of the information in a database. Enables multiple clients to access a database without compromising the consistency and integrity of data. J2EE Server Components

7 Overview of EJB Transactions (Contd.)
Importance of Transactions Transactions ensure the regular exchange of consistent data such that all parties involved in the transaction maintain the same information. Enterprise bean applications support flat transactions only. The following figure shows a flat transaction: J2EE Server Components

8 Overview of EJB Transactions (Contd.)
The functions of transactions in real world applications are: Maintain consistency of data in business operations in case of network or system failure. Maintain consistency and integrity of data in business operations executed by multiple users, simultaneously. Control the read or write access to a shared data set. J2EE Server Components

9 Overview of EJB Transactions (Contd.)
ACID Properties: Help to maintain consistency of the data used in transaction operations. ACID properties of a transaction are: Atomicity: Implies that all the operations in a transaction must succeed or fail as a group. Consistency: Ensures that either the transaction completes successfully, leaving the system in an altered state of consistency or the transaction fails, leaving the state in its original state of consistency. Isolation: Allows multiple transactions to read from or write to a database, one at a time. Durability: Ensures that data loss does not occur when a network or a system failure occurs. J2EE Server Components

10 Overview of EJB Transactions (Contd.)
Using Transactions in EJB A transaction can be demarcated: Programmatically: You need to include the code specifying a transaction’s boundaries in the bean. You have to explicitly issue a begin statement and a commit or rollback statement. Declaratively: EJB Container is responsible for starting and ending a transaction. EJB Container issues a begin statement to start a transaction. In programmatic transactions, a transaction ends when all its methods execute a commit or rollback statement. You need to define when a transaction should start, and when it should end. In declarative transactions, EJB container is responsible for starting and ending a transaction. J2EE Server Components

11 Overview of EJB Transactions (Contd.)
Types of Transactions In an enterprise bean application, transactions are managed either by EJB container or by the enterprise bean methods themselves. Depending on the bean management method they deploy, transactions are of two types: Container-managed transaction: Uses transaction attributes to specify the methods that need to be part of a transaction. Bean-managed transactions: Uses Java Database Connectivity (JDBC) API and Java Transaction API (JTA) to implement transactions in an enterprise bean. You can use only one type of transaction for all the methods in an enterprise bean. An enterprise bean uses the deployment descriptor tag, <transaction-type>, to specify the transaction type being used by the bean. J2EE Server Components

12 Overview of EJB Transactions (Contd.)
Transaction Attributes: Specify which methods can be part of a transaction and how to control their execution. The various transaction attributes are: Required: Indicates that the bean method needs to be executed within a transaction. RequiresNew: Specifies that a new transaction must start every time the specified bean method starts execution. Mandatory: Specifies that a bean method must always be part of an existing transaction. Supports: Specifies that it is not mandatory for a bean method to be part of a transaction. NotSupported: Specifies that a bean method should not be part of a transaction. Never: Specifies that a bean method should not be called in the context of an existing transaction. J2EE Server Components

13 Overview of EJB Transactions (Contd.)
Container-Managed Transactions In this type of transactions, EJB Container: Handles all method invocation in a transaction. Starts the transaction as soon as a bean method starts executing. Commits or aborts the transaction just before the bean method completes execution. The transaction attributes in a container-managed transaction define which methods are to be associated with a transaction. The advantage of using a container-managed transaction is that a bean method is free from the complexities of managing a transaction. When an error occurs in container-managed transaction’s method, the transaction is rolled back leaving the database in its original state of consistency. J2EE Server Components

14 Overview of EJB Transactions (Contd.)
Bean-Managed Transactions In a bean-managed transaction, a bean method contains the code to control transactions. You can declare a block of code within a bean method to be part of a transaction. You can only implement one bean-managed transaction at a time. A bean-managed transaction begins with the execution of the UserTransaction.begin() method and ends with the execution of the UserTransaction.commit() method. In a stateful session bean, a bean-managed transaction facilitates maintaining the transactional state across several method calls. You can use a bean-managed transaction to support compensating transactions, which can be rolled back even after it is committed. J2EE Server Components

15 Overview of EJB Transactions (Contd.)
JDBC Transactions JDBC enables you to implement a bean-managed transaction. The DBMS transaction manager controls the JDBC transactions defined in bean methods. The javax.sql.Connection interface declares the transaction methods used in JDBC transactions. A bean method invokes the commit() or rollback() method of the javax.sql.Connection interface to commit or rollback a JDBC transaction. Bean methods using JDBC transactions invoke the setAutoCommit() method of the javax.sql.Connection interface. The setAutoCommit() method notifies the DBMS transaction manager to disable the auto commit property for every SQL statement in bean methods. J2EE Server Components

16 Overview of EJB Transactions (Contd.)
JTA Transactions J2EE transaction manager enables you to use JTA for controlling transaction boundaries in a bean-managed transaction. JTA transactions use the javax.transaction.UserTransaction interface to programmatically control transactions. The getUserTransaction() method returns a reference of the javax.transaction.UserTransaction interface. The methods in the javax.transaction.UserTransaction interface are: begin() commit() rollback() setRollBackOnly() setTransactionTimeout(int) getStatus() J2EE Server Components

17 Overview of EJB Transactions (Contd.)
JTA enables you to declare a business method for performing operations in a transaction. 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. Transactions in a message-driven bean should end before the onMessage() method finishes execution. J2EE Server Components

18 Overview of EJB Transactions (Contd.)
Responsibilities of the Container Provider and the Bean Provider in Transactions EJB container provider and the bean provider are responsible for controlling transactions in an enterprise bean. EJB container performs the following operations if a method in a stateless session bean ends without completing the transaction: Rolls back the transaction. Generates an application error. Throws the exceptions, java.rmi.RemoteException or javax.ejb.EJBException, depending on whether the client is remote or local, respectively. Removes the instance of the session bean that started the transaction and returned before the completion of the transaction. J2EE Server Components

19 Overview of EJB Transactions (Contd.)
EJB Container performs the following operations if the method returns without committing the transaction: Generates an application error. Rolls back the transaction started by the method. Removes the instance of the message-driven bean that started the transaction and returned without its completion. EJB container throws the exception, java.lang.IllegalStateException, if an instance of a bean-managed transaction enterprise bean calls the methods, setRollbackOnly() or getRollbackOnly(). EJB container throws the exception, javax.transaction.NotSupportedException, if an instance of a bean-managed transaction enterprise bean starts a new transaction using the begin() method without ending the previously started transaction. J2EE Server Components

20 Overview of EJB Transactions (Contd.)
Responsibilities of EJB container in a Container-Managed Transaction An instance of a container-managed transaction enterprise bean invokes the EJBContext.setRollbackOnly() method to mark a transaction for rollback. EJB container performs the following tasks if the instance that calls the method, EJBContext.setRollbackOnly(), contains the transaction attributes, Required, RequiresNew, or Mandatory: Ensures that the transaction should not commit and marks the transaction for rollback. Checks if the enterprise bean instance has invoked the setRollbackOnly() method if EJB container starts a transaction before the business method is dispatched to the instance. When the business method completes execution, EJB container rollbacks the transaction and passes the result to the client. J2EE Server Components

21 Overview of EJB Transactions (Contd.)
EJB Container throws the java.lang.IllegalStateException if the method that started the transaction calls the EJBContext.setRollbackOnly() method and has the transaction attributes, Supports, NotSupported, or Never (for message-driven bean, NotSupported only) . The java.lang.IllegalStateException is also thrown if an instance that has the container-managed transaction calls the getUserTransaction() method of the EJBContext interface. J2EE Server Components

22 Overview of EJB Transactions (Contd.)
Responsibilities Of the Bean Provider The bean provider controls the implementation of bean-managed transactions. The bean provider performs the following functions: Defines the type of transaction for a session bean. Specifies the transaction attributes when creating the deployment descriptor. In a container-managed transaction, the application assembler can modify the transaction attributes specified by the bean provider in the deployment descriptor Defines the boundaries of a transaction. Enables one method to start many transactions in a bean-managed transaction. Enables a JTA transaction to invoke a method in a stateful session bean even if the method has closed the connection to the database. J2EE Server Components

23 Demonstration-Implementing JTA Bean-Managed Transactions
Problem Statement Nancy is developing one of the modules in the online banking application that will allow customers to deposit money. The application should use a database to store customer information and the deposited amount. The application also needs to use JTA transaction to ensure data integrity. J2EE Server Components

24 Demonstration-Implementing JTA Bean-Managed Transactions (Contd.)
Solution To solve the preceding problem, perform the following tasks: 1.      Create the stateful session bean home interface. 2.      Create the session bean remote interface. 3.      Create the session bean class. 4.      Create the Web client. 5.      Package the session bean. 6.      Package the Web client. 7.      Deploy the application. Test the application. J2EE Server Components

25 Demonstration-Implementing Container-Managed Transactions in CMP Entity Beans
Problem Statement Chris is developing the module in the online banking application that will allow customers to withdraw funds. Every account needs to maintain a minimum amount of $1000. Chris needs to use a CMP entity bean in this application and ensure that minimum balance in an account is maintained. J2EE Server Components

26 Demonstration-Implementing Container-Managed Transactions in CMP Entity Beans (Contd.)
Solution To solve the preceding problem, perform the following tasks: 1.     Create the CMP entity bean home interface. 2.     Create the CMP entity bean remote interface. Create the CMP entity bean class. Create the Web client. Package the session bean. Package the Web client. Deploy the application. Test the application. J2EE Server Components

27 Summary In this lesson, you learned:
  An enterprise bean uses transactions to maintain consistency of data in a database. All operations in a transaction need to complete successfully in order to make the transaction successful. A transaction needs to exhibit four characteristics: Atomicity, Consistency, Isolation, and Durability. A transaction can either be container-managed or bean-managed. In a bean-managed transaction, the bean contains the code to specify the transaction boundaries. In a container-managed transaction, EJB container is responsible for demarcating transaction boundaries. J2EE Server Components

28 Summary (Contd.)   EJB container uses the Required, RequiresNew, Mandatory, Supports, NotSupported, and Never transaction attributes to specify which methods can be included in a transaction. An advantage of using container-managed transaction is that it frees a bean method from the complexities of managing a transaction. It also helps in reducing the coding time. The execution of the UserTransaction.begin() method marks the start of a bean-managed transaction and the execution of the UserTransaction.commit() method marks the end of a transaction. A bean-managed transaction is implemented using JDBC and JTA. JDBC transactions use methods of the javax.sql.Connection interface to implement a transaction. The methods of javax.transaction.UserTransaction interface are used in JTA transactions to programmatically control transaction boundaries. J2EE Server Components


Download ppt "Objectives In this lesson, you will learn about:"

Similar presentations


Ads by Google