Chapter 1. Stored Procedures as Database Programming Model

Although stored procedures have been around for more than a decade now, there still is a recurrent, almost ideological, debate on this programming model. Although it takes position in favor of stored procedures, the intent of this book is not to fuel this discussion but to elaborate on the benefits, assuming that there are situations where stored procedures are the right design choices. In this chapter, I will discuss the rationales for stored procedures, the obstacles to their adoption, languages used for writing stored procedures, and proprietary procedural languages such as PL/SQL versus open standards languages such as Java.

Rationale for Stored Procedures

As database developers and database administrators (DBAs) already know, stored procedures allow the exploitation of capabilities of relational database management systems (RDBMSs) to their fullest extent. The motivations to use stored procedures range from simplifying database programming to advanced data access to performance to centrally managed data logic and to optimizing network traffic.

Simplifying Database Programming

Procedural programming (also known as modular programming), as the name indicates, is based on the concepts of modules (also known as packages) and procedures (also known as functions, routines, subroutines, or methods). Each module consists of one or more procedures. The resulting code is simple and easier to read, debug, and maintain. Stored procedures are a mix of procedural code and SQL. The runtime of stored procedures is usually tightly integrated with the RDBMS but could also be loosely coupled, as an external runtime. Procedural languages include vendors’ extensions to SQL, such as PL/SQL, as well as BASIC/Visual BASIC, COBOL, Pascal, C/C++, C#, Perl, and Java.

Centrally Managed Data Logic

By centralizing data logic, you can share it across all database projects, thereby avoiding code duplication and allowing flexible application development.

Avoids Code Duplication

Stored procedures are written once, centralized, and not dispersed across applications. When the procedure is updated, all consumer applications will automatically pick up the new version at the next invocation.

Fosters Data Logic Sharing

Irrespective of their implementation language (e.g., proprietary, Java, 3GL), stored procedures are declared and known to the database catalog through their SQL signature. In the Oracle case, this is achieved via a PL/ SQL wrapper known as Call Spec. Through this PL/SQL wrapper, SQL, PL/SQL, Java in the database, thin clients (Web), rich clients (desktop), stand-alone Java, and middle-tier components[1] access the same, centrally managed data logic. For example, a stored procedure can be used to send a notification email when a new order is placed or to invalidate the middle-tier cache to notify data change (see “Poor Man’s Cache Invalidation” example in Chapter 4).

Implementation Transparency

Interfaces allow effective modularization/encapsulation and shield consumers from implementation details, allowing multiple implementations. By decoupling the call interface (i.e., Call Spec in Oracle’s vocabulary) from its actual implementation, the stored procedure may change over time from being written in PL/SQL to Java or the opposite, transparently to the requesters.

Performance: Run JDBC Applications Faster in the Database

Performance is one of the main motivations for using stored procedures. A few years ago, Oracle used PL/SQL stored procedures to optimize the performance of a benchmark version of the infamous J2EE Blueprints “PetStore”[2] application. This optimization prompted a heated debate in the Java/J2EE community. On the heels of this debate, Microsoft implemented and published the results of a .NET variant of the same benchmark, using—guess what?—stored procedures! The main criticism[3] was the lack of portability of PL/SQL or Transact SQL stored procedures across RDBMSs. Well, this is precisely the raison d’être of Java stored procedures.

The conclusion to derive from these experiences, as database programmers already know, is that stored procedures are the right design choice for efficient database programming. Stored procedures inherently incur minimal data movement, compared with a set of individual SQL statements that ship data outside the database. By processing data within the database (sorting, filtering) and returning just the results, stored procedures reduce network traffic and data movement. To cut to the chase, let’s compare the performance of a Java application used as a stand-alone Java database connectivity (JDBC) application deployed on a Java development kit (JDK) virtual machine (VM) versus the same code deployed as a Java stored procedure running in the database (this is, by the way, an illustration of the claim that you can reuse existing Java/JDBC applications, with minor changes, in the database). The following example will already give you an overview of the few steps involved in creating, compiling, publishing, and executing Java in the database.

