Title: JDBC in J2EE
1JDBC in J2EE
- Connection to Cloudscape database using JDBC
- J2EE Platform services and architecture
- Enterprise JavaBeans (EJB)
- Session Beans vs. Entity Beans
- EJB access to databases using JDBC
- Database connection
- Persistence management (Entity Bean e.g.)
- Transaction management (Session Bean e.g.)
2J2EE Services
- HTTP - enables Web browsers to access servlets
and JavaServer PagesTM (JSP) files - EJB - allows clients to invoke methods on
enterprise beans - Authentication - enforces security by requiring
users to log in - Naming and Directory - allows programs to locate
services and components through the Java Naming
and Directory InterfaceTM (JNDI) API
3J2EE Architecture
Ref. JavaTM 2 Enterprise Edition Developer's
Guide, Figure 1-2
4Enterprise JavaBeans (EJB)
- Server-side Java components
- Contain the business logic of enterprise
application - Support database access
- Transactional
- Multi-user secure
- Managed by the EJB container
- Prohibited from a set of operations
5Session Bean vs. Entity Bean
Ref. JavaTM 2 Enterprise Edition Developer's
Guide, Table 1-1
6EJB Access to Databases Using JDBC API
J2EE uses JDBC 2.0 (java.sql) and JDBC 2.0
Optional package (javax.sql)
- Making a connection to database
- Should not hardcode the actual name (URL) of the
database in EJB - Should refer to the database with a logical name
- Use a JNDI lookup when obtaining the database
connection. - Users and password not needed for the Cloudscape
bundled with J2EE
- Driver and Data source properties
- set in J2EE default.properties file
- jdbc.driversCOM.cloudscape.core.RmiJdbcDriver
- jdbc.datasourcesjdbc/Cloudscapejdbccloudscaper
miCloudscapeDB createtrue
7Making a connection to database example
1. Specify the logical database name. private
String dbName "javacomp/env/jdbc/AccountDB" 2
. Obtain the DataSource associated with the
logical name. InitialContext ic new
InitialContext() DataSource ds
(DataSource) ic.lookup(dbName) 3. Get the
Connection from the DataSource. Connection
con ds.getConnection()
8Specifying JNDI name for deployment Step 1 Enter
the code name
9Step 2 Map the coded name to the JNDI name
10Persistence Management
- Container-Managed Persistence
- Entity bean code does not contain database access
calls. - The EJB container generates the SQL statements.
- Bean-Managed Persistence
- Entity bean code contains the database access
calls (SQLs) (i.e. you write the code!)
11Container Managed example Product entity bean
ProductEJB.java ProductHome.java Product.java Pro
ductClient.java
Bean Managed example Account entity bean
AccountEJB.java AccountHome.java Account.java Acc
ountClient.java
12(No Transcript)
13(No Transcript)
14(No Transcript)
15Transaction Management
16Container-Managed Transactions
- Description
- Code does not include statements that begin and
end the transaction - Immediately before an EJB method starts -
transaction begins - Just before the method exits - commits
- Each method can be associated with a single
transaction - Prohibited methods, e.g.
- commit, setAutoCommit, and rollback methods of
java.sql.Connection - Limitation
- When a method is executing, it can be associated
with either a single transaction or no
transaction at all
17Bean Managed Transaction
- Description
- Session bean code invokes methods that mark the
boundaries of the transaction - setAutoCommit()
commit() rollback() - An entity bean may not have bean-managed
transactions
public void ship (String productId, String
orderId, int quantity) try
con.setAutoCommit(false)
updateOrderItem(productId, orderId)
updateInventory(productId, quantity)
con.commit() catch (Exception ex)
try con.rollback()
throw new EJBException("Transaction
failed " ex.getMessage()) catch
(SQLException sqx) throw new
EJBException("Rollback failed "
sqx.getMessage())
Ref. JavaTM 2 Enterprise Edition Developer's
Guide, JDBC Transaction
18Resouces
- JavaTM 2 SDK, Enterprise Edition Technical
Documentation - JavaTM 2 Enterprise Edition Developer's Guide
- http//java.sun.com/j2ee/j2sdkee/
- http//archives.java.sun.com/archives/j2ee-interes
t.html - Developing Enterprise Applications with the
JavaTM 2 Platform Enterprise Edition
http//java.sun.com/j2ee/blueprints/ - http//www.cloudscape.com