Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in...

59
Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaver RIG US SAP Labs

Transcript of Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in...

Page 1: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40

Marc ChanNetWeaver RIG USSAP Labs

Page 2: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 2

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 3: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 3

Java Persistence Framework: Two Goals

Strict Java standard orientationn Lower TCO … n Customers like to be independent from any

software manufacturern A huge amount of skilled developers

worldwide

Enable the development of real life database centric business applications in Java that are portable between different DBMS.n SAP transfers experiences learned with the

proven and successful ABAP server to Java world!

In doing so …

Page 4: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 4

Example: Performance of Business Transactions

A "normal" Java method executes within microseconds.

A database access normally needs a few milliseconds or even seconds to return a result.

General rule:The performance of a business transaction is primarily determined by its database accesses.

DB

Application

GUI

Page 5: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 5

Java Persistence Framework: Key Features

Improves performance, reliability and supportability transparently to applications.

Enables SQL based database programming portable across supported RDBMS.

Complies with the established database programming standards for Java:n basic platform: JDBC (as designated by J2EE)n J2EE database programming model

adjusted with encouraging standards:SQLJ and JDO APIs

The proprietary infrastructure of SAP Web AS:

As many Java servers, SAP Web AS:

Page 6: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 6

Apply the Technology Smart: SAP Recommendations

J2EE and database programming are complex technologies and it is indispensable to apply them properly.

Restrict, but do not contradict J

Therefore, SAP releases recommendationsrestricting J2EE standard if in conflict with supportability, portability, reliability …

Page 7: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 7

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 8: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 8

Architecture – What about the Database?

Database programming starts with the database.

RDBMS

SAP Web AS JavaInstance

Dispatcher

Server Server... Enqueue

Message

Dispatcher

Server Server...

SAP Web AS JavaInstance

SAP Web AS Java Cluster

Central Cluster Services

The J2EE cluster runs on a single central database.

This is your Java database.

Page 9: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 9

Architecture – Characteristics of the Database

Database is relational (RDBMS) and accessiblewith JDBC

RDBMS is supportedby SAP Web AS

Unicode only

SAP Web AS Java Cluster sid»

SAP Web AS Java Cluster

Database Schema SAP«sid»DB

ApplicationData

Java Server Internal Data

JDBC

SAP Web AS JavaDefault Database Schema

RDBMS

Default databaseschema contains

n Java Server data

n Application data

Page 10: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 10

SAP Web AS 6.40 Java: Supported RDBMS

MySQL MaxDB

… complemented with a JDBC database driverapproved by SAP.

Oracle

IBM DB2 UDB for UNIX and Windows

Microsoft SQL Server

IBM DB2 UDB for z/OS

IBM DB2 UDB for iSeries

Page 11: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 11

Architecture – What about Good, Old ABAP?

SAP Web AS 6.40 is two servers put into one.Howevern Separate database storagen Logically separate databases, separate schemas

JavaServer

SAP Web AS

ABAPServer

ABAPSchema

JavaSchema

Database

Preferred configurationn Single database,

separate schemasn Single database

parameterisation,as of ABAP

Page 12: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 12

Separate Storage and Connect Applications

Consequences of separation

n No database transactions spanning ABAP and Java stack

n No direct table access across ABAP / Java stacksn No namespace coordination for database objects

Collaboration at the component level:JCo, Web Services,XI,...

JavaApp.

SAP Web AS

ABAPApp.

ABAPSchema

JavaSchema

Database

SAP JCo

Page 13: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 13

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 14: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 14

Open SQL Engine

Open SQL Engine

wraps the proprietary JDBC driver transparently to your application.

Comparable toOpen SQL for ABAP

JDBCDriver

Open SQL

JDBC code

At runtime, there is an active persistence framework: Open SQL Engine.

Page 15: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 15

The Three Support Levels of Open SQL Framework

The support level is set for each connection pool.

The default setting is ‘Open SQL’

Page 16: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 16

n SQLJn Portability check SQL and JDBCn Java Dictionaryn Table Buffer