Setup

Configuration:

A Pentium 4 M 1.80-GHz laptop, with 1 GB of RAM using Windows XP Professional Version 2002, Oracle Database 10g Release 1, and the associated JDBC drivers.

Create a table with a Varchar2, BLOB, and CLOB columns, using the following script (in a SQL*Plus session):

SQL> connect scott/tiger;
SQL> drop table basic_lob_table;
SQL> create table basic_lob_table (x varchar2 (30), b blob, c clob);

The Java Aplication

Example 1.1. TrimLob.java

/*
 * This sample shows basic BLOB/CLOB operations
 * It drops, creates, and populates table basic_lob_table
 * with columns of blob, clob data types in the database
 * Then fetches the rows and trim both LOB and CLOB
 */

// You need to import the java.sql package to use JDBC
import java.sql.*;

/*
 * You need to import the oracle.sql package to use
 * oracle.sql.BLOB
 */
import oracle.sql.*;

public class TrimLob
{
  public static void main (String args []) throws SQLException {
  Connection conn;
 /*
  * Where is your code running: in the database or outside?
  */
  if (System.getProperty("oracle.jserver.version") != null)
  {
  /*
   * You are in the database, already connected, use the default
   * connection
   */
  conn = DriverManager.getConnection("jdbc:default:connection:");
  }
  else
  {
  /*
   * You are not in the database, you need to connect to
   * the database
   */
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    conn =
          DriverManager.getConnection("jdbc:oracle:thin:", "scott",
           "tiger");
   }
   long t0,t1;
    /*
     * Turn auto commit off
     * (Required when using SELECT FOR UPDATE)
     */
     conn.setAutoCommit (false);
     t0=System.currentTimeMillis();
     // Create a Statement
     Statement stmt = conn.createStatement ();
     // Make sure the table is empty
     stmt.execute("delete from basic_lob_table");// sure we could use
 truncate
     stmt.execute("commit");

     // Populate the table
     stmt.execute ("insert into basic_lob_table values ('first', " +
                   "'010101010101010101010101010101', " +
                   "'one.two.three.four.five.six.seven')");
     stmt.execute ("insert into basic_lob_table values ('second', " +
                   "'0202020202020202020202020202020202020202', " +
                   "'two.three.four.five.six.seven.eight.nine.ten')");

    /*
     * Retreive LOBs and update contents (trim); this can be done by
 doing
     * "select ... for update".
     */
     ResultSet rset = stmt.executeQuery
                ("select * from basic_lob_table for update");

     while (rset.next ())
     {
       // Get the lobs
       BLOB blob = (BLOB) rset.getObject (2);
       CLOB clob = (CLOB) rset.getObject (3);

       // Show the original lengths of LOBs

        System.out.println ("Show the original lob length");
        System.out.println ("blob.length()="+blob.length());
        System.out.println ("clob.length()="+clob.length());

        // Truncate the lobs
        System.out.println ("Truncate LOBs to legnth = 6");
        blob.truncate (6);
        clob.truncate (6);

        // Show the lob length after truncate()
        System.out.println ("Show the lob length after truncate()");
        System.out.println ("blob.length()="+blob.length());
        System.out.println ("clob.length()="+clob.length());
      }

      // Close the ResultSet and Commit changes
      rset.close ();
      stmt.execute("commit");

      // Close the Statement
      stmt.close ();

      t1=System.currentTimeMillis();
      System.out.println ("====> Duration: "+(int)(t1-t0)+
  "Milliseconds");
      // Close the connection
      conn.close ();
    }
  }

Running the Java Application as a Stand-alone JDBC Application

Stand-alone JDBC applications run on JDK VM, against the database. For my test, the database, the JDBC driver, and application, all run on the same machine. The following steps compile the Java class and execute it:

javac TrimLob.java
java –classpath %CLASSPATH% TrimLob

Running the Java Application as a Java Stored Procedure

TrimLobSp.sql (contains Java source and SQL commands)
=============

