Oracle Database Programming Using Java and Web Services Oracle Database Related Book Titles:
Oracle 9iR2 Data Warehousing, Hobbs, et al, ISBN: 1-55558-287-7, 2004
Oracle 10g Data Warehousing, Hobbs, et al, ISBN 1-55558-322-9, 2004
Oracle High Performance Tuning for 9i and 10g, Gavin Powell, ISBN: 1-55558-305-9, 2004
Oracle SQL Jumpstart with Examples, Gavin Powell, ISBN: 1-55558-323-7, 2005
Implementing Database Security and Auditing, Ben Natan, ISBN 1-55558-334-2, 2005
Oracle Real Applications Clusters, Murali Vallath, ISBN: 1-55558-288-5, 2004
Oracle 10g RAC Grid, Services & Clustering, Murali Vallath, ISBN 1-55558-321-0, 2006
For more information or to order these and other Digital Press titles, please visit our website at www.books.elsevier.com/digitalpress! At www.books.elsevier.com/digitalpress you can: •Join the Digital Press Email Service and have news about our books delivered right to your desktop •Read the latest news on titles •Sample chapters on featured titles for free •Question our expert authors and editors •Download free software to accompany select texts Oracle Database Programming Using Java and Web Services
Kuassi Mensah
AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Elsevier Digital Press 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK
Copyright © 2006. Elsevier, Inc.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: [email protected]. You may also complete your request on-line via the Elsevier homepage (http://elsevier.com), by selecting “Customer Support” and then “Obtaining Permissions.”
Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible.
Library of Congress Cataloging-in-Publication Data Application Submitted.
ISBN 13: 978 1-55558-329-3 ISBN 10: 1-55558-329-6
British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library.
For information on all Elsevier Digital Press publications visit our Web site at www.books.elsevier.com
06 07 08 09 10 9 8 7 6 5 4 3 2 1
Printed in the United States of America
To the memory of my mother and my father.
To my loving wife, Gabrielle, and my wonderful sons, Gareth and Kenneth. This Page Intentionally Left Blank Contents
Foreword xix
Preface xxi
Introduction xxv Server-Side Database Programming (Stored Procedures) xxvi Database Programming Using JDBC, SQLJ, and JPublisher xxviii Database Programming with Web Services xxix Putting Everything Together: The “All-You-Can-Eat Buffet” xxx
Acknowledgments xxxi
Part I: Java in the Database 1
1 Stored Procedures as Database Programming Model 3 1.1 Rationale for Stored Procedures 3 1.1.1 Simplifying Database Programming 3 1.1.2 Centrally Managed Data Logic 4 1.1.3 Performance: Run JDBC Applications Faster in the Database 4 1.1.4 Encapsulation 10 1.1.5 Security: Advanced Data Access Control 10 1.1.6 Resource Optimization 11 1.1.7 Low-Cost Deployment 11 1.1.8 Fully Utilize Database Capabilities 11 1.2 Obstacles to the Adoption of Stored Procedures 11
vii viii Contents
1.2.1 Lack of Portability across RDBMS Vendors 12 1.2.2 Scalability 12 1.2.3 Maintenance and Resilience to Schema Change 12 1.2.4 Hard to Debug 13 1.2.5 Weak Support for Complex Types 13 1.3 Languages for Stored Procedures 13 1.3.1 Proprietary Languages 13 1.3.2 Java for Stored Procedures 14 1.3.3 .NET Languages 18 1.4 PL/SQL or Java 19 1.4.1 PL/SQL and Java! 21
2 OracleJVM: Under the Hood 23 2.1 Design Goals and Architecture 23 2.1.1 Tight Integration with the RDBMS 24 2.1.2 J2SE Compatibility 24 2.1.3 How Is Java Stored in the Database? 25 2.1.4 Class Sharing 30 2.1.5 Interapplication Isolation (JSR 121) 32 2.1.6 Contrasting OracleJVM with the JDK VM 32 2.1.7 Resource Control 36 2.1.8 SQL Data Access from Java in the Database 37 2.1.9 DBMS_JAVA: The All-Purpose Tool for Administering OracleJVM 39 2.2 Java Memory Management 41 2.2.1 Key Memory Structures of the Oracle Database 42 2.2.2 Java Memory Allocation Techniques 43 2.2.3 Garbage Collection Techniques 44 2.2.4 Java Memory Areas 46 2.2.5 Shared Servers versus Dedicated Processes 49 2.2.6 The Javapool 52 2.2.7 Top-Level Calls and Recursive Calls 53 2.2.8 State Preservation across Calls and End-of-Call Migration 54 2.2.9 End-of-Call, VM Termination, and Session Termination 58 2.3 Security in OracleJVM 60 2.3.1 User Authentication 60 2.3.2 Database-Schema Security 61 2.3.3 Resolver Specification and Class-Resolution Security 62 2.3.4 Login-User and Effective-User Security 64 2.3.5 Java 2 Security 66 2.3.6 Java 2 Security in OracleJVM 68 Contents ix
2.3.7 OracleJVM Security Best Practices 79 2.3.8 JNI Calls 80 2.4 Java VM Life Cycle 80 2.4.1 OracleJVM Install, Uninstall, and Reinstall 80 2.4.2 Java VM Initialization and Termination 81 2.5 Java Execution in the Database 81 2.5.1 The OracleJVM Interpreter 82 2.6 The Native Java Compiler (NCOMP) 84 2.6.1 What Is NCOMP? 84 2.6.2 Requirements and Design Choices 85 2.6.3 The NCOMP Process 88 2.6.4 The NCOMP Command 88 2.6.5 The STATUSNC Command 92 2.6.6 Dumping Java Classes with NCOMP 96 2.6.7 NCOMP Configuration and Planning 96 2.6.8 NCOMP Performance Tips, Improper Use, and Troubleshooting 100 2.7 Conclusion 103
3 Developing and Running Java in the Database 105 3.1 Developing Java in the Database 105 3.1.1 Turning JDBC Applications into Java Stored Procedures 105 3.1.2 Creating or Loading Java in the Database 106 3.1.3 Removing Java Sources, Classes, and Resources from the Database 119 3.1.4 Setting/Querying Environment Variable and System Properties 120 3.1.5 The Java Compiler within the Database 124 3.2 Turning Java in the Database into Stored Procedures 126 3.2.1 Call Spec Types 127 3.3 Mapping SQL and PL/SQL Types to/from Java Types 135 3.3.1 Mapping Matrix 135 3.3.2 Code Segments for Mapping 140 3.4 Invoking Java in the Database 207 3.4.1 Setup 207 3.4.2 Invoking Java in the Database Using OJVMJAVA 212 3.4.3 Invoking Java in the Database through the PL/SQL Wrapper 214 3.4.4 Invoking Java in the Database through Client-side Stub 216 3.4.5 Errors and Exceptions Handling 221
Contents x Contents
3.5 Managing Java in the Database 227 3.5.1 Java Audit 227 3.5.2 Oracle Enterprise Manager (Database Control) Support for Java in the Database 228
4 Pragmatic Applications Using Java in the Database 231 4.1 CNXO: Secure Credit Card Processing with Oracle and JSSE 231 4.2 Using J2EE and Java in the Database Together 239 4.2.1 Auto-generating Primary Keys for BMP Entity Beans 239 4.2.2 Calling-out EJB from OracleJVM 241 4.2.3 HTTP Call-Out: The Poor Man’s Cache Invalidation 249 4.2.4 JMS over Streams/AQ in the Database 253 4.3 JDBC Call-Out to Non-Oracle Databases 260 4.3.1 Description and Rationales 260 4.3.2 How Does It Work? 260 4.4 SAP Java Connector: Accessing the SAP System from the Oracle Database 268 4.5 Excel-like Expression Parser in the Database 277 4.5.1 Rationales for Custom Parsers in the Database 277 4.5.2 What Is the Mini-Parser? 278 4.5.3 Implementing the Mini-Parser 279
5 Database Scripting Using Non-Java Languages 287 5.1 Why Contemplate Non-Java Languages for the Database? 287 5.1.1 Common Language Runtime in RDBMS 288 5.1.2 Scripting Languages Support in RDBMS 288 5.2 Database Scripting with OracleJVM—Just for Fun! 288 5.2.1 Proof of Concept #1: Running TCL (JACL) Scripts in the Database 289 5.2.2 Proof of Concept #2: Running Jython (Python) in the Database 297 5.2.3 Proof of Concept #3: Running Kawa (Scheme) in the Database 299 5.2.4 Proof of Concept #4: Running Groovy in the Database 302
Part II: Java Persistence and Java SQL Data Access 317 Database Programming with Oracle JDBC 318 Contents xi
6 Introducing the JDBC Technology and Oracle’s Implementation 319 6.1 JDBC Primer 319 6.1.1 First Steps in JDBC 319 6.1.2 JDBC within J2SE and J2EE Environments 324 6.2 Overview of JDBC Specifications 325 6.2.1 Overview of JDBC 1.22 Specification (Where Things Started!) 325 6.2.2 Overview of JDBC 2.0 Specification (A Major Spec!) 326 6.2.3 Overview of JDBC 3.0 Specification 328 6.2.4 Overview of Upcoming JDBC 4.0 Specification 330 6.2.5 JDBC Standards Support in the Oracle JDBC Drivers 331 6.3 Architecture and Packaging of Oracle JDBC Drivers 334 6.3.1 Rearchitected Oracle JDBC Drivers 334 6.3.2 Packaging of Oracle JDBC Drivers 336 6.3.3 Features Differences Between Driver Types 340 6.3.4 JDBC Drivers and Database Interoperability 343
7 URL, DataSource, Connection, and Statements 345 7.1 JDBC URL 345 7.2 DataSources 348 7.2.1 The OracleDataSource 349 7.2.2 DataSources and JNDI 351 7.3 Connections and Connection Services 357 7.3.1 JDBC Connections and Oracle Extensions 357 7.3.2 Connection Caching: Implicit Connection Cache 358 7.3.3 The Connection Cache Manager 383 7.3.4 RAC Events and Fast Application Notification 391 7.3.5 High Availability: Fast Connection Failover 400 7.3.6 Scalability: Connection Load Balancing 406 7.3.7 JDBC Support for Transparent Application Fail-over 409 7.3.8 Proxy Authentication 415 7.3.9 Connection Wrapping 422 7.3.10 JDBC Connections in Grid Environment 424 7.4 JDBC Statements and Oracle Extensions 427 7.4.1 JDBC Statement Types 427 7.4.2 Statement 428 7.4.3 PreparedStatement 432 7.4.4 CallableStatement (Calling Stored Procedures) 444
Contents xii Contents
7.4.5 Retrieval of Auto-Generated Keys and DML with Returning 454 7.4.6 Statement Caching 457 7.4.7 DML Batching 466
8 SQL Data Access and Manipulation 471 8.1 Key Metadata in JDBC 471 8.1.1 DatabaseMetaData: OracleDatabaseMetaData 471 8.1.2 ResultSetMetaData: OracleResultSetMetaData 473 8.1.3 ParameterMetaData 476 8.1.4 StructMetaData 478 8.2 Manipulating Oracle Data Types with JDBC 479 8.2.1 Manipulating SQL Null Data 488 8.2.2 Manipulating Character Data Types 489 8.2.3 Oracle JDBC Support for Number Data Types 492 8.2.4 JDBC Support for Long and Raw Data Types 496 8.2.5 JDBC Support for SQL Datetime Data Types 500 8.2.6 JDBC Support for LOB Datatypes 505 8.2.7 JDBC Support for ROWID 517 8.2.8 JDBC Support for OPAQUE Type 518 8.2.9 JDBC Support for XMLType 518 8.2.10 JDBC Support for SQL Object Types and References Types 522 8.2.11 JDBC Support for User-Defined Collections 539 8.2.12 JDBC Support for Spatial Types 553 8.2.13 Unsupported Types 553 8.3 Result Set Support in Oracle JDBC 554 8.3.1 The Result Set API in a Nutshell 554 8.3.2 The Oracle Result Set Interface 558 8.3.3 Oracle JDBC Support for Scrollable Result Sets 559 8.3.4 Oracle JDBC Support for Updatable Result Sets 565 8.3.5 Prefetching and Auto Refresh 573 8.3.6 Changes Detection and Visibility 575 8.4 RowSet 577 8.4.1 Introducing the RowSet API 577 8.4.2 JDBCRowSet and OracleJDBCRowSet 579 8.4.3 CachedRowSet and OracleCachedRowSet 581 8.4.4 WebRowSet and OracleWebRowSet 587 8.4.5 FilteredRowSet and OracleFilteredRowSet 610 8.4.6 JoinRowSet and OracleJoinRowSet 617 8.5 Conclusion 623 Contents xiii
9 JDBC Quality of Services and Best Practices 625 9.1 Transaction Services 625 9.1.1 Transactions 625 9.1.2 AutoCommit 625 9.1.3 Transaction Isolation Levels 626 9.1.4 Transaction SavePoint Support 627 9.1.5 Global/Distributed Transaction 632 9.1.6 Connection Sharing between Local and Global Transactions 640 9.2 Security Services 642 9.2.1 Oracle JDBC Support for SSL 642 9.3 Tips and Best Practices 657 9.3.1 End-to-End Tracing 658 9.3.2 Common Errors 662 9.3.3 Optimizing Result Set Retrieval 662 9.3.4 Logging Service 663 9.4 Conclusion 666
Part III: Oracle Database Programming with SQLJ 667
10 Introducing the SQLJ Technology and Oracle’s Implementation 669 10.1 Overview 669 10.1.1 What Is SQLJ? 669 10.1.2 Why SQLJ? 671 10.1.3 The Oracle SQLJ Translator 676 10.1.4 The Oracle SQLJ Runtime 689 10.1.5 Environment Setup 690 10.1.6 SQLJ Primer 692 10.2 SQLJ in the Database 693
11 The SQLJ Language and Oracle Extensions 701 11.1 Declaration Statements 701 11.1.1 Import Statements 701 11.1.2 Connection Contexts 701 11.1.3 Execution Contexts 712 11.1.4 Iterators 717 11.1.5 IMPLEMENTS Clause in Context Declarations 726
Contents xiv Contents
11.1.6 WITH Clause in Context Declarations 728 11.2 Executable Statements 730 11.2.1 Statement Clauses 731 11.2.2 Assignment Clauses 737 11.2.3 Dynamic SQL 740 11.3 Expressions in SQLJ 744 11.3.1 Context and Result Expressions 746 11.3.2 Expressions Evaluation 748 11.4 Interoperability: Using SQLJ and JDBC Together 748 11.4.1 JDBC to SQLJ Interoperability 748 11.4.2 SQLJ to JDBC Interoperability 751 11.5 Conclusion 753
12 SQLJ Data Access and Best Practices 755 12.1 Manipulating Oracle SQL and PL/SQL Data Types with SQLJ 755 12.1.1 Oracle SQLJ Type-Mapping Summary 755 12.1.2 Column Definitions 759 12.1.3 Manipulating SQL Null Data with SQLJ 760 12.1.4 Manipulating Character Data Types with SQLJ 762 12.1.5 Oracle SQLJ Support for Number Data Types 764 12.1.6 SQLJ Streams, LONG, and RAW Data Types 765 12.1.7 SQLJ Support for SQL Datetime Data Types 772 12.1.8 SQLJ Support for SQL LOB Data Types 774 12.1.9 SQLJ Support for Oracle SQL ROWID 780 12.1.10 SQLJ Support for OPAQUE Types 782 12.1.11 SQLJ Support for SQL Object Types and SQL References Types 783 12.1.12 Serialized Java Objects 796 12.1.13 SQLJ Support for User-Defined SQL Collections 807 12.1.14 PL/SQL Associative Array 809 12.1.15 Unsupported Types 809 12.2 SQLJ Best Practices 811 12.2.1 Row Prefetch 811 12.2.2 Statement Caching 811 12.2.3 Update Batching 812 12.3 Conclusion 813 Contents xv
Part IV: Oracle Database Programming with JPublisher 815
13 Abridged Oracle JPublisher 817 13.1 Why JPublisher? 817 13.2 Overview 819 13.2.1 Environment Requirements 821 13.2.2 JPublisher Options 829 13.3 JPublisher In Action 849 13.3.1 User-Defined SQL Object Types 850 13.3.2 SQL Object Reference Types (REF types) 854 13.3.3 REF Cursor Types and Subclassing 855 13.3.4 User-Defined SQL Collection Types 858 13.3.5 User-Defined OPAQUE Types 862 13.3.6 XMLType 863 13.3.7 PL/SQL Conversion Functions 865 13.3.8 PL/SQL RECORD Types 866 13.3.9 PL/SQL Table or Scalar Index-by-Table 866 13.3.10 Oracle Streams AQ 872 13.3.11 Java in the Database 873 13.4 Conclusion 873
Part V: Programming the Oracle Database with Web Services 875
14 Web Services and SOA for DBA, Data Architects, and Others 879 14.1 Web Services 101 879 14.1.1 Core Web Services Technologies 880 14.2 Service-Oriented Architecture (SOA): The Bigger Picture 890 14.3 Conclusion 897
15 Database as Web Services Provider Service 899 15.1 Rationales for Database as Web Services Provider 899 15.2 How Does Database as Web Services Provider Work? 899 15.2.1 Implementation and Packaging 900 15.2.2 How Does Oracle Database as Web Services Provider Work? 901
Contents xvi Contents
15.2.3 Web Services and SOA Features in Oracle Application Server 10.1.3 902 15.3 Turning Oracle Database Functionality into Web Services 905 15.3.1 Type Conversions and Result Set Representation 905 15.3.2 Setting up the Oracle AS OC4J for Database as Web Services Provider 907 15.3.3 Assembling PL/SQL Web Services Using JDeveloper Wizard 908 15.4 Assembling Database Web Services Using the Command-Line Tool 925 15.4.1 Assembling PL/SQL Web Services Using Web Services Assembler 928 15.4.2 Assembling Java in the Database as a Web Service 937 15.4.3 Assembling SQL Queries or SQL DML Statements as Web Services 939 15.4.4 Assembling Oracle Streams AQ as Web Services 940 15.5 Data Type Restrictions 942 15.6 Conclusion 943
16 Database as Web Services Consumer 945 16.1 Rationales for Database as Web Services Consumer 945 16.2 How Database as Web Services Consumer Works 946 16.2.1 The Software Pieces 946 16.2.2 The Required Steps 947 16.3 Turning Your Oracle Database into a Web Service Consumer 954 16.3.1 Ensure That Java Is Installed in the Database 954 16.3.2 Installing JPublisher on the Client Machine 955 16.3.3 Installing the Web Services Call-Out Utility in Your Database 956 16.4 Database Web Services Call-Out Samples 959 16.4.1 Calling Out Google Search Web Service 959 16.4.2 Calling Out the Phone Verifier Web Service 962 16.5 Conclusion 964
Part VI: Putting Everything Together 965
17 360-Degree Programming the Oracle Database 967 17.1 TECSIS Systems: Custom Enterprise Integration Framework 968 17.1.1 About the Company 968 Contents xvii
17.1.2 About the Application 968 17.1.3 Our Business and Technical Requirements 969 17.1.4 The Architecture of the Integration Framework 970 17.1.5 The Complete Picture 972 17.1.6 Conclusion 974 17.2 Oracle interMedia 974 17.2.1 What Is Oracle interMedia? 974 17.2.2 How Does It Work? 975 17.2.3 Rationales for Storing Media Data in the Database 976 17.2.4 interMedia Powered by the Oracle Database Extensibility Framework 978 17.2.5 interMedia Powered by Java in the Database 978 17.2.6 Developing Feature-Rich Multimedia Applications Using interMedia 981 17.3 British Columbia: Online Corporate Registration 993 17.3.1 Corporate Online: Background 994 17.3.2 How It Works 994 17.3.3 Architecture: Requirements and Design 994 17.3.4 Messaging across Tiers 1004 17.3.5 Future Work 1009 17.3.6 Conclusion 1009 17.4 Information Retrieval Using Oracle Text 1009 17.4.1 What Is Oracle Text? 1010 17.4.2 Why Java in the Database? 1011 17.4.3 Technical Features 1012 17.4.4 Benefits of an Integrated Search Capability 1016 17.4.5 Yapa 1017 17.4.6 Conclusion 1028 17.5 Database-Driven Content Management System (DBPrism CMS) 1028 17.5.1 DBPRISM CMS: Key Features and Benefits 1029 17.5.2 The Architecture of DBPrism CMS 1030 17.5.3 DBPrism CMS Internals 1033 17.5.4 Extended Capabilities 1046 17.5.5 Text Searching 1049 17.5.6 Installing DBPRism CMS 1051 17.5.7 Future Work 1057 17.6 Conclusion 1058
Index 1059
Contents This Page Intentionally Left Blank Foreword
I spend the bulk of my time working with Oracle database software and, more to the point, with people who use this software. Over the last 18 years, I’ve worked on many projects—successful ones as well as complete failures—and if I were to encapsulate my experiences into a few broad state- ments, they would be