n SQL Tracen SQL Statement Pooling

n Database Connection Pooln Full JDBC standard APIn Vendor-specific SQL statements

Open SQL Framework: Adding Value Layer by Layer

Native SQL

Open SQL

Vendor SQL

Recommended if:- you cannot use the default

database schema.- database tables already exist.

Generally recommended!Obligatory for internal SAP projects!

Default databaseschema only

Page 17: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 17

Database Connection Pool

SAP Web AS Java transparently caches open database connections in a server-sidedatabase connection pool.

Open and release database connections without delay!

Access the default database schema through the pre-configureddefault connection pool.

You must not change the settings of the default connection pool!

JDBCConnector

Service

SAP Web AS Java

JavaSchema

Database

DefaultConnection

Pool

Application

Page 18: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 18

Statement Pooling

Still active behind the scenes:Statement Pooling:avoids repeated (expensive) preparation of SQL statements in the database.

A server-side pool for both “Prepared” and “Callable” JDBC statement types.

Prepared statement pool for each physical database connection.

DB-Server

SAP Web ASSQL

Parse and Compile SQL

Execution PlanSQLPr

epar

e SQ

L

ExecuteSQL

Result Set

Done once!

Page 19: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 19

SQL Trace: JDBC Call Logging and Viewing

http://localhost:50000/SQLTraceLocate performance bottlenecks J

Locate bugs J

JDO, CMP, SQLJ: Examine the generated code! J

Page 20: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 20

Table Buffer

Server 1 Server 2

Table Cache

Database

Table Cache

T

TT

BC_SYNCLOG

Update on T

READINSERTUPDATE T Timer (60 s)

Caches table data inside VM. One table buffer in each VM (Server node). Synchronization possibly delayed.

Page 21: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 21

Enable Table Buffering: Developer Studio

Buffer small tables that are mostly read andrarely changed and certain inconsistenciesare acceptable.

… Is it a good idea to cache the table TMP_CUSTOMER?

Choose the granularity of buffering

Answer: No

Page 22: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 22

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 23: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 23

Portable Table Definition: Java Dictionary

Development: Portabledefinition, creation and modification of database tables.

Runtime: Code checkagainst the Table Catalog.

SAP «sid» DB

Oracle, DB2, MSSQL, MaxDB

DDL

JavaDictionary

CREATE TABLE

True "write once, run anywhere" for data definition.

Page 24: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 24

Developer Studio: Dictionary Perspective

Pre-definedJDBC likeportabledata types

here, not there

Page 25: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 25

Built-In Dictionary Database Types

■ timestamps expressed by date, time and fractional seconds■ time zone attached, SAP uses UTCtimestamp

■ time values expressed by hour, minute and second■ no time zone attachedtime

■ date values expressed by year, month and day■ no time zone attacheddate

■ exact numeric numbers with precision P > 0 and scale S >= 0decimal(P,[S])

■ “8 Byte” floating point numbers, approximate numericdouble

■ “4 Byte” floating point numbers, approximate numericfloat

■ “8 Byte” signed integral numbers, exact numericlong

■ “4 Byte” signed integral numbers, exact numericinteger

■ “2 Byte” signed integral numbers, exact numericshort

■ byte strings with a fixed size of N bytes, if short (≤255 B)■ varying length byte string with maximum length of N bytes, if longerbinary(N)

■ varying length character strings with a maximum length of N characters■ Unicode onlystring(N)

Given Type Mapping: Dictionary ↔ JDBC ↔ Java

Page 26: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 26

Code Portability with Open SQL Engine

Java Program

Relational Database

Open SQL Engine

Open SQL Processor

DB Access Layer

Relational Database System

Table Buffer

Table Catalog

Statement Pool

SQL Trace

Object Relational Persistence

EJB (CMP)

JDO

Relational Persistence (SQL)

Open SQL / SQLJ

Open SQL / JDBC

Native SQL / JDBC

Vendor JDBC

