4.7. Key Point Summary

The Music EJB accesses a database through the JDBC API. Because this access is read only, we implement our business process with a session bean. A stateless session bean is appropriate because keeping track of client-specific state is not necessary.

The Music EJB uses the Value Object pattern introduced in the previous chapter. In addition, we introduce the Data Access Object pattern, which abstracts persistence operations into a separate helper class. Using an interface for vendor-independent database access helps implement classes that work with a particular vendor's SQL implementation. A factory instantiates the correct implementation class by looking up the class name in the naming context environment entry.

Here are the key points from the chapter.

  • The Java Database Connectivity (JDBC) API provides a portable way to access a relational database using SQL.

  • The JDBC API provides a way to instantiate Statements and PreparedStatements, which consists of SQL query, update,create, or delete commands. In general, PreparedStatements are more efficient than Statements, because the database can cache a compiled statement and not recompile it with each access.

  • Be careful to close statements and result sets properly. This frees up database resources and makes a difference with high-volume database hits. Closing database resources within a finally block guarantees that it's closed even if an exception is thrown.

  • The Statement method executeQuery() requests SQL execution, returning the query data in an object called a ResultSet. Method executeUpdate() performs other SQL operations, such as updates, deletions, removals, and insertions.

  • The Music Collection Database consists of four related tables, as diagrammed in Figure 4-2 on page 88. Relational databases use foreign keys to relate records from one table to records of another.

  • The Music EJB provides read-only business methods to access the Music Collection Database.

  • The bean implementation class (MusicBean) contains a non-empty ejbCreate() method that we use to perform initializations of instance variables. These variables are not client-specific, so we are able to use a stateless session bean.

  • Class NoTrackListException is an example of an application exception, which represents nonfatal, recoverable errors. The EJB container propagates application exceptions to the remote client. Application exceptions should subclass Exception.

  • Class MusicDAOSysException is an example of a system exception, which represents nonrecoverable system errors. Enterprise bean code should catch all system exceptions and throw an EJBException back to the EJB container. The container can then decide what to do. Frequently, the container wraps a system exception in a RemoteException and sends it to the remote client. System exceptions should subclass RuntimeException.

  • Classes RecordingVO and TrackVO implement the Value Object pattern and encapsulate related data into coarse-grained objects. This reduces the number of remote calls needed to access the encapsulated data.

  • The Data Access Object (DAO) pattern improves database access flexibility and encapsulates all database access into separate helper classes. Use the Factory pattern to instantiate the specific DAO class by performing a naming service lookup.

  • The Music EJB is client independent. In this chapter, we've used both a Java Swing application client and a web-based JSP program to access the Music EJB.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.138.134