21 copyright © 2005, oracle. all rights reserved. oracle application server 10g transaction support
TRANSCRIPT
21Copyright © 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) Supportfor 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 <transaction-type> element set to container
in the deployment descriptor– No transactional management code in the bean– Transaction management depends on value of the
<trans-attribute> element – Available to entity, session, and message-driven
beans (MDBs)• Bean-managed transactional demarcation:
– The <transaction-type> 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
<session> <ejb-name>hrApp</ejb-name> <home>demos.hrAppHome</home> ... <transaction-type>Container</transaction-type> <resource-ref> <res-ref-name>jdbc/hrCoreDS</res-ref-name> ...</session><assembly-descriptor> ...<container-transaction> <description>no description</description> <method> <ejb-name>HrApp</ejb-name> <method-name>*</method-name> </method> <trans-attribute>RequiresNew</trans-attribute></container-transaction></assembly-descriptor> ...
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 bean’s 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 bean’s 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 ofexecution
Client (bean or servlet)
Bean
No transactional context
Suspended
Resumed
No transactional context
Transactional context
21-11 Copyright © 2005, Oracle. All rights reserved.
Threads ofexecution
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)
Client (bean or servlet)
Transactional context
Bean
Threads ofexecution
21-12 Copyright © 2005, Oracle. All rights reserved.
Threads ofexecution
Transaction Attribute: Supports
A client has:
• No transaction: The bean does not start new one.
• A transaction: The bean uses it.
No transactional contextNo transactional context
Client (bean or servlet)
Client (bean or servlet)
Transactional context
Bean
Client transactional context
Bean
Threads ofexecution
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 contextNo transactional context
Client (bean or servlet)
Bean
Client (bean or servlet)
Client Transactional context
Bean
Bean transactional context
Suspended
Resumed
Threads ofexecution
21-14 Copyright © 2005, Oracle. All rights reserved.
Threads ofexecution
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)
Client (bean or servlet)
Transactional context Client transactional context
Bean
Bean
EXCEPTION THROWN
Threads ofexecution
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.RemoteExceptionTransactional context
Client (bean or servlet)
Threads ofexecution
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 <transaction-type> 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 transactionut.commit();
21-25 Copyright © 2005, Oracle. All rights reserved.
BMT Demarcation: Restrictions
• Session and message-driven EJBs can have bean-managed transactions if their <transaction-type> 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"
url="jdbc:oracle:thin:@localhost:5521:oracle"
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" .../><resource-ref><res-ref-name>jdbc/hrCoreDS</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth>
</resource-ref>
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"/>
<resource-ref>
<res-ref-name>jdbc/HumanResourceDS </res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth>
</resource-ref> 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)
Client1
2
3
4
5
6
7
8 9
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-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