Page 27: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 27

Developer View: Standardized Persistence APIs

JDBC, SQLJrelational, SQL-based coding: expressive!

JDO, EJB CMP entity beansobject relational coding: SQL-free! Portable!

EJB BMP entity beansobject relational, SQL based coding.Alternatively: Can map to an ABAP backend acting as the data store!

Standardized Persistence APIs

Page 28: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 28

Developer View: An Application Scenario

JDBC

Java Application

SQLJ JDO EJBEntityBeans

EJBSession Bean

Business Logic

Database Access

Choose the adequate Java persistence API!

DBMSRecommended

Page 29: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 29

SQLJ: SQL Check at Development Time

SQL statement check against the Open SQL Grammarand against Java Dictionary Table Definitions:

Use SQLJ for static SQL and JDBC for dynamic SQL

At design time!

I know right away my code is

correct and portable.

Page 30: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 30

J2EE Persistence APIs – Use of Open SQL Engine

Native vs. Open SQLn Open SQL language complies to SQL standard if at all possiblen Open SQL is NOT JDBC-compliant as it contains restrictionsn Valid Open SQL statements do not in general run on Native SQL

nonoyesSQLJ

toleratedsupportedyesJDBC

as required byJDO certificationsupportedyesJDO

toleratedsupportedyesEJB

Vendor SQLNative SQLOpen SQL

Page 31: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 31

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 32: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 32

SQLJn Standard for using Java and SQL together

SQLJn Standard way for embedding static SQL into Java programs

n As opposed to JDBCu where SQL statement is String argument of JDBC method

SQLJ – What is it?

SQLSQLJJ = = JavaJava + + SQLSQL

Java source text

SQL

ResultSet rs = stmt.executeQuery( "SELECT cs, ci FROM dbtab" );

Page 33: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 33

SQLJ – How Does It Work?

Special Syntax for identifying SQL

Java source text

#sql ... ;{ SQL }

The actual SQL statement

SQLJ source files with own extension .sqlj

Pre-processing by the SQLJ translator

SQLJ clause

Page 34: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 34

SQLJ – Translating SQLJ Sources

SQLJsource file

Javasource file

Javaclass file

SQLJTranslator

javac

MyClass.sqlj

MyClass.class

MyClass.java

§ replaces SQLJ clauses bycalls to the SQLJ runtime

§ transparently integrated into the IDE

Page 35: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 35

#sql [ctx] { Select col into :var FROM tab };

SQLJ – Syntax

SQL text is enclosed by "{" and "}".

SQL text is case insensitive.

Host variables are prefixed with ":"

Java text is case sensitive.

Page 36: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 36

SQLJ in the IDE – Creating SQLJ Sources

Creating SQLJ sourcesn File

→ New→ Other

→ Persistencen Context menu

→ New → Other

→ Persistence

Page 37: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 37

SQLJ in the IDE – Reference to the Logical Catalog

Logical Catalogn Offline description of

database tablesn Represented by an

Dictionary Projectn Used by the SQLJ translator

u for checking SQL statements

Usage of a Logical Catalogn By referring to the

corresponding Dictionary Project

Page 38: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 38

JDBC

Data source (jdbc/myDB)

Get a connection from the data source

Work with the connectionn Send SQL statements n Iterate over result setsn Release resources

Close the connection

Working with SQLJ

SQLJ

Connection Context Class

Connection Context

Named Iterator

Page 39: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 39

SQLJ – Connection Context Class

Connection Context Classn Represents a data sourcen Represents a logical catalog at design-time

Declaration

Connection Context Class = Ordinary Java Classn Created by the SQLJ translatorn for each "#sql context" clause

#sql context MyCtxwith (dataSource = "java:comp/env/jdbc/myDB");

Page 40: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 40

MyCtx ctx = null;

try {ctx = new MyCtx();#sql [ctx] { DELETE FROM dbtab WHERE key = 123 };

}finally {

ctx.close();}

SQLJ – Connection Contexts

Connection Context (Object)n Represents a database connectionn Instance of a connection context class

Example

Get a connection

Always close the connection in the

finally block!

Page 41: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 41

SQLJ – Insert, Update, Delete

Insert Statement

Update Statement

Delete Statement

#sql [ctx] { DELETE FROM dbtab WHERE key = 123 };

#sql [ctx] { UPDATE dbtab SET name = :newNameWHERE name = :oldName };

#sql [ctx] { INSERT INTO dbtab(Id, Lastname, FirstName)VALUES (1, 'Smith', 'John')};

Page 42: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 42

Fetch a single row of a Result Set into host variables

The Result Set must contain only one row

- specify the full key

- use aggregate functions

SQLJ – Single Row Query

String s;int i, keyVal = 1234;

#sql [ctx] { SELECT cs, ci INTO :s, :iFROM dbtabWHERE key = :keyVal };

The selected columns are fetched into host variables.

Ensure that the result set contains only one

row

Page 43: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 43

Empty result set: com.sap.sql.NoDataException

More than one row: com.sap.sql.CardinalityViolationException

SQLJ – Single Row Query (Exceptions)

String s;

try {

#sql [ctx] { SELECT cs INTO :s FROM tab };

} catch (NoDataException ex) {// no data

} catch (CardinalityViolationException ex) {// too many rows

}

Page 44: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 44

SQLJ – Queries Returning More Than One Row

How to access the rows of the result of a SQL query?

Named Result Set Iteratorsn Java class declared by #sql iteratorn Like a Result Set / Cursorn Strongly typed

n Name-Type pair for columnsn Type compatibility checked at compile-time

#sql iterator NamedIter ( String CS, int CI );

NamedIter nIter;

#sql [ctx] nIter = { SELECT cs, ci FROM dbtab };

The result of the query is assigned to the iterator nIter.

Page 45: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 45

SQLJ – Using Named Result Set Iterators

#sql iterator NamedIter ( String CS, int CI );int i;String s; NamedIter nIter;

#sql [ctx] nIter = { SELECT cs, ci FROM dbtab };

while ( nIter.next() ) {System.out.println( nIter.CS() );...System.out.println( nIter.CI() );

}

nIter.close();

n Forward only iteratorn Move to the next row with next()n Close iterator with close()

Page 46: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 46

SQLJ – Host Variables

Prefixed with ":"

Static type checking of host variables

Available for IN and OUT parameters

int keyValue = 5;String value = null;

#sql [ctx] { SELECT col INTO :value FROM dbtabWHERE key = :keyValue };

OUTparameter

INparameter

Page 47: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 47

SQLJ – Host Expressions

Enclosed between ":(" and ")"

May have side effects

IN and OUT expressions evaluated before execution of the statement in lexical order

String[] values = new String[5];MyClass ref = new MyClass();int i = 3;

#sql [ctx] { SELECT colINTO :(values[++i]) FROM dbtabWHERE key = :(ref.getKey(i)) };

Evaluated first before execution

Evaluated second before execution

Page 48: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 48

SQLJ – What About Dynamic SQL Statements?

Dynamic SQL Statementn not known statically (compile-time)n constructed at run-time

SQLJ not applicable!

Resort to JDBCn Dynamic call-level APIn SQL statement = String argument of JDBC method

Page 49: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 49

SQLJ – Interoperability with JDBC

Usage of SQLJ + JDBCn in the same transactionn on the same connectionn on the same result set

Easy to accomplishn since SQLJ is built on top of JDBC

Open SQLJDBC

Open SQLJDBC

Open SQL EngineOpen SQL Engine

SQL Processor

DB access layer

Open SQLSQLJ

Open SQLSQLJ

Page 50: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 50

Connections – SQLJ → JDBC

Connection Context java.sql.ConnectiongetConnection

SQLJ JDBC

#sql context MyCtxwith (dataSource = "java:comp/env/jdbc/myDB");

MyCtx myCtx = new myCtx();

java.sql.Connection myCon = myCtx.getConnection();

