User Tools

Site Tools


at-m42:lecture14

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
at-m42:lecture14 [2009/04/25 14:52] eechrisat-m42:lecture14 [2011/01/14 12:45] (current) – external edit 127.0.0.1
Line 350: Line 350:
   * The following example opens a database of CD data and looks for a CD's that match a given theme as given on the command line.   * The following example opens a database of CD data and looks for a CD's that match a given theme as given on the command line.
   * It selects only the CDs that match the category then prints out the CD data:    * It selects only the CDs that match the category then prints out the CD data:
-  *  
-  *  
-  *  
-  *  
  
 ===== Find CDs for a party ===== ===== Find CDs for a party =====
Line 367: Line 363:
 ===== … define db attributes ===== ===== … define db attributes =====
 <code java> <code java>
-String dbUrl = "jdbc:derby:CDs"; +static final String DB = "jdbc:derby:CDs"; 
-String user = ""; +static final String USER = ""; 
-String password = "";+static final String PASSWORD = ""; 
 +static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
 </code> </code>
  
Line 382: Line 379:
  
 <code java> <code java>
-Class.forName("org.apache.derby.jdbc.EmbeddedDriver");+Class.forName(DRIVER);
 </code> </code>
  
Line 392: Line 389:
 ===== … connect to database ===== ===== … connect to database =====
 <code java> <code java>
-Connection c = DriverManager.getConnection(dbUrluserpassword);+Connection c = DriverManager.getConnection(DBUSERPASSSWORD);
 </code> </code>
  
 ---- ----
  
-  * Database driver knows how to connect to databases that it manages.+  * The atabase driver knows how to connect to databases that it manages.
   * Connection object represents the database for rest of session.   * Connection object represents the database for rest of session.
   * Possible (and desirable) to pool connections if many clients may access same database simultaneously.   * Possible (and desirable) to pool connections if many clients may access same database simultaneously.
Line 417: Line 414:
 ===== … execute query and return results ===== ===== … execute query and return results =====
 <code java> <code java>
-ResultSet r +// SQL query 
-  .executeQuery("SELECT albums.title, bands.band_name, albums.year\n" +String QUERY = "SELECT albums.title, bands.band_name, albums.year\n" 
-    + "  FROM bands INNER JOIN albums ON \n" +                + "  FROM bands INNER JOIN albums ON \n" 
-    + "     bands.band_id = albums.artist \n" +                + "     bands.band_id = albums.artist \n" 
-    + "  WHERE (((albums.category)='" + args[0] + "'))");+                + "  WHERE (((albums.category)='" + args[0] + "'))" 
 +// SQL code: 
 +ResultSet r = s.executeQuery(QUERY);
 </code> </code>
  
Line 463: Line 462:
   * If you have a pool of connections you'd want to release your connection (returning it to the pool) when you've finished.   * If you have a pool of connections you'd want to release your connection (returning it to the pool) when you've finished.
      
 +=====  Simplified Queries with Groovy =====
 + 
 +
 +<code groovy 1|Example 2; Database lookup in Groovy (at-m42/Lectures/lecture14/Lookup.groovy)>
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture14/Lookup.groovy
 +</code>
 +
 +----
 +
 +As with other examples we've seen in this course, the designers of Groovy have created a JDBC API that further simplifies the interaction with a JDBC-supported database (and as we'll see, takes this even further in the Grails framework).
 +
 +In this example we note immediately that the opening of a connection to a database is achieved in a single //factory method// ''def sql = Sql.newInstance(DB, USER, PASSWORD, DRIVER)''. Once we have the ''Sql'' object, we can use the ''eachRow'' method, which takes a closure, to process each row. Note also, that the results are returned as a Map, so we can refer to each field as ''r.band_name'' (or if you prefer ''%%r['band_name']%%'' rather than the more long-winded ''r.getString(%%"bAND_NAME"%%)''). Also, because Groovy is a dynamic language, we need not know, nor care, what the type of the field represents.
 +
 +Further more, the use of multi-line strings simplifies the creation of queries, and because ''String'' interpretation works, queries can easily be made //programmatic//.
 +
 ===== SQL Data-types and Java Data-types ===== ===== SQL Data-types and Java Data-types =====
  
Line 523: Line 537:
 ===== Immortal Objects ===== ===== Immortal Objects =====
  
-  * How to ensure that the state of your business objects survives (persists) beyond the ending of one process to the start of the next. E.g. to survive crashes and shutdown.+  * How to ensure that the state of your business objects survives (''persists'') beyond the ending of one process to the start of the next. E.g. to survive crashes and shutdown.
   * Although you can use files for persistence, it doesn't scale well.   * Although you can use files for persistence, it doesn't scale well.
   * These days you have to use a relational database. If you use Java to implement your application you have to use JDBC.   * These days you have to use a relational database. If you use Java to implement your application you have to use JDBC.
   * How can persistence issues can be affectively hidden from the business objects that rely on it?   * How can persistence issues can be affectively hidden from the business objects that rely on it?
