Oracle Database Programming Using Java and Web Services Oracle Database Related Book Titles
Total Page:16
File Type:pdf, Size:1020Kb
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