Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

DaisyWongJ2EE.ppt

703 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

DaisyWongJ2EE.ppt

  1. 1. JDBC in J2EE <ul><li>Connection to Cloudscape database using JDBC </li></ul><ul><li>J2EE Platform – services and architecture </li></ul><ul><li>Enterprise JavaBeans (EJB) </li></ul><ul><ul><li>Session Beans vs. Entity Beans </li></ul></ul><ul><li>EJB access to databases using JDBC </li></ul><ul><ul><li>Database connection </li></ul></ul><ul><ul><li>Persistence management (Entity Bean e.g.) </li></ul></ul><ul><ul><li>Transaction management (Session Bean e.g.) </li></ul></ul>
  2. 2. J2EE Services <ul><li>HTTP - enables Web browsers to access servlets and JavaServer PagesTM (JSP) files </li></ul><ul><li>EJB - allows clients to invoke methods on enterprise beans </li></ul><ul><li>Authentication - enforces security by requiring users to log in </li></ul><ul><li>Naming and Directory - allows programs to locate services and components through the Java Naming and Directory InterfaceTM (JNDI) API </li></ul>
  3. 3. J2EE Architecture Ref. Java TM 2 Enterprise Edition Developer's Guide, Figure 1-2
  4. 4. Enterprise JavaBeans (EJB) <ul><li>Server-side Java components </li></ul><ul><li>Contain the business logic of enterprise application </li></ul><ul><li>Support database access </li></ul><ul><li>Transactional </li></ul><ul><li>Multi-user secure </li></ul><ul><li>Managed by the EJB container </li></ul><ul><li>Prohibited from a set of operations </li></ul>
  5. 5. Session Bean vs. Entity Bean Ref. Java TM 2 Enterprise Edition Developer's Guide, Table 1-1 Persistent. Entity state remains in a database. Not persistent. Persistence May be shared by multiple clients. May have one client. Shared Access Represents a business entity object that exists in persistent storage. Performs a task for a client Purpose Entity Bean Session Bean
  6. 6. EJB Access to Databases Using JDBC API <ul><li>Making a connection to database: </li></ul><ul><li>Should not hardcode the actual name (URL) of the database in EJB </li></ul><ul><li>Should refer to the database with a logical name </li></ul><ul><li>Use a JNDI lookup when obtaining the database connection. </li></ul><ul><li>Users and password not needed for the Cloudscape bundled with J2EE </li></ul>J2EE uses JDBC 2.0 (java.sql) and JDBC 2.0 Optional package (javax.sql) <ul><li>Driver and Data source properties: </li></ul><ul><li>set in J2EE default.properties file </li></ul><ul><li>jdbc.drivers=COM.cloudscape.core.RmiJdbcDriver </li></ul><ul><li>jdbc.datasources=jdbc/Cloudscape|jdbc:cloudscape:rmi:CloudscapeDB; create=true </li></ul>
  7. 7. Making a connection to database example 1. Specify the logical database name. private String dbName = &quot;java:comp/env/jdbc/AccountDB&quot;; 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();
  8. 8. Specifying JNDI name for deployment Step 1: Enter the code name
  9. 9. Step 2: Map the coded name to the JNDI name
  10. 10. Persistence Management <ul><li>Container-Managed Persistence </li></ul><ul><li>Entity bean code does not contain database access calls. </li></ul><ul><li>The EJB container generates the SQL statements. </li></ul><ul><li>Bean-Managed Persistence </li></ul><ul><li>Entity bean code contains the database access calls (SQLs) (i.e. you write the code!) </li></ul>
  11. 11. <ul><li>Container Managed example : Product entity bean </li></ul><ul><ul><li>ProductEJB.java </li></ul></ul><ul><ul><li>ProductHome.java </li></ul></ul><ul><ul><li>Product.java </li></ul></ul><ul><ul><li>ProductClient.java </li></ul></ul><ul><li>Bean Managed example : Account entity bean </li></ul><ul><ul><li>AccountEJB.java </li></ul></ul><ul><ul><li>AccountHome.java </li></ul></ul><ul><ul><li>Account.java </li></ul></ul><ul><ul><li>AccountClient.java </li></ul></ul>
  12. 15. Transaction Management
  13. 16. Container-Managed Transactions <ul><li>Description </li></ul><ul><li>Code does not include statements that begin and end the transaction </li></ul><ul><li>Immediately before an EJB method starts - transaction begins </li></ul><ul><li>Just before the method exits - commits </li></ul><ul><li>Each method can be associated with a single transaction </li></ul><ul><li>Prohibited methods, e.g.: </li></ul><ul><li>commit, setAutoCommit, and rollback methods of java.sql.Connection </li></ul><ul><li>Limitation: </li></ul><ul><li>When a method is executing, it can be associated with either a single transaction or no transaction at all </li></ul>
  14. 17. Bean Managed Transaction <ul><li>Description </li></ul><ul><li>Session bean code invokes methods that mark the boundaries of the transaction - setAutoCommit(); commit(); rollback(); </li></ul><ul><li>An entity bean may not have bean-managed transactions </li></ul>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(&quot;Transaction failed: &quot; + ex.getMessage()); } catch (SQLException sqx) { throw new EJBException(&quot;Rollback failed: &quot; + sqx.getMessage()); } } } Ref. Java TM 2 Enterprise Edition Developer's Guide, JDBC Transaction
  15. 18. Resouces <ul><li>Java TM 2 SDK, Enterprise Edition Technical Documentation </li></ul><ul><ul><li>Java TM 2 Enterprise Edition Developer's Guide </li></ul></ul><ul><li>http://java.sun.com/j2ee/j2sdkee/ </li></ul><ul><li>http://archives.java.sun.com/archives/j2ee-interest.html </li></ul><ul><li>Developing Enterprise Applications with the Java TM 2 Platform Enterprise Edition http://java.sun.com/j2ee/blueprints/ </li></ul><ul><li>http://www.cloudscape.com </li></ul>

×