WebSphere Application Server V8.5 Liberty Profile JPA
by user
Comments
Transcript
WebSphere Application Server V8.5 Liberty Profile JPA
WebSphere Application Server V8.5 Liberty Profile JPA Lab 3: Liberty and JPA (DB2) Module 6 : Complete and test the JPA application with DB2 1 © Copyright International Business Machines Corporation 2012. All rights reserved. Trademarks • IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide. Worklight is a trademark registered in the United States to Worklight, an IBM company. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml. • Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. • Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. • Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates. • Other company products or service names may be trademarks or service marks of others. • This document may not be reproduced in whole or in part without the prior written permission of IBM. About IBM® • 2 See http://www.ibm.com/ibm/us/en/ © Copyright International Business Machines Corporation 2012. All rights reserved. About this module In module 1.2 you set up the database and server, in module 2.2 you configured the server, and in module 3.2 you created the application project and a database class In modules 4.2 and 5.2 you added the DBInteraction and CustomerCredit servlets In module 6.2 you will add two files index.html provides the browser interface for your web application persistence.xml provides database configuration information You will also deploy and test your application Prerequisites You must complete modules 1.2, 2.2, 3.2, 4.2 and 5.2 before you start this module 3 © Copyright International Business Machines Corporation 2012. All rights reserved. About index.html index.html provides the entry point to your JPA application It consists of an HTML table which you can use to provide data input for a customer's details Number Name Money The code also includes a submit button which triggers the CustomerCredit class, and leads to the database updates 4 © Copyright International Business Machines Corporation 2012. All rights reserved. Create a new web page 1 In Eclipse or RAD, expand the WebCustomerCredit project 2 Select the WebContent folder 3 Select Right-click → New → HTML file 4 Change the file name to index.html 5 Click Finish 5 © Copyright International Business Machines Corporation 2012. All rights reserved. Edit index.html 1 Select the Source view for the generated index.html file 2 Replace all the generated source in index.html with this HTML <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Customer Form</title> </head> <body> <H1>Customer Form</H1> <BR> <!--Form--> <FORM name="myForm" method="POST" action="CustomerCredit" style="text-align: center;"> <INPUT TYPE="SUBMIT" NAME="Create customer" VALUE="Create new customer" > <A href="javascript:submitForm()"></A> <BR> <TABLE align='center' border="8" bgcolor="#cccccc" bordercolor="#FFCC99"> <TBODY> <TR> <TH>Customer number<br></TH> <TD><INPUT name='customerNumber' type='text' value=></TD></TR> <TR> <TH>Customer name</TH> <TD><INPUT name='customerName' type='text' value=></TD></TR> <TR> <TH>Customer money</TH> <TD><INPUT name='customerMoney' type='text' value=></TD></TR> </TBODY> </TABLE> </FORM> </body> </html> 6 © Copyright International Business Machines Corporation 2012. All rights reserved. Review index.html 1 Make sure there are no errors or warnings 2 Save the index.html file 7 © Copyright International Business Machines Corporation 2012. All rights reserved. About persistence.xml persistence.xml provides the JPA configuration that the server will use It includes details about the persistence unit and data source, as well as some transactional properties 8 © Copyright International Business Machines Corporation 2012. All rights reserved. Edit persistence.xml 1 Expand WebCustomerCredit → Java Resources → src → META-INF and open persistence.xml 2 Select the Source view 3 Replace all the source in persistence.xml with this XML and save the file <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="JPAService"> <jta-data-source>java:comp/env/jdbc/DB2Connection</jta-data-source> <class>my.jpa.Account</class> <!-- exclude-unlisted-classes>true</exclude-unlisted-classes --> <properties> <property name="openjpa.LockTimeout" value="30000" /> <property name="openjpa.jdbc.TransactionIsolation" value="read-committed" /> <property name="openjpa.Log" value="none" /> <property name="openjpa.jdbc.UpdateManager" value="operation-order" /> </properties> </persistence-unit> </persistence> 9 © Copyright International Business Machines Corporation 2012. All rights reserved. Review persistence.xml The jta-data-source value matches the data source you configured for the server in module 2.2 The class value matches the Account class you generated in module 3.2 The persistence unit matches the @PersistenceUnit unitName you configured when you created the CustomerCredit class in module 5.2 1 Save the file 10 © Copyright International Business Machines Corporation 2012. All rights reserved. Run the application on the Liberty server 1 Select index.html 2 Drag it onto the WebSphere Application Server V8.5 Liberty Profile 11 © Copyright International Business Machines Corporation 2012. All rights reserved. Verify server start 1 Eclipse will automatically launch a browser window for the application, and start the server 2 You should see the WebCustomerCredit application start in the console 12 © Copyright International Business Machines Corporation 2012. All rights reserved. Enter data 1 In the Customer Form, enter (any) values for the customer id, name, and money, some examples are shown 2 Click Create new customer 13 © Copyright International Business Machines Corporation 2012. All rights reserved. Lab completed 1 You should now see the updated database contents 14 © Copyright International Business Machines Corporation 2012. All rights reserved. Lab Review In module 1.2 you set up the database and created a server In module 2.2 you configured the server In module 3.2 you created the application project and a database class In modules 4.2 and 5.2 you added the DBInteraction and CustomerCredit servlets In module 6.2 you added two files, index.html for the user interface and persistence.xml for the database interface You deployed and tested the whole application Your JPA application is now complete 15 © Copyright International Business Machines Corporation 2012. All rights reserved. Thank you 16 © Copyright International Business Machines Corporation 2012. All rights reserved. Appendix 17 © Copyright International Business Machines Corporation 2012. All rights reserved. DB2 Errors DB2 Errors are normally wrapped in a javax.transaction.RollbackException Look into the nested errors in the console to find the underlying cause, for example you will see this error if you enter the same Customer Id twice One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "DB2ADMIN.ACCOUNTS" from having duplicate values for the index key.. SQLCODE=-803, SQLSTATE=23505, DRIVER=3.62.56. And you will see this error if the DB2ADMIN user does not have authorization to create new data in the table "DB2ADMIN" does not have the required authorization or privilege to perform operation "INSERT" on object "DB2ADMIN.ACCOUNTS".. SQLCODE=-551, SQLSTATE=42501, DRIVER=3.62.56 18 © Copyright International Business Machines Corporation 2012. All rights reserved. Debugging If your application does not behave as expected, you can enable JPA and container trace in the server using the logging feature 1 Select Server Configuration and click Add 2 Select Logging 3 Set a trace specification of *=info=enabled:Injection=all:JPA=all:webcontainer=all:RRA=all 4 Logs are found under <Liberty_server_dir>/usr/server/<serverName>/logs 19 © Copyright International Business Machines Corporation 2012. All rights reserved. Suggested enhancements In this lab, you have generated a database application very quickly: here are some enhancements you can make if you have more time 1 In DBInteractions, there are several places where the code uses catch (Exception e) You can modify the code to catch and process different exceptions separately 2 In DBInteractions and CustomerCredit, there are several new methods with no Javadoc You can add comments to these methods based on their function 3 There is no validation in index.html or the CustomerCredit servlet You can add more checking functions, for example Verify that the “money” data entered is a number Prevent duplicate customer ids These requirements are already enforced in java or the database, but the errors users see could be improved 4 In the doPost method in the CustomerCredit servlet, there is very little logic 20 You can add more processing, for example Subtract 10% from the money deposited Include a button to return to index.html and enter new details Provide the ability to update details for an existing customer © Copyright International Business Machines Corporation 2012. All rights reserved.