-  * Persistence is Last Step in a Transaction+===== Persistence is Last Step in a Transaction =====
   * In distributed applications we need transactions to guarantee data integrity.   * In distributed applications we need transactions to guarantee data integrity.
-  * Commit if successful. Rollback if fails.+    * Commit if successful. Rollback if fails.
   * Transaction choreographs a persistence operation.    * Transaction choreographs a persistence operation. 
-  * When the transaction is notified that it is complete it creates a persistence transaction which tells each object modified in the transaction to insert, update or delete itself in the persistent store. +  * When the transaction is notified that it is complete it creates a //persistence transaction// which tells **each object modified in the transaction** to //insert////update// or //delete// itself in the persistent store. 
-  * The persistence transaction makes sure that all data store accesses take place within a single data store transaction (i.e using the same connection object) and is committed at the end.+  * The persistence transaction makes sure that all data store accesses take place within a **single data store transaction** (i.e using the same connection object) and is **committed** at the end.
   * The component model doesn't care about how the persistence is achieved. It only cares about the transaction.   * The component model doesn't care about how the persistence is achieved. It only cares about the transaction.
-   +   
-  Component Transaction and Persistence Transaction+===== Component Transaction and Persistence Transaction ===== 
 +{{:at-m42:persitence.png?417|Persistence transaction}} 
 ===== Lecture Contents ===== ===== Lecture Contents =====
  
