Craig S. Mullins Articles Database Performance Management |
|
November 2000 |
|
|
Using Java with DB2 for OS/390
What is Java? First and foremost, Java is an object-oriented programming language. Developed by Sun Microsystems in 1991, Java was modeled after, and most closely resembles C++. But Java requires a smaller footprint and eliminates some of the more complex and error-prone features of C and C++ (such as pointer management and the goto construct). Additionally, many tasks have been moved from the programming language to the JVM (Java Virtual Machine). Java enables animation for and interaction with the World Wide Web. Although Web interaction is one of its most touted features, Java is a fully functional programming language that can be used for developing general-purpose programs independent from the Web. But let’s look at the web aspect of Java a little more closely. Using HTML, developers can run Java programs, called applets, over the Web. But Java is a completely different language than HTML, and it does not replace HTML. Java applets are automatically downloaded and executed by users as they surf the Web. The Java applet is run by the Web browser. Another thing that makes Java special is that it was designed to be multi-platform. In theory, regardless of the machine and operating system that you are running, any Java program should be able to run. Many possible benefits accrue because Java enables developers to write an application once and then distribute it to be run on any platform. These benefits include reduced development and maintenance costs, lower systems management costs, and more flexible hardware and software configurations. So, to summarize, the major qualities of Java are
Now that DB2 for OS/390 supports application development using Java, all of these qualities are available to DB2 applications. Java Bytecodes and the Java Virtual Machine (JVM) After the Java program is written the program source code is compiled into machine-independent constructs called bytecodes using the Java compiler. Bytecodes are the manner in which Java achieves it platform independence. Because the Java bytecode is in a machine-independent, architecture-neutral format, it can run on any system with a standard Java implementation. The Java bytecodes are then processed by the Java Virtual Machine (JVM). The JVM interprets the bytecodes for the platform on which the Java program is to be run. The JVM loads and verifies the Java bytecode. It is then passed to the Java interpreter to be executed. Alternately, the bytecodes can be passed to a just-in-time (JIT) compiler to be compiled into machine code to be executed. Java has a reputation as a "slow" language. That is, the performance of Java is questionable. The major disadvantage is that Java is an interpreted language, like BASIC. Both the Java interpreter and the JIT compiler consume resources and take time to process the Java bytecodes before execution. Therefore, the performance of a Java program will lag when compared to an equivalent program that is compiled and link-edited into object code (such as a C or COBOL program). As a developer, you must decide whether the platform independence and Web development capabilities offset the potential performance degradation. Java Applications, Applets, and Servlets There are three types of Java implementation methods that you can implement when accessing DB2 data from Java:
A Java application program is basically the same as a program written in any other programming language. It can perform all of the tasks normally associated with programs, including many tasks that Java applets cannot perform. Furthermore, a Java application does not need a browser to be executed. It can be executed in a client or server machine. A Java applet is a small application program that must be downloaded before it is run within a Java-enabled Web browser. Java applets reside on a Web server. When the Web server returns an HTML page that points to a Java applet, the Java-enabled Web browser requests the applet to be downloaded from the Web server. After the applet is received at the browser, either the browser starts the applet internally, or an external JVM executes it. Applets typically perform simple operations, such as editing input data, control screen interaction, and other client functionality. Of course, Java applets can be written to perform more complex functionality, but to load and run non-Java code in the client requires signed applets, which have the authority needed to run code in the client machine. You should be aware that performance problems could arise because Java applets must be downloaded before they can be run. The time to download the applet must be factored into its service levels. In general, Java applets are small, so the performance impact should be negligible. But, even small downloads can be slow if there are network problems. Java applets can be cached by the Web browser, which can diminish the performance impact. A Java servlet is basically server-side Java. A Java servlet runs on the Web server, just like an applet runs in the Web browser. Java servlets can be used to extend the functionality of the Web server. The Web server hands requests to the servlet, and the servlet replies to them. Servlets can be used instead of CGI applications. To run Java servlets, your Web server must support the Java servlet API, developed by JavaSoft. This API defines how the servlet communicates with the server. Java servlets have security advantages over client-side Java applets. A servlet that runs on a Web server inside a firewall can control access to sensitive data and business logic. Java applets do not inherently provide these security capabilities. Before choosing which Java development style to use, you must know the basics of the environment in which the program must run. Ask the following questions when deciding what type of Java program is required for your development needs:
Java applications, Java applets, and Java servlets are similar in nature. However, a different method is used to invoke each of them. Java applets and servlets are started from an HTML page. Java applications do not require a Web component but can be used as part of an intranet solution. To implement Java programs, you need to use the Java Developers Kit or JDK for short. The JDK is a development environment for writing Java programs conforming to the Java 1.1 Core API (Java 1.1 and later because that is the first JDK that included Java driver manager). The JDK includes the Java Virtual Machine (JVM), Java classes, source files to create the classes in the JVM, documentation, and the JDK tools required for building and testing Java bytecode. These tools include the Java compiler and interpreter, the Java applet viewer, and the Java debugger. JDBC and SQLJ Access to DB2 for OS/390 data in Java applications is accomplished using one of two methods: JDBC or SQLJ. Java Database Connectivity, or JDBC, is an API that enables Java to access relational databases. Similar to ODBC, JDBC consists of a set of classes and interfaces that can be used to access relational data. Anyone familiar with application programming and ODBC (or any call-level interface) can get up and running with JDBC quickly. JDBC uses dynamic SQL to access DB2 data. The primary benefits of JDBC include the following:
Potential drawbacks of JDBC include in the following:
The other way to access DB2 data from a Java program is SQLJ. SQLJ enables developers to embed SQL statements in Java programs. SQLJ provides static SQL support to Java. Developers can embed SQL statements into Java, and a precompiler is used to translate SQL into Java code. Then the Java program can be compiled into bytecodes, and a bind can be run to create a package for the SQL. Simply stated, SQLJ enables Java programs to be developed the way most DB2 programs have been developed for years. Of course, SQLJ does not allow dynamic SQL. But you can mix SQLJ and JDBC in a single Java program, which effectively enables you to choose static or dynamic SQL for your Java programs. The primary benefits of SQLJ include the following:
Potential drawbacks of the SQLJ approach include the following:
Be sure to check out the Java sections of IBM’s Web site for additional information regarding Java support and sample Java code. Two good URLs to bookmark are
Using Result Set Iterators to Retrieve Multiple Rows Traditional DB2 application programs written in host languages use a DB2 cursor to retrieve individual rows from a multi-row result set. The SQLJ equivalent of a cursor is a result set iterator. A result set iterator can be passed as a parameter to a method. The result set iterator is defined using an iterator declaration clause specifying a list of Java data types. The Java data types represent columns of the table in the result set. The information in the following table shows the SQL data types and their equivalent SQLJ data types that can be specified in result set iterator declarations. The SQLJ data type in the left column can be used for data retrieved that is of any of the SQL data types listed in the right column.
Table 1: SQLJ and SQL Data Type Equivalents SQLJ Data Type SQL Data Type java.lang.String CHAR VARCHAR LONG VARCHAR GRAPHIC VARGRAPHIC LONG VARGRAPHIC java.math.BigDecimal NUMERIC INTEGER DECIMAL SMALLINT FLOAT REAL DOUBLE Boolean INTEGER SMALLINT Integer INTEGER SMALLINT DECIMAL NUMERIC FLOAT DOUBLE Float INTEGER SMALLINT DECIMAL NUMERIC FLOAT DOUBLE Double INTEGER SMALLINT DECIMAL NUMERIC FLOAT DOUBLE byte[ ] CHARACTER VARCHAR LONG VARCHAR GRAPHIC VARGRAPHIC LONG VARGRAPHIC java.sql.Date DATE java.sql.Time TIME java.sql.Timestamp TIMESTAMP
DBA Considerations Before your shop moves forward with Java development under DB2 for OS/390, be sure that your DBAs have been trained in Java. Intelligent organizations require that the database administration group reviews all application code that accesses DB2 databases before the code can be promoted to production. Well, if your DBAs do not understand Java, how will they be able to provide tuning advice for Java programs? The short answer is, they won’t. So all DBAs should have at least rudimentary knowledge of Java constructs and the way in which Java programs are developed. Failure to educate the DBAs in the basics of Java is a sure-fire method for ensuring performance problems with DB2-based Java applications. Summary The Internet is infiltrating every aspect of information technology. And DB2 is most definitely impacted. One of those impacts is the increased usage of Java. Java is increasing in popularity not just because of its web abilities, but also because of its portability. Now that DB2 for OS/390 supports Java, your organization may be the next one to begin using Java. Make sure that you are prepared to support Java and understand it benefits, drawbacks, and how it works, prior to using Java for production applications.
|