EJB 21 days

download EJB 21 days

of 22

Transcript of EJB 21 days

  • 8/12/2019 EJB 21 days

    1/22

    Sams Teach Yourself EJB in 21 Days

    Copyright 2003 by Sams Publishing

    International Standard Book Number: 0-672-32423-7

    (d)Warning and Disclaimer

    Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitnimplied. The information provided is on an "as is" basis. The author and the publisher shall have neither liabresponsibility to any person or entity with respect to any loss or damages arising from the information conta

    this book.

    When reviewing corrections, always check the print number of your book. Corrections are made to printed books wsubsequent printing. To determine the printing of your book, view the copyright page. The print number is rignumber on the line below the "First Printing" line. For example, the following indicates the 4thprinting of a

    First Printing: June 2002

    05 04 03 02 4 3 2 1

  • 8/12/2019 EJB 21 days

    2/22

    Misprint CorrectionPage xxix, Ragae Ghaly bio

    He is an independent consultant, and he He is a senior architect for Digital Evolution, aServices company. He

    Page 15, paragraph immediately following first note

    The most common examples of a view beansare in designing aGUI.

    The most common examples of a view bean are in GU

    Page 17, paragraph following first bulleted list

    If the containers in which EJB components are deployedassume responsibility for managing runtime services such aspersistence, transactions, and concurrent database access,bean developers are free to focus on developing softwarecomponents that encapsulate business logic.

    Bean developers are free to focus on developing scomponents that encapsulate business logic, as thin which EJB components are deployed assume responfor managing runtime services such as persistencetransactions, and concurrent database access.

    Page 33, paragraph in middle of page

    Entity beans typically contain data-related logic, such asinserting, updating, and removing a customerrecord in thedatabase.

    Entity beans typically contain data-related logicinserting, updating, and removing a record in the

    Page 51, first bullet

    A remote home interface extends the java.ejb.EJBHomeinterface, whereas a local home interface extendsjavax.ejb.EJBLocalObject.

    A remote home interface extends the java.ejb.EJBHomewhereas a local home interface extends javax.ejb.EJ

    Page 51, third bulletThe bean class implements the javax.ejb.SessionBeaninterface.In addition, it implements all business methods listed inthe remoteinterface.

    The bean class implements the javax.ejb.SessionBeaniIn addition, it implements all business methods lcomponentinterface.

    Page 55, last code snippet on page

    /* ctx and cart constitute the conversational state */ /* The member variable cart constitutes the enterprise

    bean's conversational state */

  • 8/12/2019 EJB 21 days

    3/22

    Page 57, third paragraph

    Afterserializing the enterprise bean state, the EJB

    container calls the ebjPassivate()method on the instance.

    Beforeserializing the enterprise bean state, the

    container calls the ebjPassivate()method on the ins

    Page 59, "What You Need to Do" column for ejbRemove()entry

    Close resources, if any, and assign corresponding instancefields to null. For example, close sockets, JDBCconnections, and so on. For a stateless session bean, thisis empty because the container never passivates the beaninstance.

    Close resources, if any, and assign corresponding fields to null. For example, close sockets, JDBC cand so on. The instance typically releases the samthat it releases in the ejbPassivate()method.

    Page 60, second bulletThe container instantiates the bean using the newInstancemethod and then calls the methods setSessionContextandejbCreate. The container also sets the transaction contextand security attributes (as set in the deploymentdescriptor). Now the bean is ready to serve any client.

    The container instantiates the bean using the newImethod and then calls the methods setSessionContextejbCreate.

    Page 68, first paragraph

    JNDI is a unified Java API designed to standardizedaccessto a variety of naming and directory services.

    JNDI is a unified Java API designed to standardiza variety of naming and directory services.

    Page 68, first bullet

    An application-level programming interface (API). APIsareused by the application components to access naming anddirectory services.

    An application-level programming interface (API).used by the application components to access namindirectory services.

    Page 69, paragraph preceding "Context Operations"

    The tree-like structure of JNDI is a natural to supportsubcontexts.

    The tree-like structure of JNDI inherently supporsubcontexts.

  • 8/12/2019 EJB 21 days

    4/22

    Page 73, last item in Table 4.1

    Disconnects from the JNDI service, and is used tofreeresources used by a context.

    Disconnects from the JNDI service and frees resoua context.

    Page 75, replace code snippet on bottom half of page Day 4: Demonstration of the usage of JNDI...

    Connecting to a JNDI service...

    Connected successfully. Initial context created.

    Getting Environment Properties... Properties: {java.naming.provider.url=t3://localhost:7001,

    java.naming.factory.initial=weblogic.jndi.WLInitialContextFa

    Binding a new name: mary to an object: [email protected]...

    Object: [email protected] is bound to name: mary

    Looking up the name...

    Found Name= mary, with email= [email protected]

    Unbinding the name...

    Name is unbound successfully!

    Spanning JNDI context bindings...

    javax: weblogic.jndi.internal.WLContextImpl: WLContext (ja

    jms: weblogic.jndi.internal.WLContextImpl: WLContext (java

    transaction: weblogic.jndi.internal.WLContextImpl: WLConte

    (javax.transaction)

  • 8/12/2019 EJB 21 days

    5/22

    weblogic: weblogic.jndi.internal.WLContextImple: WLContext

    drs: weblogic.jndi.internal.WLContextImpl: WLContext (webl

    jms: weblogic.jndi.internal.WLContextImpl: WLContext (webl

    transaction: weblogic. jndi.internal.WLContextImple: WLCon

    (weblogic.transaction)

    rmi: weblogic.jndi.internal.WLContextImple: WLContext (web

    common: weblogic.jndi.internal.WLContextImpl: WLContext

    (weblogic.common)

    jdbc. weblogic. jndi.internal.WLContextImpl: WLContext (we j2ee: weblogic.jndi.internal.WLContextImpl: WLContext (web

    management: weblogic.jndi.internal.WLContextImpl: WLContex

    (weblogic.management)

    security: weblogicl.jndi.internal.WLContextImpl: WLContext

    (weblogic.security)

    fileSystem: weblogic.jndi.internal.WLContextImpl: WLContex

    (weblogic.fileSystem) . . .

    Lookup for the unbound name...error expected

    **ERROR: An unexpected exception

    occurred...javax.naming.NameNotFoundException: Unable to

    'mary' Resolved: '' Unresolved:'mary' ; remaining name 'mary

    Connection to JNDI is closed successfully.

    Page 76, paragraph after "Programmatic Method"

    The following sample code illustrates the accessto a JNDIservice from inside your code:

    The following sample code illustrates the connectservice from inside your code:

  • 8/12/2019 EJB 21 days

    6/22

    Page 80, first paragraph

    Clients can be calling from the Web tier to the business-tier, or from the client tier to either the Web tier or tothe EJB tier.

    Clients can be calling from the Web tier to the butier, or from the client tier to either the Web tthe business-tier.

    Page 83, end of first paragraph (See the Service Locator Design Pattern section in Day 15.Page 84, last paragraph

    Try to use compositenames such as the DNS namewww.samspublishing.comand the Windows/DOS name

    file://c:/projects/myfile.doc.

    Try to use compoundnames such as the DNS namewww.samspublishing.comand the Windows/DOS name

    file://c:/projects/myfile.doc.Page 94, headline for Listing 5.7

    The Full Text of C:\styejb\examples\day05\jboss.xml The Full Text of day05/jboss.xmlPage 96, code in step 1

    C:\styejb>setEnvWebLogic.batC:\styejb>startWebLogic.bat

    C:\styejb\examples>setEnvWebLogic.batC:\styejb\examples>startWebLogic.bat

    Page 97, Note at top of page

    When prompted, enter systemas the username and administratoras the password.

    When prompted, enter the username (system) and pachose when you installed WebLogic Server (see App"WebLogic Application Server 7.0").

    Page 117, Listing 6.7

    for(int i = 0; i < 10; i++ ) for(int i = 0; i < 15; i++ )Page 124, first paragraph after "Understanding Web

    Applications"

    These requests can be in the form of either HTTP PUT or GETactions that are processed by the Web components to generateresults back to the client.

    These requests can be in the form of either HTTP GETactions that are processed by the Web componengenerate results back to the client.

    Page 128, Figure 7.2

    manifest.xml should be manifest.mf

  • 8/12/2019 EJB 21 days

    7/22

    Page 149, last paragraph

    ...parameters to the appropriate components, which handle

    the business logic. Finally, the servlettakes the resultback and uses it to provide a response to the user. Theservletusually forwards the response to a JSP to perform apresentation task (see Figure 7.9). Servlets are the linkbetween the client request and the model (EJB andJavaBeans).

    ...parameters to the appropriate handler, which c

    business logic. Finally, the handlertakes the reand uses it to provide a response to the user. Thusually forwards the response to a JSP to perform presentation task (see Figure 7.9). Servlets are between the client request and the model (EJB and The Struts framework of Apache's Jakarta Project source Java framework, which is based on the MVC pattern and the ideas presented in this section.

    Page 158, third paragraph

    Entity beans typically contain data-related logic thatperforms a task such as inserting, updating, or removing acustomerrecord in the database.

    Entity beans typically contain data-related logic performs a task such as inserting, updating, or rrecord in the database.

    Page 160, caption for Figure 8.3

    Bean-managed persistence contains the calls that access thedatabase.

    Bean-managed persistence bean contains the calls the database.

    Page 161, paragraph following "Container-ManagedPersistence"

    If your bean has container-managed persistence (CMP),... With container-managed persistence,...

    Page 165, paragraph following "Instance Pool and InstanceCache"

    Just as with stateless session beans, and EJB container maymaintain and instance pool ofeach type of entity bean.

    Just as with stateless session beans, and EJB conmaintain and instance pool foreach type of entit

    page 172, first entry in Table 8.2

    SetEntityContext setEntityContext

    Pages 175 and 176, replace "Best Practices section Before the introduction of the EJB 2.0 specificatdevelopers often used BMP rather than CMP. The EJBspecification enabled developers to develop portabapplications using CMP that are database independ

  • 8/12/2019 EJB 21 days

    8/22

    of database access code.

    In addition, containers typically optimize the da

    code generated for CMP entity beans. The containemonitor for any change to the in-memory buffer ofdata and avoid writing this data to the database did not change. Also, the container can optimize

    methods. To load an entity bean from a database, two calls to the database: The first call loads tkey via a finder method and the second call loads bean data using the ejbLoad()method. On the other CMP the container can load the entity data in a sto the database. Thus the entity beans can give bperformance than BMP entity beans because the congood hold on CMP entity beans.

    Page 176, replace "Summary" Today you examined entity beans and learned that server-side components that represent back-store

    middle tier. By design, entity beans are persistesurvive any server crashes. They are transactiona

    their state with multiple clients.

    You also learned about both types of persistence: managed and container-managed. With bean-managed pthe entity bean contains the calls that access th

    With container-managed persistence, the EJB contaresponsible for generating the necessary data acc

    The EJB container may maintain an instance pool f

    of entity bean. This saves the precious time of cdestroying objects. To save the resources such as EJB container can have an instance cache for each entity bean.

    You also examined the various entity bean methodsmethods allow clients to create entity beans. Themethods allow clients to locate entity beans. Theinvokes the ejbLoad()and ejbStore()methods to synch

  • 8/12/2019 EJB 21 days

    9/22

    entity bean's state with the corresponding values database. The home methods contain business logic

    specific to an entity bean instance. Over the nexyou will examine entity beans in more detail with examples.

    Page 182, Figure 9.2

    Applet box under EJB tier should be named EJB

    Page 198, first paragraph

    To change this implicit behavior, JDBC provides the method

    setAutoCommit(false)to set the transaction mode on theConnectionobject.

    To change the implicit behavior of "auto commit,"

    provides the method setAutoCommit(false)to set the tmode on the Connectionobject.

    Page 205, code in Listing 9.3, remove following lines

    if (stmt != null)

    stmt.close();

    Page 208, step 2

    c:\styejb\examples>startPointbase.batc:\styejb\examples>setEnvWebLogic.bat c:\styejb\examples>setEnvWebLogic.batc:\styejb\examples>startPointbase.batPage 209, step 5

    In the left pane, expand Services > JDBC. In the left pane, expandmydomain > Services > JD

    Page 209, step 6

    In the left pane, expand Services > JDBC. In the left pane, expandmydomain > Services > JD

    Page 209, step 6

    For Pool Name, enter styejbPoolFigure 9.8 shows the corresponding screen shot.

    For Pool Name, enter styejbPoolSelect Emulate Two-Phase Commit for non-XA driver

    Figure 9.8 shows the corresponding screen shot.

  • 8/12/2019 EJB 21 days

    10/22

    Pages 211 and 212, code at bottom and top of pages

    Trying to drop table STUDENTS...

    Table STUDENTS is created...

    Trying to drop table DAY09_STUDENTS...Table DAY09_STUDENTS is created...

    Page 216, paragraph in "Exercises" section

    Modify the day's example to perform few batch updates on theStudenttable. Modify the day's example to perform few batch updDAY09_STUDENTStable.Page 221, code snippet near top of page

    create table students (student_id varchar(64); first_name varchar(64); last_name varchar(64); address varchar(64));

    create table students (student_id varchar(12) NOT NULL; first_name varchar(15); last_name varchar(15); address varchar(164));

    Page 221, paragraph following code snippet

    In the case of the JBoss server, we'll use the HyperSonicdatabase.

    In the case of the JBoss server, we'll use the Hypdatabase. Note that the database tables were alrein Day 9.

    Page 242, remove step 1 at top of page and renumbersubsequent steps

    Insert text before new step 2

    The preceding step starts the PointBase database well.

    Page 242, insert text before second step 2

    The preceding step starts the default Hypersonic databaseserver as well.

    Page 253, first paragraph after Listing 11.3

    Note that the ejbCreate()method returns NULL. Note that the ejbCreate()method returns null.

  • 8/12/2019 EJB 21 days

    11/22

    Page 256, "TheWHEREClause" section

    TheWHEREclause consists of a conditional expression thatis used to select or values that satisfy the expression.Therefore, theWHEREclause restricts the results of thequery.

    TheWHEREclause consists of a conditional expressused to select objectsor values that satisfy the Therefore, theWHEREclause restricts the results query.The following sections discuss the various constructs that can be used in the WHEREclause.

    Page 266, first set of steps in "Running the Example"section, remove step 1 and renumber subsequent steps

    1. Start WebLogic Server in a new command window as follows. 1. Start WebLogic Server and PointBase database snew command window as follows.

    Page 292, second paragraph in note

    Instance bis composed of instance cand instance discomposed of c. Instance bis composed of instance cand instancecomposed of d.Page 298, Listing 12.15

    ClientLineItem item

    =(ClientLineItem)havax.rmi.PortableRemoteObject.narrow

    ClientLineItem item =(ClientLineItem)it.next();

    Page 299, last line before "Running the Example" section

    C:\styejb\examples\day 11>buildJboss.bat C:\styejb\examples\day 12>buildJboss.batPage 299, first set of steps in "Running the Example"section, remove step 1 and renumber subsequent steps

    1. Start WebLogic Server in a new command window as follows: 1. Start WebLogic Server and PointBase database snew command window as follows.

    Page 299, last paragraph on page

    The JBoss server automatically starts the default HyperSonicdatabase.

    The JBoss server automatically starts the default database.

  • 8/12/2019 EJB 21 days

    12/22

  • 8/12/2019 EJB 21 days

    13/22

    UserManager

    day16.UserManagerHome

    day16.UserManagerBean

    day16.UserManagerBean

    Stateful

    Bean

    jms/ConnectionFactory

    javax.jms.QueueConnectionFactory

  • 8/12/2019 EJB 21 days

    14/22

    jdbc/styejbDB

  • 8/12/2019 EJB 21 days

    15/22

    UserManager

    jms/ConnectionFactory

    javax.jms.QueueConnectionFactory

  • 8/12/2019 EJB 21 days

    16/22

    RegistrarQ

  • 8/12/2019 EJB 21 days

    17/22

    RegistrarMDB

    queue/RegistrarQ

  • 8/12/2019 EJB 21 days

    18/22

    the examples/day21folder for detailed instructions.Page 533, remove step 1 and renumber subsequent steps

    1. Start WebLogic server and PointBase server in a newcommand window as follows:

    Page 536, step 2

    The remaining steps are similar to those listed for WebLogicServer's steps 4through 7.

    The remaining steps are similar to those listed fServer's steps 3through 6.

    Page 538

    In addition, add a Collection getStudentEntrollments()method tothe Studentcomponent. This method invokes the methodCollection findStudentEnrollment(String studentId)of the Enrollmentcomponent.

    In addition, add a Collection getStudentEntrollments()mthe StudentFacadecomponent. This method invokes theCollection findStudentEnrollments(String studentId)of thecomponent.

    Page 541, insert new text immediately following chaptertitle

    Before you start, we assume you already downloadeexamples from the Web site http://www.samspublishing.cunzipped the downloaded file into the c:\directorwill create the examples under the directory C:\stFor detailed and up-to-date information, please r

    document Setup Weblogic.pdfin the examplesdirectory.

    Page 541, paragraph following "Downloading and InstallingWebLogic 7.0"

    BEA provides you with a fully functional trial version ofthis server for 30days (from the installation date), whichprovides ample time for running the examples within the bookat the suggested pace of 21 days.

    BEA provides you with a fully functional trial vethis server for 90days (from the installation daprovides ample time for running the examples withat the suggested pace of 21 days.

    Page 542, replace numbered steps1. Double-click on the downloaded file net_platformthe Installer will prompt you with the Welcome winon the Next button to proceed.

    2. When the BEA License Agreement window appears, you if you agree with the terms of the License. Pthe license and select the Yes radio button if you

  • 8/12/2019 EJB 21 days

    19/22

    the terms of the license. Then click the Next but

    3. Now you will be presented with a window to sel

    you want to choose your BEA home directory. Becauinstallation can be used to install multiple produBEA, the BEA home directory is used as a high levfor all other products. Go ahead and select the ddirectory C:\beaand click on the Next button. If yselect another directory, feel free to do so, but that you have enough disk space on the selected d

    4. Now you can choose the installation type. Becauoffers multiple products to try within the same in

    the typical installation will install all these pwhich requires about 430 MB. Choose Custom Instalclick the Next button.

    5. In the Choose Components window (see Figure A.the first WebLogic Server product, which includes three components. Unselect all other components. Next button to proceed.

    ***Insert Figure A.1 Afig01***

    Figure A.1

    Components selection dialog.

    6. Now you need to tell the Installer where you wtemporary files to be stored (in c:\temp) and if yoInstaller to get rid of them after the installaticomplete. If you want The Installer to remove the

    temporary files, choose this option. Otherwise, ychoose to delete the files after the installation complete. Click the Next button.

    7. Now the installation process will start downlofiles from BEA's site. Make sure that you are stito the Internet at this time. Be patient: This prtake 45 minutes if you are using a DSL connection KB/sec).

  • 8/12/2019 EJB 21 days

    20/22

    8. The next screen will automatically appear (see A.2). Now you need to choose the WebLogic server directory. This directory is of prime importance

    successfully run the examples in the book. Enterc:\bea]weblogic701and then click the Next button. Ifdecided to change the BEA home directory in step to use subdirectory weblogic701. (Notice the Instsuggests choosing weblogic700 as the default.)

    ***Insert Figure A.2 Afig02***

    Figure A.2

    Product Directory dialog.

    9. Now the Installer will install the binary filedownloaded archives in the temporary directory. Thbe faster than the previous step.

    10. After the installation is complete, select thConfiguration Wizard to Create My Application Domthen click the Next button.

    Page 542, first paragraph after "Configuring WebLogic 7.0"

    When the installation is complete, you must configure theWebLogic Server environment. Front the Start menu, selectPrograms, BEA WebLogic Platform 7.0, and choose theConfiguration Wizard application. You'll be presented withthe screen in Figure A.1.

    Now you must configure the Weblogic server. Scrolthe template selections until you find WLS Domainselect it. Choose mydomain, then click Next to prYou'll be presented with the screen in Figure A.3

    Pages 543 through 545, renumber figures and figure

    references by two

  • 8/12/2019 EJB 21 days

    21/22

    Page 544, last sentence

    Now you can choose End Configuration Wizard, and click theNextbutton to finish. Now you can choose End Configuration Wizard, and Donebutton to finish.

    Page 545, first paragraph

    After you've entered your server's configuration parametersin the setEnvWeblogic.batfile located in the examples' rootdirectory (as per the instructions contained in README.txtlocated in the same directory), you can start running theexamples provided with this book.

    After you've entered your server's configuration in the Setup_WebLogic.batfile located in the exampledirectory (as per the instructions contained in exlocated in the same directory), you can start runnexamples provided with this book.

    Page 547, insert new text immediately following chaptertitle

    Before you start, we assume you already downloadeexamples from the Web site http://www.samspublishing.cunzipped the downloaded file into the c:\directorwill create the examples under the directory c:\stFor detailed and up-to-date information, please rSetup_JBoss.pdfdocument in the examplesdirectory.

    Page 547, first paragraph

    JBoss is an open-source, J2EE-compliantapplication server,which is currently distributed for free. JBoss is an open-source, J2EEapplication server, currently distributed under LGPL license.

    Page 548, add to end of second Note After downloading, install it under the directoryThis directory will be your JAVA_HOME.

    Page 548, new text for "Configuring JBoss 3.0" section After the installation is complete, you must editconfiguration parameters before you start running server. In order to run the examples successfullyto open setEnvJBoss.bat, in directory C:\styejb/example

    a text editor (such as Notepad). Edit the first fwith the following information (shown in bold):

    set JBOSS_HOME=c:\jboss3.0.1set JAVA_HOME=c:java131set STYEJB_HOME=c:\styejb\examples

    After making these changes, save the file and exieditor. If you have already followed our recommen

  • 8/12/2019 EJB 21 days

    22/22

    the preceding steps, you probably don't need to chanything in these settings. If you have chosen diinstallation directories, you need to set appropr

    for the JBOSS_HOME, JAVA_HOME, and STYEJB_HOMEenvironmvariables.

    Page 549, replace text in "Running and Testing JBoss 3.0" To run and test JBoss, open a command window and following commands:

    c:\>cd styejb\examplesc:\styejb\examples>setEnvJBoss.batc:\styejb\examples>startJBoss.batWhen JBoss server starts, it will print a message

    14:23:51,532 INFO [Server] JBoss (MX MicroKernel) [3.0.1

    Date:200208122231] Started in 0m:23s:3ms

    Now open your browser and enter the URLhttp://localhost:8080/jmx-console. This should display similar to the JBoss Console in Figure B.1.

    Congratulations! Now the setup is complete and you

    to run the examples in the book.

    Page 560, second paragraph

    The class also contains the operations addCoursesandgetCourses.

    The class also contains the operations addCourses,and empty.

    This errata sheet is intended to provide updated technical information. Spelling and grammar misprints are uduring the reprint process, but are not listed on this errata sheet.