21 Copyright © 2005, Oracle. All rights reserved. Oracle Application Server 10g Transaction Support.

Slides:



Advertisements
Similar presentations
Using the SQL Access Advisor
Advertisements

1
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Processes and Operating Systems
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
RXQ Customer Enrollment Using a Registration Agent (RA) Process Flow Diagram (Move-In) Customer Supplier Customer authorizes Enrollment ( )
1 Hyades Command Routing Message flow and data translation.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination. Introduction to the Business.
Communicating in J2EE.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
3 Copyright © 2005, Oracle. All rights reserved. Designing J2EE Applications.
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
J2EE Overview.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
13 Copyright © 2005, Oracle. All rights reserved. Monitoring and Improving Performance.
PP Test Review Sections 6-1 to 6-6
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 31 Slide 1 Service-centric Software Engineering.
Sample Service Screenshots Enterprise Cloud Service 11.3.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Transactions Celsina Bignoli Motivations Atomic operations –Group of operation that must all succeed or all fail Network or machine.
Version # Software Engineering Institute Carnegie Mellon University Pittsburgh, PA Sponsored by the U.S. Department of Defense © 1999 by Carnegie.
Enterprise Java Beans - (EJB)
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
Java Transaction API Sean C. Sullivan
1 Transactions Michael Brockway Sajjad Shami CG0165: Advanced Applications Development in Java Northumbria University School of Computing, Engineering.
Preface 1Introduction Course Objectives 1-2 Course Environment 1-4 Course Overview 1-5 About the Course Applications 1-8 Order Entry Schema 1-9 Human Resources.
Copyright © 2002 ProsoftTraining. All rights reserved. Enterprise JavaBeans.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Enterprise Java v121030Java EE Transactions1. Enterprise Java v121030Java EE Transactions2 Goals Understand the basic concepts behind a transaction Be.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Transactions and Security. Contents  Transactions  Transaction Support in EJB  Security  Security Support in EJB.
Objectives In this lesson, you will learn to:
Objectives In this lesson, you will learn about:
Presentation transcript:

21 Copyright © 2005, Oracle. All rights reserved. Oracle Application Server 10g Transaction Support

21-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify the use of bean-managed transactions (BMT) Identify the use of container-managed transactions (CMT) Describe how Oracle Application Server 10g Containers for J2EE (OC4J) supports one-phase and two-phase transaction protocols

21-3 Copyright © 2005, Oracle. All rights reserved. What Is a Transaction? A transaction: Is a single logical unit of work or a set of tasks that are executed together May access one or more shared resources, such as databases Must be atomic, consistent, isolated, and durable (ACID)

21-4 Copyright © 2005, Oracle. All rights reserved. Enterprise JavaBeans (EJB) Support for Transactions The EJB architecture supports declarative and programmatic transactions. The bean provider is not exposed to the complexity of distributed transactions. The EJB container provides a transaction infrastructure. EJBs do not support a nested transaction model.

21-5 Copyright © 2005, Oracle. All rights reserved. EJB Transaction Model Demarcating a transaction determines: –Who begins and ends a transaction –When each of these steps occur A bean-managed (explicit) transaction: –Is demarcated by the bean –Is specified programmatically in the bean through JTA interface or Java Database Connectivity (JDBC) interface A container-managed (declarative) transaction: –Is demarcated by the container –Is specified declaratively (implicit) through the XML deployment descriptor

21-6 Copyright © 2005, Oracle. All rights reserved. Demarcating Transactions Container-managed transactional demarcation: –The element set to container in the deployment descriptor –No transactional management code in the bean –Transaction management depends on value of the element –Available to entity, session, and message-driven beans (MDBs) Bean-managed transactional demarcation: –The element set to bean in the deployment descriptor –Bean implementation must demarcate the begin, commit, or rollback for the transaction –Available to session bean and MDBs, but not entity beans

21-7 Copyright © 2005, Oracle. All rights reserved. Container-Managed Transactions hrApp demos.hrAppHome... Container jdbc/hrCoreDS no description HrApp * RequiresNew...

21-8 Copyright © 2005, Oracle. All rights reserved. CMT: Transaction Attributes The following are the EJB-specified values of transaction attributes: – NotSupported – Required – Supports – RequiresNew – Mandatory – Never The transactional behavior of a bean can be changed with these attributes during deployment time.

21-9 Copyright © 2005, Oracle. All rights reserved. CMT: Transaction Attributes You specify the transaction attributes as follows: Specify for all the methods of a stateful session bean or an entity beans component interface and all direct and indirect superinterfaces of the component interface. Do not specify for: –The methods of the javax.ejb.EJBObject interface and the beans home interface in an stateful session bean –The getEJBHome, getHandle, getPrimaryKey, isIdentical, getEJBMetaData, and getHomeHandle methods in an entity bean

21-10 Copyright © 2005, Oracle. All rights reserved. Transaction Attribute: NotSupported A client has: No transaction: The bean does not start one. A transaction: The bean suspends it. The transaction resumes when the client gains control. Client (bean or servlet) Bean No transactional context Threads of execution Client (bean or servlet) Bean No transactional context Suspended Resumed No transactional context Transactional context

21-11 Copyright © 2005, Oracle. All rights reserved. Threads of execution Transaction Attribute: Required A client has: No transaction: The bean starts a new one. A transaction: The bean uses it. Client transactional context No transactional context Bean New transactional context Client (bean or servlet) Transactional context Bean Threads of execution

21-12 Copyright © 2005, Oracle. All rights reserved. Threads of execution Transaction Attribute: Supports A client has: No transaction: The bean does not start new one. A transaction: The bean uses it. No transactional context Client (bean or servlet) Transactional context Bean Client transactional context Bean Threads of execution

21-13 Copyright © 2005, Oracle. All rights reserved. Transaction Attribute: RequiresNew A client has: No transaction: The bean starts a new one. A transaction: It is suspended, the bean starts a new one and commits it, and then the old one resumes. Bean transactional context No transactional context Client (bean or servlet) Bean Client (bean or servlet) Client Transactional context Bean Bean transactional context Suspended Resumed Threads of execution

21-14 Copyright © 2005, Oracle. All rights reserved. Threads of execution Transaction Attribute: Mandatory A client has: No transaction: The bean requires one. It throws the javax.transaction.TransactionRequiredException exception. A transaction: The bean uses it. No transactional context Client (bean or servlet) Transactional context Client transactional context Bean EXCEPTION THROWN Threads of execution

21-15 Copyright © 2005, Oracle. All rights reserved. Transaction Attribute: Never A client has: No transaction: The container calls the method in an unspecified transaction context. A transaction: The container throws the java.rmi.RemoteException exception. java.rmi.RemoteException Transactional context Client (bean or servlet) Threads of execution Bean

21-16 Copyright © 2005, Oracle. All rights reserved. CMT: The setRollbackOnly() Method The setRollbackOnly() method can control the transaction state in the bean for a CMT. The setRollbackOnly() method marks the current transaction for rollback. If a transaction is marked for rollback, then the container rolls back the transaction before returning to the caller.

21-17 Copyright © 2005, Oracle. All rights reserved. JDeveloper: Setting Transaction Attributes 1.Open the EJB Module Editor for a selected EJB. 2.Select the Container Transactions section.

21-18 Copyright © 2005, Oracle. All rights reserved. JDeveloper: Setting Transaction Attributes 1.Select the Transaction Attribute type from the list. 2.Select methods from the list and associate them with the necessary transaction attributes.

21-19 Copyright © 2005, Oracle. All rights reserved. Java Transaction API (JTA) JTA is used for managing transactions in J2EE. JTA transactions involve: –Enlisting resources: Single-phase commit or two- phase commit –Demarcating transactions: BMT or CMT The JTA package provides an application interface ( UserTransaction ).

21-20 Copyright © 2005, Oracle. All rights reserved. JTA: The UserTransaction Interface Allows applications to explicitly manage transaction boundaries Encapsulates most of the functionality of a transaction manager public interface javax.transaction.UserTransaction{ public abstract void begin (); public abstract void commit (); public abstract int getStatus (); public abstract void rollback (); public abstract void setRollbackOnly (); public abstract void setTransactionTimeout( int secs); }

21-21 Copyright © 2005, Oracle. All rights reserved. Bean-Managed Transactions Demarcation Is indicated by the value Bean for the element in the deployment descriptor Uses the UserTransaction interface of JTA to demarcate and manage the transactions programmatically By using a UserTransaction object, the bean: Initializes a transaction context on the client Invokes the begin(), commit(), or rollback() methods on the current transaction context to manage the transactions

21-22 Copyright © 2005, Oracle. All rights reserved. BMT Demarcation: Process 1.Retrieve the UserTransaction object from the bean code by using a JNDI name. 2.Start a transaction by invoking the begin() method on the UserTransaction object. 3.Execute the business logic to be included in the transaction. 4.End the transaction by invoking the commit() or rollback() methods of the UserTransaction object.

21-23 Copyright © 2005, Oracle. All rights reserved. Using UserTransaction Support in EJBs Code example using BMT: SessionContext ctx; public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } public beanMethodA() { UserTransaction utx = ctx.getUserTransaction(); utx.begin(); do work …… utx.commit(); }

21-24 Copyright © 2005, Oracle. All rights reserved. Client-Demarcated Transactions Using UserTransaction For Web applications (or EJB client) demarcation: Get an InitialContext object Look up java:comp/UserTransaction, and cast to javax.transaction.UserTransaction Context ctx = new InitialContext (); // Retrieve the UserTransaction object. // Use its methods for transaction demarcation UserTransaction ut = (UserTransaction) ictx.lookup("java:comp/UserTransaction"); ut.begin(); //Start the transaction //Look up bean & access logic to perform sql // If everything went well, commit the transaction ut.commit();