connect scott/tiger;
create or replace java source named TrimLob as

rem
rem -> Insert here the above Trimlob.java here
rem

/

show errors;

alter java source TrimLob compile;

show errors;

create or replace procedure TrimLobSp as
 language java name 'TrimLob.main(java.lang.String[])';
/

show errors;
set serveroutput on
call dbms_java.set_output(50000);

call TrimLobSp();

Table 1.1 contrasts the performance of 10 invocations of the same Java code as stand-alone JDBC, and as Java stored procedure, on the same laptop, using exactly the same configuration (i.e., Oracle Database 10g Release 1 and its embedded OracleJVM).

Table 1.1. Performance Contrasts

Run#

Stand-alone JDBC

Java Stored Procedure

1st

570 ms

121 ms

2nd

241 ms

61 ms

3rd

240 ms

60 ms

4th

250 ms

50 ms

5th

230 ms

50 ms

6th

281 ms

50 ms

7th

280 ms

50 ms

8th

241 ms

50 ms

9th

250 ms

50 ms

10th

251 ms

50 ms

Although we cannot draw a universal conclusion, because of the elimination of network roundtrips and because it runs within the same address space as SQL, this JDBC application runs four to five times faster in the database than outside it. This example proves that, when appropriate, you can move Java/JDBC applications to the database and run them faster.

Encapsulation

Encapsulation is an object-oriented design principle that lets you structure an application into modules that hide data structures from outside view and also protect it from unauthorized access. Stored procedures allow building specialized modules, which can be tuned by domain specialists and DBAs, shielding consumers from the arcane data structure and SQL programming. Encapsulation also hides differences between RDBMSs by presenting the same call interface over different enterprise information systems (see “TECSIS System Use” case in Part VI).

Security: Advanced Data Access Control

Database-related applications that have explicit knowledge of database schema login and password may compromise the security of your system and may break upon schema change. You can enforce security as part of your design by using JDBC data sources that remove and defer the actual database and login information to deployment time, and, in addition, implement security policies in stored procedures (validate login information on each procedure call) and only allow users/apps to call these stored procedures. You can control database access use through customized, advanced, sophisticated data access logic that does CRUD (i.e., Create, Retrieve, Update, Delete) operations on tables while denying users direct access to these tables. Database triggers are traditionally used to enforce referential integrity and constraints, thereby making sure that only valid data enters the database; stored procedures that implement more complex constraints and additional operational security restrictions (e.g., forbid salary table update during weekends!) can be implemented as triggers, on top of the built-in security mechanisms offered by the RDBMS engine.

Resource Optimization

All database clients accessing the same database schema run the same in-memory copy of the procedure, thereby reducing overall memory allocation. Also, as demoed previously, depending on the level of integration, stored procedures can run within the same address space as the SQL engine, incurring minimal call overhead and optimizing memory utilization. In Chapter 2, I will describe in detail the internal mechanisms of the Java VM in the Oracle database.

Low-Cost Deployment

Independent software vendors (ISVs) and integrators already know that the ability to bundle their products on top of the database considerably simplifies installation, platform support, and product distribution. Java integration with the database eliminates the need for an external JDK/JRE and the headache of platform compatibility; furthermore, it works the same way on every platform on which the database runs.

Fully Utilize Database Capabilities

Part VI of this book describes how Oracle interMedia, TECSIS Systems, Oracle Text, British Columbia Corporate Online, and DBPrism CMS case studies use the database to its full extent.

Obstacles to the Adoption of Stored Procedures

The following concerns are usually invoked as showstoppers for adopting stored procedures: portability across database vendors, scalability, maintainability, and debugability. As discussed in the following text, some of these concerns are valid, but others are misperceptions.

Lack of Portability across RDBMS Vendors

In corporate IT environments that use more than one RDBMS, DBAs and database developers have to learn different procedural languages, such as PL/SQL, T-SQL, SQL/PL, and so on. Large IT organizations can afford to dedicate specific resources to each RDBMS for handling tasks ranging from managing databases to writing stored procedures. However, most organizations are looking for the flexibility of redeploying their staff of DBAs and developers according to their business priorities. Using Java across tiers and an RDBMS enables the portability of skills. Also, in the unlikely situation where an organization decides to move to a different RDBMS, it will have to migrate not only the schema and data but also the set of stored procedures developed over the years. Using Java leaves the door open for such a move because the Java sources and classes can be migrated smoothly, with minimal changes, losses, and cost.

Scalability

In typical application deployments, the clients (i.e., Web client, rich client, desktop) run against middle-tier nodes, which in turn funnel threads corresponding to clients against a pool of fewer database connections, typically an order of magnitude less than the number of concurrent clients/threads. Still, database scalability is crucial to middle-tier scalability. The session-based architecture of the Oracle database makes it scale linearly on symmetric multiprocessing (SMP) using a single RDBMS instance and quasi-linearly on clusters and grid nodes using multiple RDBM instances (i.e., Real Application Clusters [RAC]). To conclude, PL/SQL and Java stored procedures scale very well as far as the platform permits. In other words, the scalability of stored procedures is a by-product of the architecture of the target RDBMS and not a limitation of the stored procedure programming model per se.

Maintenance and Resilience to Schema Change

Upon schema change (i.e., when changes to table/column names, locations, or references occur), the stored procedures need to be updated to adapt to the new schema however, all of the applications built on top of those stored procedures remain unchanged and still return the exact result sets from the new database design. Shielding your applications (i.e., business logic) from the inevitable schema change by encapsulating the database schema within centralized stored procedures and validation logic is a small price to pay compared with the benefits of maintenance. Stored procedures act as interfaces between the data schema and the business logic layer, shielding each layer from changes occurring in the others. Encapsulation significantly reduces the ripple effect.

Hard to Debug

Most RDBMSs support stored procedures development and debugging through an integrated development environment (IDE) using either proprietary mechanisms such as the former Oracle’s java.debugAgent, which has now fallen into obsolescence, or standard mechanisms such as the Java Debug Wire Protocol (JDWP). Oracle JDeveloper integrates JDWP and allows simultaneous debugging of PL/SQL and Java stored procedures in the same session. Third-party IDE, which support JDWP, would also allow debugging PL/SQL and/or Java directly in the database. Alternatively, and this is what most Java developers currently do, you debug your Java code first outside the database (as a JDBC application), and then deploy it in the database. The bottom line is that debugging stored procedures is a bit less straightforward than debugging middle-tier applications or presentation logic using your favorite development tool; hence, there is this legitimate concern.

Weak Support for Complex Types

This concern is rather a question of perception. As shown in Chapter 3, stored procedures can pass complex database types, such as user-defined types (ADT), SQL object types, nested tables, VARRAY, and multilevel collections between the client program and the database. The standard SQLData interface allows custom mapping of user-defined types (ADT) in JDBC applications and stored procedures; furthermore, the Oracle JDBC extensions allow exchanging Oracle Object types between SQL (RDBMS) and JDBC applications (i.e., Java stored procedures).

Languages for Stored Procedures

This section discusses the pros and cons of using proprietary languages, Java, and the emerging category of .NET languages in the database.

Proprietary Languages

The following discussion applies to most proprietary languages for stored procedures;[4] however, I focus on the Oracle PL/SQL, which is widely used and regarded as one of the best vendor-supplied languages for stored procedures.

Seamless Integration with SQL

Proprietary languages for stored procedures such as Oracle’s PL/SQL are an extension to SQL and as such are well integrated into SQL with little or no data type conversion and optimized for faster SQL data access. PL/SQL is well suited for wrapping intensive SQL operations with moderately complex procedural logic.

IDE Support

Those languages benefit from a strong vendor-supplied development environment and also third-party IDE. As an example, the Oracle JDeveloper, as well as third-party IDE, provides a nice environment for writing, debugging, and maintaining PL/SQL programs.