Example:

Page 51: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 51

Connections – JDBC → SQLJ

java.sql.Connection Connection ContextConstructor

JDBC SQLJ

#sql context MyCtx;

java.sql.Connection myCon = myDS.getConnection();

MyCtx myCtx = new myCtx( myCon );

Example:

Page 52: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 52

#sql iterator MyIter ( String CS, int CI );

#sql [ctx] myIter = { SELECT cs, ci FROM dbtab };

java.sql.ResultSet rs = myIter.getResultSet();

Example:

Result Sets – SQLJ → JDBC

Named Iterator java.sql.ResulSetgetResultSet

SQLJ JDBC

Page 53: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 53

Result Sets – JDBC → SQLJ

java.sql.ResultSet Named IteratorCAST clause

JDBC SQLJ

#sql iterator MyIter ( String CS, int CI );

String query = "SELECT cs, ci FROM dbtab";java.sql.ResultSet rs = stmt.executeQuery( query);

MyIter myIter;#sql myIter = { CAST :rs };

Example:

Page 54: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 54

SQLJ – Summary

Benefitsn Simpler, more compact and more robust programsn Compile-time checks (syntax / types)n Integrated into NetWeaver Developer Studio

u SQLJ translator transparently invokedu Syntax high-lighting

n Integrated into the Java Development Infrastructure

Always use SQLJ for static SQL

Use JDBC only for dynamic SQL

Page 55: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 55

Agenda

Java Persistence @ SAP

Database Architecture

Runtime: Open SQL Engine

Developer View

Open SQL / SQLJ

Summary

Page 56: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 56

Java Persistence – Summarizing the Benefits

Database Portabilityn Oracle, DB2, MSSQL, SAPDB

Standard Conformant APIsn JDBC, SQLJ, JDO, EJB entity

beans

Performance Enhancementsn Table Buffern Statement Pooln Connection Pool

Supportabilityn SQL Trace

Seamlessly integrated into

the J2EE server

Comes with the J2EE server

Runs out of the box

Page 57: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 57

Java Persistence Documentation

SAP Libraryin SAP NetWeaver Developer Studio

n Architecture Manualu“Java Persistence”

n Development Manualu“Java Dictionary”uDeveloping Business Logicl “Java Persistence”

n Reference Manualu“Java Persistence Reference”

Page 58: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 58

Q&A

Questions?

Page 59: Java Persistence and the Open SQL Engine in SAP … · Java Persistence and the Open SQL Engine in SAP Web Application Server 6.40 Marc Chan NetWeaverRIG US SAP Labs

SAP AG 2004, Java Persistence and Open SQL Engine in SAP Web AS 6.40 / Slide 59

n No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.

n Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.

n Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint® and SQL Server® are registered trademarks of Microsoft Corporation.

n IBM®, DB2®, DB2 Universal Database, OS/2®, Parallel Sysplex®, MVS/ESA, AIX®, S/390®, AS/400®, OS/390®, OS/400®, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere®, Netfinity®, Tivoli®, Informix and Informix® Dynamic ServerTM are trademarks of IBM Corporation in USA and/or other countries.

n ORACLE® is a registered trademark of ORACLE Corporation.

n UNIX®, X/Open®, OSF/1®, and Motif® are registered trademarks of the Open Group.

n Citrix®, the Citrix logo, ICA®, Program Neighborhood®, MetaFrame®, WinFrame®, VideoFrame®, MultiWin® and other Citrix product names referenced herein are trademarks of Citrix Systems, Inc.

n HTML, DHTML, XML, XHTML are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.

n JAVA® is a registered trademark of Sun Microsystems, Inc.

n JAVASCRIPT® is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape.

n MarketSet and Enterprise Buyer are jointly owned trademarks of SAP AG and Commerce One.

n SAP, R/3, mySAP, mySAP.com, xApps, xApp and other SAP products and services mentioned herein as well astheir respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies.

Copyright 2004 SAP AG. All Rights Reserved