21-25 Copyright © 2005, Oracle. All rights reserved. BMT Demarcation: Restrictions Session and message-driven EJBs can have bean- managed transactions if their element is set to Bean. An instance that starts a transaction must complete the transaction before it starts a new transaction. A stateful session bean can commit a transaction before a business method ends. A stateless session bean must commit the transaction before the business method returns.

21-26 Copyright © 2005, Oracle. All rights reserved. Local and Global Transactions A local transaction: –Is started and coordinated internally by the resource manager –Has a single-phase commit A global transaction: –Is controlled by a transaction manager external to the resources involved –Has a two-phase commit

21-27 Copyright © 2005, Oracle. All rights reserved. Single-Phase Commit Configure a data source: –Use the default emulated data source configuration. –Modify the url attribute with the URL of your database. Enlist a resource (database): Retrieve a connection to the data source in the bean after the transaction has begun. –Look up the data source in the JNDI namespace. –Retrieve the connection by using the JTA/JDBC interface.

21-28 Copyright © 2005, Oracle. All rights reserved. Data Source Revisited A data source is an instantiation of an object that implements the javax.sql.DataSource interface. You can use a data source to retrieve a connection to a database server. A data source offers a portable, vendor- independent method for creating JDBC connections. J2EE applications use JNDI to look up DataSource objects. Data sources are defined in data-sources.xml. Data sources can be emulated or nonemulated.

21-29 Copyright © 2005, Oracle. All rights reserved. Default data-sources.xml <data-source class="com.evermind.sql.DriverManagerDataSource" name="OracleDS" location="jdbc/OracleCoreDS" xa-location="jdbc/xa/OracleXADS" ejb-location="jdbc/OracleDS" connection- driver="oracle.jdbc.driver.OracleDriver" username="scott" password="tiger" inactivity-timeout="30" />

21-30 Copyright © 2005, Oracle. All rights reserved. Emulated Versus Nonemulated Data Sources An emulated data source: –Is valid for a single database and local transactions –Is a wrapper around the Oracle data source –Is useful for Oracle and other databases –Has the com.evermind.sql.DriverManagerDataSource class A nonemulated data source: –Is pure Oracle data source implementation –Is needed for two-phase commit and global transactions –Provides XA and JTA global transaction support –Has the com.evermind.sql.OrionCMTDataSource class

21-31 Copyright © 2005, Oracle. All rights reserved. Retrieve Connection to Data Source <data-source class="com.evermind.sql.DriverManagerDataSource" name="hrCourseDS" location="jdbc/CoreDS" xa-location="jdbc/xa/hrCoreXADS" ejb-location="jdbc/hrCoreDS"... /> jdbc/hrCoreDS javax.sql.DataSource Container Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/hrCoreDS"); Connection conn = ds.getConnection(); ejb-jar.xml data-sources.xml Bean Code

21-32 Copyright © 2005, Oracle. All rights reserved. Retrieve Connection to Data Source <data-source... ejb-location="jdbc/hrCoreDS"... /> <resource-ref-mapping name="jdbc/HumanResourcesDS" location="jdbc/hrCoreDS"/> jdbc/HumanResourceDS javax.sql.DataSource Container Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/HumanResourceDS"); data-sources.xml orion-ejb-jar.xml ejb-jar.xml Bean Code

21-33 Copyright © 2005, Oracle. All rights reserved. Global Transaction Resource Request Flow When an EJB requests a JDBC connection or some other transactional resource, it gets associated with the global transaction. Consider an EJB with a container-managed transactions (CMT). Assume that: –The client invokes a bean with the transaction attribute Required –The client is not associated with a global transaction

21-34 Copyright © 2005, Oracle. All rights reserved. Resource Request Flow Transaction manager (Oracle10g DB) Resource adapter OC4J Application (Bean) Client

21-35 Copyright © 2005, Oracle. All rights reserved.

21-36 Copyright © 2005, Oracle. All rights reserved. Enlisting Database Resources The process of including SQL updates in a transaction is called enlisting. JTA automatically enlists databases opened with a DataSource object in a global UserTransaction object. Since JDK 1.2, a DataSource published into a JNDI namespace is the recommended way to make connections. If your global transaction involves more than one database, then you must configure a two-phase commit engine.

21-37 Copyright © 2005, Oracle. All rights reserved.

21-38 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe bean-managed and container-managed transactions Explain how OC4J supports one-phase and two- phase transaction protocol logic

21-39 Copyright © 2005, Oracle. All rights reserved. Practice 21-1: Overview This practice covers the following topics: Deploying a Web application and entity bean with CMT Altering the transaction attributes of the methods in the entity bean, and testing behavior Optionally, creating a session bean to mediate a transaction between the Web application and the entity bean methods

21-40 Copyright © 2005, Oracle. All rights reserved.

21-41 Copyright © 2005, Oracle. All rights reserved.

21-42 Copyright © 2005, Oracle. All rights reserved.