Portability

Cross-platform portability of proprietary language such as PL/SQL is inherited from the portability of the RDBMS. As an example, compiled PL/SQL packages can be moved to different platforms where the Oracle database runs—from Solaris to Linux or Windows or vice versa—without recompilation. Cross-vendor portability (e.g., run one vendor’s language in another vendor’s database) is technically possible (see section 1.3.3) but not yet a sure thing.

Java for Stored Procedures

Complete Programming Language

The Java language is by design an object-oriented programming language that supports many programming models, including simple models such as JavaBean, POJO, JDBC applications, Java stored procedures, and more complex J2EE programming models such as Servlets, JavaServer Pages, and Enterprise Java Beans.

Secure Language

The Java language has built-in security mechanisms, such as the lack of pointer arithmetic, which prevents it from computing offending memory offset; the Garbage Collector, which reduces the risk of memory corruption by cleaning up dead objects and reclaiming unused memory; the type safety, described next; the byte-code verifier described later in this chapter; and Java 2 security for accessing system resources or remote systems (described in Chapter 2).

Type Safety

Java’s strong typing[5] and static typing (i.e., compile time type checking) make the language less vulnerable to viruses and buffer overflow security holes. The creators of Java carefully designed the language and byte code formats to facilitate static type checking. The byte code verifier effectively checks static types at compile time, giving Java developers the opportunity to fix any type errors before deployment, resulting in a type safe program that runs efficiently.

Robustness

Java requires catching exceptions that can be thrown by methods in any class, thereby making Java stored procedures more robust. The automatic memory Garbage Collector also enforces robustness because it reduces the likelihood of memory corruption.

Productivity: Rapid Design Features

The Java language comes with a set of built-in rapid application design (RAD) features, such as the following:

  • Built-in automatic bounds checking on arrays

  • Built-in network access classes (java.net, java.rmi)

  • Automatic Garbage Collector, which eliminates whole classes of memory management issues

  • Standard data types and application programming interfaces (APIs) contain many useful and ready-to-use classes (or easy-to-implement interfaces)

Using Java as a Procedural Language

Like most RDBMSs, the Oracle database promotes a simplified programming model that can be summarized as “no threading within applications code.” Although OracleJVM lets you deploy a threaded Java code, its scheduler is nonpreemptive; in other words, the active thread will run until it is no longer runable. The running Java application in a session is practically the only code running in the embedded Java VM. Java stored procedures also share the same simplicity with J2EE programming models: no threading within components code; the container itself is threaded, but the components (i.e., EJB, Servlet, JSP) are nonthreaded. Furthermore, Java experts discourage threading and recommend having only a very few for application robustness and portability [Bloch01]. This simplified programming model also simplifies memory management by removing the need to place memory allocation locks during garbage collection (GC).

Standard Specifications for Java Stored Procedures

The following American National Standards Institute (ANSI) specifications define SQLJ, Java stored procedures, and SQLJ Object types:

  • SQLJ Part 0. “Database Language SQL—Part 10: Object Language Bindings (SQL/OLB),” ANSI X3.135.10-1998. Specifications for embedding SQL statements in Java methods. Similar to the traditional SQL facilities for embedded SQL in COBOL and C and other languages. The Java classes containing embedded SQL statements are precompiled to pure Java classes with JDBC calls. Also known as SQL.

  • SQLJ Part 1. “SQL Routines Using the Java Programming Language,” ANSI NCITS N331.1. Specifications for installing Java classes in a SQL system and for invoking Java static methods as SQL stored procedures and functions. Also known as Java stored procedures.

  • SQLJ Part 2. “SQL Types Using the Java Programming Language,” ANSI NCITS N331.2. Also known as SQLJ Object Types.

POJO-like Programming Model

What are POJOs? If you Google “Java POJO,” you’ll get the following definition.

POJO = “Plain Old Java Object.” Term coined by Martin Fowler, Rebecca Parsons, and Josh MacKenzie to denote a normal Java object that is not a JavaBean, an EntityBean, a SessionBean, etc., and does not serve any other special role or implement any special interfaces of any of the Java frameworks (EJB, JDBC, . . .).