-  * **[[#What is a Relational Database?|An introduction to relational databases and SQL]]**+  * [[#What is a Relational Database?|An introduction to relational databases and SQL]]
   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]
-  * [[Immortal Objects]] +  * [[#Immortal Objects]] 
-  * [[Persistence Patterns]] +  * **[[#Persistence Patterns]]** 
-  * [[Other forms of Enterprise Integration]]+  * [[#Other forms of Enterprise Integration]]
      
-===== Persistence Patterns ===== + 
-  In many applications it is natural to work directly with the relational model, issuing SQL queries and parsing result sets. +===== Persistence Patterns: Transaction script ===== 
-  * This is known as the Transaction Script pattern: business logic is organized into procedures for each use case.+In many applications it is natural to work directly with the relational model, issuing SQL queries and parsing result sets. 
 +  * This is known as the //[[http://martinfowler.com/eaaCatalog/transactionScript.html|Transaction Script]]// pattern((All patterns discussed in this lecture are from //Patterns of Enterprise Application Architecture//. See [[Recommended Reading]] for bibliographic details.)): business logic is organized into procedures for each use case.
   * Direct use of SQL is also well suited for aggregating queries and set-based updates (accessing large amounts of data or updating many tables in the same transaction)   * Direct use of SQL is also well suited for aggregating queries and set-based updates (accessing large amounts of data or updating many tables in the same transaction)
-  * Another scenario is that you have simple queries returning small sets of rows (often in a single table), which receive selective updates.+ 
 +===== Persistence Patterns: ORM ===== 
 + 
 +Another scenario is that you have simple queries returning small sets of rows (often in a single table), which receive selective updates.
   * Little need for set-based updates   * Little need for set-based updates
   * Data entities mapped to persistent Java objects    * Data entities mapped to persistent Java objects 
   * Business logic deals with these objects rather than the tables and records.   * Business logic deals with these objects rather than the tables and records.
-  * General term for latter is Object-Relational Mapping (ORM) +General term for this pattern is Object-Relational Mapping (ORM) 
-   + 
-  Patterns for ORM Strategies +===== Patterns for ORM Strategies ===== 
-  * Active record – wraps a row in a database table, encapsulates the database access, and adds domain logic to the data. +  * //[[http://martinfowler.com/eaaCatalog/activeRecord.html|Active record]]// – wraps a row in a database table, encapsulates the database access, and adds domain logic to the data. 
-  * Data mapper – A layer of mappers that move data between objects and a database while keeping them independent of each other and the mapper itself. +  * //[[http://martinfowler.com/eaaCatalog/dataMapper.html|Data mapper]]// – A layer of mappers that move data between objects and a database while keeping them independent of each other and the mapper itself. 
-  * Query object – An object that represents a database query +  * //[[http://martinfowler.com/eaaCatalog/queryObject.html|Query object]]// – An object that represents a database query 
-  * Unit of work – Maintains a list of objects effected by a business transaction and coordinates the writing of changes and the resolution of concurrency problems. +  * //[[http://martinfowler.com/eaaCatalog/unitOfWork.html|Unit of work]]// – Maintains a list of objects effected by a business transaction and coordinates the writing of changes and the resolution of concurrency problems. 
-  *  +===== Persistence Frameworks ===== 
-  * Persistence Frameworks + 
-  * You can "roll your own” persistence framework … +  * You can "roll your ownpersistence framework ... 
-  * See e.gextended examples in Reese+ 
-  * … but its a non-trivial task and fraught with difficulties and traps.+  * ... but it's a non-trivial task and fraught with difficulties and traps.
   * Better to use an existing framework. Many available!   * Better to use an existing framework. Many available!
-  * Most popular: CMP (more later), JDO (Java extension) and Hibernate (an open source framework). +  * All implement //Data Mapper//, //Query Object// and //Unit of Work// patterns to provide **Transparent Persistence** for Java objects. 
-  * All implement Data Mapper, Query Object and Unit of Work patterns to provide Transparent Persistence for Java objects+ 
-  The Object/Relational "Impedance Mismatch+---- 
 + 
 +Reese (see [[Recommended Reading]]) gives an extended example of the development of a persistence framework. 
 + 
 +The most widely used persistence frameworks are 
 +  * Java EE Container Managed Persistence (CMP(more later),  
 +  * [[http://java.sun.com/jdo/index.jsp|Java Data objects]] (JDO(Java extension),  
 +  * [[http://www.hibernate.org/|Hibernate]] (a widely deployed open source framework) and  
 +  * The "official" [[https://glassfish.dev.java.net/javaee5/persistence/#Java_Persistence_API|Java Persistence API]] (JPA) part of Java EE 5 and available as a standalone extension API for Java SE 6
 + 
 +===== The Object/Relational Impedance Mismatch =====
   * For all but the simplest domain models, there is no direct match from relational database to object model.   * For all but the simplest domain models, there is no direct match from relational database to object model.
-  * OO concepts such as class, polymorphism, and inheritance have no direct analogy in an RDMS. +  * OO concepts such as class, //polymorphism//, and //inheritance// have no direct analogy in an RDMS. 
-  * RDMS concepts such as normalized data, sets and efficient access have no direct relationship in the OO world. +  * RDMS concepts such as //normalized data////sets// and //efficient access// have no direct relationship in the OO world. 
-  * Its best to keep the two worlds separate and in the domain of the experts. +  * It's best to keep the two worlds separate and in the domain of the experts. 
-  * Use an ORM framework (if its appropriate) to provide the bridge between the two worlds. +  * Use an ORM framework (if it's appropriate) to provide the bridge between the two worlds. 
-  *  +===== Some Issues that the Developers of an ORM Have to be Aware Of ===== 
-  * Some Issues that the Developers of an ORM Have to be Aware Of +  * //Identity//: ensuring a record in a database is unique in a global sense. 
-  * Identity: ensuring a record in a database is unique in a global sense. +  * //Instantiated objects should be cached//: avoids a further trip to the database if it is needed in two separate transactions. 
-  * Instantiated objects should be cached: avoids a further trip to the database if it is needed in two separate transactions. +  * "Dirtyobjects (objects that have been created or changed in a transaction) //have to be committed to the database in a single atomic operation//.
-  * "Dirty” objects (objects that have been created or changed in a transaction) have to be committed to the database in a single atomic operation.+
   * Objects that have not been changed do not need to be written back to the database.   * Objects that have not been changed do not need to be written back to the database.
   * Queries may result in large numbers of records being returned from a database:   * Queries may result in large numbers of records being returned from a database:
-  * Usually returned as a collection +    * Usually returned as a collection 
-  * However objects in the collection should not be instantiated unless it is needed. +    * However objects in the collection should not be instantiated unless it is needed. 
-  *  +===== Choosing a Persistence Strategy (1) =====
-  *  +
-  *  +
-  *  +
-  * Choosing a Persistence Strategy+
   * Many enterprise applications need to use legacy databases, or share the database with other systems, so choices are limited!   * Many enterprise applications need to use legacy databases, or share the database with other systems, so choices are limited!
   * Despite the hype, it is rare for an enterprise to change its database supplier, so it is often not worth completely abstracting the details of a DBMS out of code.   * Despite the hype, it is rare for an enterprise to change its database supplier, so it is often not worth completely abstracting the details of a DBMS out of code.
-  * It is worth providing a data access layer so that your business logic does not talk directly JDBC but goes through a set of Data Access Objects.  +  * It is worth providing a data access layer so that your business logic does not talk directly JDBC but goes through a set of //Data Access Objects//.  
-  * If the database schema changes, it will be in the access layer that changes will be needed, not in the business logic. +    * If the database schema changes, it will be in the access layer that changes will be needed, not in the business logic. 
-  * If the business logic changes, again, persistence code changes are limited to the access layer. +    * If the business logic changes, again, persistence code changes are limited to the access layer. 
-  Choosing a Persistence Strategy +===== Choosing a Persistence Strategy (2) ===== 
-  * In some applications with a limited number of simple tables it will be quickest to use active record and talk directly to the database. +  * In some applications with a limited number of simple tables it will be quickest to use //active record// and talk directly to the database. 
-  * If the application will require heavy use of set access, aggregation of data from many tables, or batch updates in many rows, a direct implementation using transaction script may be most efficient.+  * If the application will require heavy use of set access, aggregation of data from many tables, or batch updates in many rows, a direct implementation using //transaction script// may be most efficient.
   * ORM is a complex strategy which be of benefit only for complex domain models and or databases   * ORM is a complex strategy which be of benefit only for complex domain models and or databases
-  * Choosing a Persistence Strategy +===== Choosing a Persistence Strategy (3) ===== 
-  ORM can be of benefit if:+ORM can be of benefit if:
   * Your application has the typical CRUD – create, retrieve, update, delete – workflow for domain objects.   * Your application has the typical CRUD – create, retrieve, update, delete – workflow for domain objects.
   * Objects are found in large sets but are updated and deleted individually.   * Objects are found in large sets but are updated and deleted individually.
-  * A large number of objects exist but they are "read-mostly+  * A large number of objects exist but they are "read-mostly"
   * There is a natural mapping between classes and fields and database tables and records   * There is a natural mapping between classes and fields and database tables and records
-  * There are no unusual requirements such as the need to used customized SQL optimizations.+  * There are no unusual requirements such as the need to use customized SQL optimizations.
   * For Java programmers ORM has the advantage of keeping SQL out of the code. But that is why we have DB architects!   * For Java programmers ORM has the advantage of keeping SQL out of the code. But that is why we have DB architects!
      
 +
 +
 ===== Lecture Contents ===== ===== Lecture Contents =====
  
-  * **[[#What is a Relational Database?|An introduction to relational databases and SQL]]**+  * [[#What is a Relational Database?|An introduction to relational databases and SQL]]
   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]
-  * [[Immortal Objects]] +  * [[#Immortal Objects]] 
-  * [[Persistence Patterns]] +  * [[#Persistence Patterns]] 
-  * [[Other forms of Enterprise Integration]]+  * **[[#Other forms of Enterprise Integration]]**
      
 ===== Other Forms of Enterprise Integration ===== ===== Other Forms of Enterprise Integration =====
  
   * Enterprise integration is not just about databases and SQL.   * Enterprise integration is not just about databases and SQL.
-  * Enterprise information systems such as:+  * Enterprise information systms also often have to be built in to enterprise applications. 
 +  * Main technologies for this form of integration is the Java Connectivity API and Web Services. 
 +  * May not be talking Java so CORBA and similar technologies may come into play. 
 +   
 +---- 
 + 
 +Some examples of enterprise information systems are: 
   * Human Resources Systems   * Human Resources Systems
   * Payroll systems   * Payroll systems
Line 622: Line 656:
   * Business to Business Systems   * Business to Business Systems
   * Enterprise communication systems   * Enterprise communication systems
-  * Other "legacy” systems  +  * Other //so-called// "legacysystems  
-  * also often have to be built in to enterprise applications. +  
-  * Main technologies are the Java Connectivity API and Web Services. +
-  * May not be talking Java so CORBA and similar technologies come into play.+
 ===== Lecture Summary ===== ===== Lecture Summary =====
  
   * [[#What is a Relational Database?|An introduction to relational databases and SQL]]   * [[#What is a Relational Database?|An introduction to relational databases and SQL]]
   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]   * [[#The Trouble with Databases|JDBC -- Java database connectivity API]]
-  * [[Immortal Objects]] +  * [[#Immortal Objects]] 
-  * [[Persistence Patterns]] +  * [[#Persistence Patterns]] 
-  * [[Other forms of Enterprise Integration]]+  * [[#Other forms of Enterprise Integration]]
  
 ---- ----
  
 [[Home]] | [[lecture13|Previous Lecture]] | [[Lectures]] | [[lecture15|Next Lecture]]  [[Home]] | [[lecture13|Previous Lecture]] | [[Lectures]] | [[lecture15|Next Lecture]] 
at-m42/lecture14.1240671170.txt.gz · Last modified: 2011/01/14 12:24 (external edit)