Design Pattern Sample Application - Session Facade
Design Pattern Sample Application -
Session Facade
Date: 14-Dec-2004
Table of Contents
Introduction
Application Overview
Software Requirements
Terminology
Configuring the Application
Deploying and Running the Application
Sample Application Files
TroubleShooting
Additional References
Introduction
Prerequisite
To understand this sample application the user is expected
to have knowledge in the following area,
Technical Overview
In a typical enterprise J2EE application business entities are
represented using Enterprise Java Beans. Application clients need access to
business objects to fulfill their responsibilities and to meet user requirements.
If EJB clients (swing, servlets, jsps etc) access entity beans directly over
the network, the following problems may be observed :
- To carry out a workflow, client objects must make numerous
remote calls to access the EJBs, leading to increased network traffic and
reduced performance.
- Tight coupling makes the client directly dependent on
the implementation of the business objects - meaning when an EJB's interface
changes, client objects must also be updated
- As client requirements increase, the complexity of interaction
between various business objects increases
- Lack of a uniform client access strategy, exposing business
objects to misuse
A session facade solves such problems by presenting client
objects with a unified interface to the underlying EJBs. Client objects interact
only with the facade, which resides on the server and invokes the appropriate
EJB methods. As a result, dependencies and communication between clients and EJBs
is reduced. A session facade can also simplify transaction management: for example,
when a database transaction involves multiple method calls, all could be wrapped
in one method of the facade and the transaction could be monitored at that level.
Following are the list of advantages by using this design
pattern,
- Reduces coupling and interdependencies thereby increases
manageability
- Centralizes and aggregates all business logic that needs
to be exposed to remote clients
- Improves performance by reducing fine-grained method access
and provides coarse-grained access
- Centralizes security management
- Centralizes transaction control
Application Overview
This sample application demonstrates the use of Session Facade
design pattern in a typical HR application. Two local CMP ( Container Managed
Persistence ) EJBs, Employees and Departments are
used to store and retrieve employee and department information from the database.
All method calls to the entity beans are routed through the stateless session
bean, HRSessionEJB, which acts as a facade over these two entity
beans. This effectively reduces the network traffic, rather the client making
many fine-grained method calls on the entity beans to retrieve or store data
in the database.
In this application only the admin user is allowed to add a new department.
In the ejb-jar.xml, the addNewDepartment() method
is configured such that users only from the admin role are given the permission
to access this method. When a user from any other role tries to access this
method, the application throws a RemoteException, informing the
user about the security restrictions. This is an example of using this pattern
to centralize the security management.
In addition, the application allows the user to add a new department and assign
existing employees to the newly added department. As this involves accessing
two different entities, it should take place in one transaction. The addNewDepartment
method in the session bean and create methods in the entity beans are configured
with the 'Required' transaction attribute which makes sure that the data is
committed only in case of a record is added or updated in both the tables successfully.
This is an example of centralized transaction control.
The following class diagram shows the important classes and their relationship
in the Session Facade design pattern. Here the JSPs invoke required methods
on the ClientBean, which uses the HRSessionEJB to retrieve or update data from
the Departments and Employee EJBs.

Software Requirements
List the softwares required for configuring and running this
sample application.
Terminology
| Term |
Definition |
|
<OC4J_HOME>
|
The directory where OC4J is installed. For e.g., D:\oc4j
|
|
<J2EE_HOME>
|
The directory j2ee/home under <OC4J_HOME>.
For e.g., D:\oc4j\j2ee\home
|
| <JAVA_HOME>
|
Directory where JAVA is installed
|
| <ANT_HOME> |
Directory where Apache Ant is
installed |
| <ORACLE_HOME>
|
The directory where Oracle Database
is installed |
Configuring the
Application
- Download, Unzip and extract the SessionFacade.zip
file into your
<SAMPLE_HOME> folder. This creates SessionFacade
folder with all the source files.
- To setup the database connection, edit the data-sources.xml
file under the
<SAMPLE_HOME>\SessionFacade\src\META-INF
directory.
Replace <dbHostName>:<dbPort>:<dbSID> in the url, with your
database values and save the file.
|
dbHostName
|
= |
hostname of the machine
where Oracle database is running
|
|
dbPort
|
= |
port on the host machine
on which the database listener is listening
|
|
dbSID
|
= |
SID of the database
|
Note: This sample application uses HR sample schema in the database.
If the HR account is locked, open the SQL command prompt, login as 'sys' user
and issue the following command to unlock the HR account.
SQL>ALTER USER hr ACCOUNT UNLOCK;
Deploying and Running
the Application
The application can be deployed and run in either of the following
ways:
Run the Application
using JDeveloper 10g
This section describes the steps required
in deploying and running this application inside embedded OC4J using Oracle
JDeveloper 10g.
Run the Application using standalone OC4J
This section describes the steps
required in deploying this application to the Standalone OC4J using ANT Tool
and running using the browser.
Note:
Make sure that the environment variables[<JAVA_HOME>, <ANT_HOME>/bin
in the PATH;, <JAVA_HOME>/bin in the PATH] have been set before proceeding
further. For more information on how to setup these environment variables,
please refer environment set up
readme document.
Sample Application
Files
This section provides a tabular listing of the sample application
files, along with their respective directory locations and description.
| Directory |
File |
Description |
|
SessionFacade\doc
|
Readme.html
|
This file
|
|
SessionFacade\src\oracle\otnsamples\facade
|
*Java
|
Java source files used in this sample application |
|
SessionFacade\src\oracle\otnsamples\facade\ejb
|
*Java |
The EJB source files |
|
SessionFacade\public_html
|
*.jsp
|
JSP files used in this sample application |
|
SessionFacade\public_html\WEB-INF\web.xml
|
web.xml
|
Web deployment descriptor |
|
SessionFacade\config
|
Connection.properties
|
The properties file that holds the database
details |
|
SessionFacade\src\META-INF
|
*.xml
|
Application deployment descriptors |
|
SessionFacade
|
build.xml
|
The ANT build file |
Troubleshooting
When you run the application and insert a record, you may
encounter following database errors.
i) ORA-20205: You may only make changes during normal office hours
ii) ORA-00001: unique constraint (HR.JHIST_EMP_ID_ST_DATE_PK) violated
This is because of the two triggers update_job_history and
secure_employees. Disable these triggers and run the application.
You can issues the following command from the SQL prompt, to disable the trigger
SQL>ALTER TRIGGER update_job_history DISABLE ;
SQL>ALTER TRIGGER secure_employees DISABLE ;
To enable the trigger issue the following command,
SQL>ALTER TRIGGER update_job_history ENABLE ;
SQL>ALTER TRIGGER secure_employees ENABLE ;
Additional References
Please enter your comments about this sample application
here.
|