Any Java object can run within an EJB container, but many people don’t know that or forget it. Fowler et al. invented the acronym POJO so that such objects would have a “fancy name,” thereby convincing people that they were worthy of use.

POJOs are useful for creating a Domain Model. In contrast, the various types of beans and other special Java objects often have constraints that make it difficult to use them directly to model a domain.

Stored procedures use explicit SQL statements through JDBC and aren’t, therefore, pure POJOs; however, they have in common the simplicity of their programming models. Unlike when using Enterprise JavaBeans (EJBs), you don’t need to be a rocket scientist to get a Java stored procedure right. As a matter of fact, the next EJB specification (EJB 3.0) is looking at simplifying the EJB model by integrating the POJO programming model.

Stored Procedures and O/R Mapping

O/R mapping generally refers to transparent mapping of Java objects to a relational database, which is achieved through several mechanisms (or programming models), including EJB CMP, POJO, and Java Data Object (JDO).[6] Stored procedures may be used by O/R mapping frameworks to perform a custom mapping of a specific object but are by no means a substitute. Stored procedures belong to explicit persistence mechanisms (i.e., SQL intrusive), whereas O/R mapping frameworks address transparent persistence (i.e., non-SQL intrusive).

Cross-Database Portability

Most RDBMSs (except SQL Server) support Java, either through a loosely coupled external JDK-based runtime or through a tight integration of the Java runtime with the database kernel (i.e., OracleJVM). Database developers who choose Java in the database motivate this choice, among other things, by its cross-vendor portability. Although Java stored procedures implementations differ from one vendor to another, Java is by far the most portable language for stored procedures. This book offers in-depth coverage of Oracle’s implementation.

Huge Class Library and Tools: Reduced Development Time and Costs

As we all know, the ability to reuse existing libraries results in quicker and lower-cost applications development. The availability of a rich and very large set of standard libraries as well as third-party class libraries is one of the biggest benefits that Java brings to database developers. The smart and lazy developers will extend their databases with new capabilities (see Chapter 4) in no time, writing only a few lines of code and scripts to adapt and glue Java with the database.

Skills Reuse

Because Java is one of the most dominant and widespread programming languages, it is likely that Java programmers already exist within your organization; furthermore, most new hires graduating from college have Java programming skills. The ability to use the same language across the middle tier (business/application logic) and the database tier (data logic) bolsters skills reuse, which in turn simplifies resource allocation, thereby reducing project costs.

Java Database Connectivity and SQL Data Access

The OracleJVM embeds a special JDBC driver and a SQLJ runtime for direct SQL data access. This enables redeploying J2SE/JDBC/SQLJ applications in the database (see section 1.1.3).

Starting with Java

An introduction to Java is beyond the scope of this book; however, here are some pointers to start with Java:

.NET Languages

SQL Server 2005 introduces the Common Language Runtime (CLR) on top of the .NET framework, for running stored procedures written in C#, VB.NET, J#, and other languages. CLR can be viewed as a generic virtual machine, which supports multiple languages in the database. The most interesting aspect of CLR is its support by the latest releases of DB2 and Oracle; as a result, and similarly to Java, CLR would in theory allow the portability of code not only across the Microsoft middle tier and database tier,[7] but also across RDBMSs.[8] Java may no longer be the only portable language for stored procedures across RDBMSs but remains by far the most portable,[9] the most widely used, and the one that offers the largest reusable set of code and class libraries. Because the version of CLR might vary across vendors, it is not yet clear what will be the uptake of C#, J#, VB.NET beyond SQL Server 2005.

PL/SQL or Java

