[ Pobierz całość w formacie PDF ]
.So let's put some code to our talk.5.1.1 Making It WorkThe starting point to this solution is a new table in the database.This table is probably thesimplest yet: it has only two columns, no ID, and does not need to reference any other tables.Example 5-1 shows the SQL used to create this table.All the PRIMARY_KEYS table needs is aKEY_NAME column to hold the table name that the key is for, and a NEXT_VALUE column tohold the next valid primary key for that table.Finally, there is some Forethought-specificwork to do by adding entries for all the tables in the application, starting each value off at 1.Create this table in your database, using the SQL in Example 5-1.The script in Example 5-1 assumes that you have no existing data;therefore the counters for all tables start at 1.If you need to use thisstructure for an application with existing data, you will need to makesome small modifications.For each table, find the highest existing valuein use, and start with a number at least 1 greater than that value.So ifyour table has primary keys ranging as high as 2012, you want to startthat table's primary key value at 2013.Additionally, this script drops any existing PRIMARY_KEYS table.If youalready use this structure, and then run this script, you run the risk ofduplicating primary key values.Be careful!Example 5-1.SQL Script for Primary Keys Storage-- Drop any existing PRIMARY_KEYS tableDROP TABLE PRIMARY_KEYS;-- PRIMARY_KEYS tableCREATE TABLE PRIMARY_KEYS (KEY_NAME VARCHAR(20) PRIMARY KEY NOT NULL,NEXT_VALUE INT NOT NULL);-- Add initial values for each tableINSERT INTO PRIMARY_KEYS VALUES ('USER_TYPES', 1);INSERT INTO PRIMARY_KEYS VALUES ('OFFICES', 1);INSERT INTO PRIMARY_KEYS VALUES ('USERS', 1);INSERT INTO PRIMARY_KEYS VALUES ('ACCOUNT_TYPES', 1);INSERT INTO PRIMARY_KEYS VALUES ('ACCOUNTS', 1);INSERT INTO PRIMARY_KEYS VALUES ('TRANSACTIONS', 1);INSERT INTO PRIMARY_KEYS VALUES ('FUNDS', 1);INSERT INTO PRIMARY_KEYS VALUES ('INVESTMENTS', 1);With this table created, we can start writing the session bean to access the data in the table,ensuring that it is usable by the various entity beans in the application.72 Building Java"! Enterprise Applications Volume I: ArchitectureBefore getting too carried away, realize that this scenario assumes thatall the applications accessing your database will use this sequencingfacility.If you are going to have concurrent access from other Java (ornon-Java) components, the IDs in the PRIMARY_KEYS table can becomestale.In this case, you will need to take a different approach.5.1.1.1 Adapters and session beansFirst, it's time to save some time and effort by writing another utility class.Like entity beans,session beans have several callback methods that often are never coded in standard beans.Although there aren't as many methods for session beans, it is still a pain to write emptymethod implementations for tens or even hundreds of session beans in an application.Writinga SessionAdapter class, similar to the EntityAdapter class, allows us to avoid this hassleand keep session bean code clean and concise.Example 5-2 shows this class; there is nothingnew here, just a session bean version of Example 4-1.Example 5-2.The SessionAdapter Utility Classpackage com.forethought.ejb.util;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class SessionAdapter implements SessionBean {protected SessionContext sessionContext;public void ejbActivate( ) {}public void ejbPassivate( ) {}public void ejbRemove( ) {}public void setSessionContext(SessionContext sessionContext) {this.sessionContext = sessionContext;}public void unsetSessionContext( ) {sessionContext = null;}public SessionContext getSessionContext( ) {return sessionContext;}}All of your session bean implementations can then extend the SessionAdapter utility class,allowing them to ignore any callbacks that are not explicitly used in the implementation.Thispaves the way for building the actual bean used in sequence retrieval.73 Building Java"! Enterprise Applications Volume I: Architecture5.1.1.2 The application exceptionBefore writing the session bean itself, you should take a moment to define a new applicationexception.In EJB-land, application exceptions are used to report problems that are notdirectly caused by RMI, network communication, and container-related issues [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •