<?xml version="1.0"?>
<!--
  JBoss, Home of Professional Open Source
  Copyright 2006, Red Hat Middleware LLC, and individual contributors
  as indicated by the @author tags.
  See the copyright.txt in the distribution for a full listing
  of individual contributors.
  This copyrighted material is made available to anyone wishing to use,
  modify, copy, or redistribute it subject to the terms and conditions
  of the GNU Lesser General Public License, v. 2.1.
  This program is distributed in the hope that it will be useful, but WITHOUT A
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License,
  v.2.1 along with this distribution; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA  02110-1301, USA.


  (C) 2005-2006,
  @author JBoss Inc.
-->
<page title="JBossTS Trail map: Managing Transaction in EJB" rootpath="../..">
	<header>
        Trail map: Managing Transactions in EJB
    </header>
	<content>
		<navigation>
			<links include="includes/www_arjuna_com_navigation.xml"/>
			<links include="includes/trailmap_navigation.xml"/>
			<links title="Navigation">
				<link ref="index.html">Home</link>
				<link ref="JTA_Overview.html">Previous</link>
			</links>
			<links include="includes/trailmap_additional.xml"/>
		</navigation>
		<area title="Managing Transactions in EJB">
			<html>
				<h3>An Application Server Model - The Enterprise Java Beans</h3>
				<h4>EJB Overview</h4>
Enterprise Java Beans (EJB) is a technology specification from Sun Microsystems
Inc. that specifies a framework for building component-based distributed
applications. As an application server framework, the EJB servers address
transaction processing, resource pooling, security, threading, persistence,
remote access, life cycle etc.
<p>The EJB framework specifies construction, deployment and invocation
of components called as enterprise beans. The EJB specification classifies
enterprise beans into two categories: entity beans and session beans. While
entity beans abstract persistent domain data, session beans provide for
session specific application logic. Both types of beans are maintained
by EJB compliant servers in what are called as containers. A container
provides the run time environment for an enterprise bean. Figure 4 shows
a simplified architecture of transaction management in EJB compliant application
servers.</p>
				<br>
					<img SRC="includes/j2ee_5_ejb_model.GIF"/>
				</br>
				<br>
Figure 4 - EJB and Transactions</br>
				<p>An enterprise bean is specified by two interfaces: the home interface and the
  remote interface. The home interface specifies how a bean can created or found.
  With the help of this interface, a client or another bean can obtain a reference
  to a bean residing in a container on an EJB server. The remote interface specifies
  application specific methods that are relevant to entity or session beans.<br>
  Clients obtain references to home interfaces of enterprise beans via the Java
  Naming and Directory Interface (JNDI) mechanism. An EJB server should provide
  a JNDI implementation for any naming and directory server. Using this reference
  to the home interface, a client can obtain a reference to the remote interface.
  The client can then access methods specified in the remote interface. The EJB
  specification specifies the Java Remote Method Invocation (RMI) as the application
  level protocol for remote method invocation. However, an implementation can
  use IIOP as the wire-level protocol.</br>
					<br>
  In Figure 5, the client first obtains a reference to the home interface, and
  then a reference to an instance of Bean A via the home interface. The same procedure
  is applicable for instance of Bean A to obtain a reference and invoke methods
  on an instance of Bean B.</br>
				</p>
				<h4>EJB Transaction Model</h4>
The EJB framework does not specify any specific transaction service (such
as the JTS) or protocol for transaction management. However, the specification
requires that the javax.transaction.UserTransaction interface of the JTS
be exposed to enterprise beans. This interface is required for programmatic
transaction demarcation as discussed in the next section.
<br>
The EJB framework allows both programmatic and declarative demarcation of transactions.
Declarative demarcation is needed for all enterprise beans deployed on the EJB.
In addition, EJB clients can also initiative and end transactions programmatically.</br>
				<br>
The container performs automatic demarcation depending on the transaction
attributes specified at the time of deploying an enterprise bean in a container.
The following attributes determine how transactions are created.</br>
				<ul>
					<li>
						<b>NotSupported</b>: The container invokes the bean without a global transaction
context.</li>
					<li>
						<b>Required</b>: The container invokes the bean within a global transaction
context. If the invoking thread already has a transaction context associated,
the container invokes the bean in the same context. Otherwise, the container
creates a new transaction and invokes the bean within the transaction context.</li>
					<li>
						<b>Supports</b>: The bean is transaction-ready. If the client invokes the
bean within a transaction, the bean is also invoked within the same transaction.
Otherwise, the bean is invoked without a transaction context.</li>
					<li>
						<b>RequiresNew</b>: The container invokes the bean within a new transaction
irrespective of whether the client is associated with a transaction or
not.</li>
					<li>
						<b>Mandatory</b>: The container must invoke the bean within a transaction.
The caller should always start a transaction before invoking any method
on the bean.</li>
				</ul>
				<h4>Transaction Demarcation</h4>
The EJB framework supports three types of transaction demarcation.
<ul>
					<li>
						<i>Declarative Demarcation</i>: This is also called as <i>container managed
demarcation</i>. The container demarcates transactions on behalf of the
bean. The Required or RequiresNew attribute is specified in a deployment
descriptor at the time of deploying the bean on an EJB server. The bean
can use the javax.ejb.EJBContext.setRollbackOnly() method to mark the transaction
for rollback.</li>
					<li>
						<i>Bean Managed Demarcation</i>: This is similar to the client-managed
demarcation.</li>
					<li>
						<i>Client Managed Demarcation</i>: Java clients can use the javax.transaction.UserTransaction
interface to demarcate transactions programmatically.</li>
				</ul>
				<h4>Resource Enlistment</h4>
Resource enlistment is automatic with EJB. The EJB containers automatically
enlists connections to EJB-aware resource managers whenever a bean obtains
a connection.

<h4>Application Synchronization</h4>
The EJB specification provides the javax.ejb.SessionSynchronization interface
for application synchronization. When implemented by a bean, the container
calls the afterBegin, beforeCompletion and afterCompletion methods for
application synchronization during the two-phase commit process.

			</html>
		</area>
	</content>
	<footer/>
</page>