This is the $72,526 techno-political question being asked all the time: “When should we use PL/SQL and when should we use Java for stored procedures?” The short but correct answer is, “It depends!” It indeed depends on your goals; your requirements, such as the profile of the code being executed in the database (i.e., data access intensive versus computation intensive); the available skills within your organization; and whether you might, in the future, need to migrate the code in question from the database to the middle tier or vice versa. According to a survey conducted by Evans Data Corporation among database developers (across all RDBMSs), 22 percent declare using PL/SQL (which must be the majority of Oracle database customers) while 41 percent to 46 percent declare using Java, across all RDBMSs that support it. These figures are not exclusive; the person who declared using Java also uses PL/SQL when dealing with the Oracle Database. As you have already figured out, there is no straight answer; however, here are my own rules of thumb for choosing between Java and PL/SQL, but each DBA, database developer, and data architect has his or her own rules or motivations for choosing one approach versus another:

  • Prefer PL/SQL when (i) your data logic (i.e., data processing or data validation logic) is SQL intensive, or (ii) you already have the skills. Modeled after the ADA programming language, PL/SQL is an advanced procedural language. Its seamless integration with SQL and the Oracle database allows faster SQL data access with little or no type conversion. There is a large community with Oracle-supplied packages[10] and third-party libraries.

  • Prefer Java in the database when (i) your data logic has moderate SQL data access requirements (as opposed to SQL intensive) and moderate computational requirements (as opposed to compute intensive), or (ii) you already have the skills (Java skills are more pervasive, most college graduates know Java), or (iii) you need to accomplish things you cannot do in PL/SQL, such as interaction with ERP systems, RMI servers, Java/J2EE, and Web services, or (iv) you want to leave the door open for partitioning your application between the middle tier and the database tier . There is a large Java community with tons of class libraries (standard and third party) that you can reuse. When your data logic becomes too complex, you just migrate it to the middle tier.

  • Furthermore, you should consider Java/J2EE in the middle tier (stand-alone JDBC, JavaBeans, POJOs, EJBs, Servlets/Java Server-Pages, and so on) when (i) your business logic is complex or compute intensive with little to moderate direct SQL access, or (ii) you are implementing a middle-tier-driven presentation logic, or (iii) you require transparent Java persistence (i.e., POJOS, CMP EJB) as opposed to SQL intrusive persistence, or (iv) you require container-managed infrastructure services (transaction, security), or (v) many other reasons not specified here. If your business logic becomes SQL data access bound, you may migrate it into the database tier. JavaBeans, POJOs, and J2EE design models may orthogonally use stored procedures (PL/SQL and/or Java) directly through JDBC or indirectly through O/R mapping frameworks.

If performance is the key requirement, since Java in the database is sandwiched between PL/SQL (SQL intensive) and Java/J2EE in the middle tier (compute intensive), when is it competitive? As illustrated in section 1.1.3, when your code combines Java and SQL, Java in the database wins, since it incurs less roundtrips (minimal network overhead) and less data traffic.

[A poster on Slashdot.org] “While I agree that I would tend to abstract all SQL to some PL/SQL call that ‘DBAs who get it’ have control over, there are LOTS of things that Java can do that are VERY handy, when viewed at in the application architecture point of view and not just in a SQL context.”

PL/SQL and Java!

The pragmatic database developers use both Java and PL/SQL, because these complement each other very well to glue together the rich set of database features. Now that we have set the stage, in the next chapters, I’ll walk you through the entrails of the Java runtime in the Oracle database, how to reuse standard libraries, how to deploy your own Java classes, and examples of real-life cases.



[1] Mastering Enterprise JavaBeans, 2nd edition, by Ed Roman, Scott W. Ambler, and Tyler Jewell (New York: John Wiley & Sons, 2002).

[4] Not including languages supported by Microsoft’s Common Language Runtime, such as Visual BASIC and C#.

[5] Strong typing refers to the requirement that the type of each field and variable and the return type of each method be explicitly declared.

[7] Enabled by the integration of .NET with the SQL Server 2005 RDBMS.

[8] IBM DB2 Release 8.2 and Oracle Database 10g Release 2 support CLR 1.x.

[9] DB2, Oracle, Sybase, PortGresSQL, and MySQL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.134.130