JDBC in J2EE

Document Sample
scope of work template
							                 JDBC 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.)
                    J2EE 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
                               J2EE Architecture




Ref. JavaTM 2 Enterprise Edition Developer's Guide, Figure 1-2
             Enterprise 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
            Session Bean vs. Entity Bean

                     Session Bean                        Entity Bean
Purpose           Performs a task                  Represents a business
                  for a client                     entity object that
                                                   exists in persistent
                                                   storage.
Shared            May have one                     May be shared by
Access            client.                          multiple clients.
Persistence Not persistent.                        Persistent. Entity state
                                                   remains in a database.

         Ref. JavaTM 2 Enterprise Edition Developer's Guide, Table 1-1
        EJB 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.drivers=COM.cloudscape.core.RmiJdbcDriver
jdbc.datasources=jdbc/Cloudscape|jdbc:cloudscape:rmi:CloudscapeDB;
create=true
         Making a connection to database example

1. Specify the logical database name.
  private String dbName = "java:comp/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();
Specifying JNDI name for deployment Step 1: Enter the code name
Step 2: Map the coded name to the JNDI name
             Persistence 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!)
Container Managed example: Product entity bean
   ProductEJB.java
   ProductHome.java
   Product.java
   ProductClient.java


Bean Managed example: Account entity bean
   AccountEJB.java
   AccountHome.java
   Account.java
   AccountClient.java
Transaction Management
           Container-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
                           Bean 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
                          Resouces

• 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-interest.html

• Developing Enterprise Applications with the JavaTM 2 Platform
  Enterprise Edition http://java.sun.com/j2ee/blueprints/

• http://www.cloudscape.com

						
Related docs