Chapter 3 DB-Gateways

Similar documents
Chapter 3 DB-Gateways

Chapter 3 DB-Gateways

Chapter 4 Application Programs and Object-Relational Capabilities

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 13 Introduction to SQL Programming Techniques

Chapter 3 DB-Gateways

SQL: Programming Midterm in class next Thursday (October 5)

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Lecture 2. Introduction to JDBC

Top 50 JDBC Interview Questions and Answers

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Chapter 10 Web-based Information Systems

Unit 3 - Java Data Base Connectivity

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

JDBC - INTERVIEW QUESTIONS

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

Chapter Outline. Chapter 2 Distributed Information Systems Architecture. Layers of an information system. Design strategies.

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Chapter Outline. Chapter 2 Distributed Information Systems Architecture. Distributed transactions (quick refresh) Layers of an information system

Chapter 6 Object Persistence, Relationships and Queries

Database Application Development

JDBC MOCK TEST JDBC MOCK TEST IV

Communication and Distributed Processing

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

JDBC SHORT NOTES. Abstract This document contains short notes on JDBC, their types with diagrams. Rohit Deshbhratar [ address]

Chapter 4 Remote Procedure Calls and Distributed Transactions

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

DATABASE DESIGN I - 1DL300

UNIT-3 Java Database Client/Server

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

Introduction JDBC 4.1. Bok, Jong Soon

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

ITCS Implementation. Jing Yang 2010 Fall. Class 14: Introduction to SQL Programming Techniques (Ch13) Outline

JAVA AND DATABASES. Summer 2018

Middleware for Heterogeneous and Distributed Information Systems

Chapter 11 - Data Replication Middleware

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Communication and Distributed Processing

UNIT III - JDBC Two Marks

JDBC 3.0. Java Database Connectivity. 1 Java

Chapter 2 Distributed Information Systems Architecture

JDBC [Java DataBase Connectivity]

Database Application Development

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

Non-interactive SQL. EECS Introduction to Database Management Systems

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

Persistency Patterns. Repository and DAO

CMPT 354 Database Systems I

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

INTRODUCTION TO JDBC - Revised spring

Database Application Development

PESIT Bangalore South Campus

Chapter 10 Web-based Information Systems

SQLJ Part 0, now known as SQL/OLB (Object-Language Bindings)

INTRODUCTION TO JDBC - Revised Spring

Using Java - for PL/SQL and Database Developers Student Guide

Lecture 27 10/30/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Unit 2 JDBC Programming

Fast Track to Java EE

SQL DML and DB Applications, JDBC

SQL in a Server Environment

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

JDBC, Transactions. Niklas Fors JDBC 1 / 38

EMBEDDED SQL. SE 3DB3 Fall 2016 MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

SQL in a Server Environment (ii)

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

SQL Environment: Module Types. System Aspects of SQL. SQL Environment: Introduction. SQL Environment: Introduction. SQL Environment: Privileges

Java Database Connectivity

Working with Databases and Java

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

JDBC Programming: Intro

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

Databases 2012 Embedded SQL

Database Application Development

Chapter 17 Web Services Additional Topics

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Why use a database? You can query the data (run searches) You can integrate with other business systems that use the same database You can store huge

Instructor: Jinze Liu. Fall 2008

SQL: Programming. Introduction to Databases CompSci 316 Fall 2017

Java J Course Outline

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

TOPLink for WebLogic. Whitepaper. The Challenge: The Solution:

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

Outline. Chapter 5 Application Server Middleware. Types of application server middleware. TP monitors CORBA Server-side components and EJB Summary

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Database connectivity (II)

Programming in Java

ABOUT CORE JAVA COURSE SCOPE:

JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

Transcription:

Prof. Dr.-Ing. Stefan Deßloch AG Heterogene Informationssysteme Geb. 36, Raum 329 Tel. 0631/205 3275 dessloch@informatik.uni-kl.de Chapter 3 DB-Gateways Outline Coupling DBMS and programming languages approaches requirements Programming Model (JDBC) overview DB connection model transactions Data Access in Distributed Information System Middleware DB-Gateways architectures ODBC JDBC SQL/OLB embedded SQL in Java Summary 2 WS 2008/09 1

Coupling Approaches Overview Embedded SQL (static) SQL queries are embedded in the programming language cursors to bridge so-called impedance mismatch preprocessor converts SQL into function calls of the programming g language potential performance advantages (early query compilation) vendor-specific Dynamic (embedded) SQL SQL queries can be created dynamically by the program character strings interpreted as SQL statements by an SQL system Call-Level Interface (CLI) standard library of functions that can be linked to the program same capabilities as (static and dynamic) embedded SQL queries are string parameters of function invocation avoids vendor-specific precompiler Prof.Dr.-Ing. Stefan Deßloch 3 Middleware for Information Systems Coupling Approaches (Examples) Embedded SQL static Example: exec sql declare c cursor for SELECT empno FROM Employees WHERE dept = :deptno_var; exec sql open c; exec sql fetch c into :empno_var; dynamic Example: strcpy(stmt, "SELECT empno FROM Employees WHERE dept =?"); exec sql prepare s1 from :stmt; exec sql declare c cursor for s1; exec sql open c using :deptno_var; exec sql fetch c into :empno_var; Call-Level Interface (CLI) Example: strcpy(stmt, "SELECT empno FROM Employees WHERE dept =?"); SQLPrepare(st_handle, stmt, ); SQLBindParam(st_handle, 1,, &deptno_var, ); SQLBindCol(st_handle, 1,, &empno_var, ); SQLExecute(st_handle); SQLFetch(st_handle); 4 WS 2008/09 2

Standard Call Level Interfaces - Requirements Uniform database access query language (SQL) meta data (both query results and DB-schema) Alternative: SQL Information Schema programming interface Portability call level interface (CLI) no vendor-specific pre-compiler binaries are portable but: increased complexity dynamic binding of vendor-specific run-time libraries Dynamic, late binding to specific DB/DBS late query compilation flexibility vs. performance 5 Additional Requirements for DB-Gateways Remote data access Multiple simultaneously active DB-connections within the same thread to the same DB to different DBs within the same (distributed) transaction Simultaneous access to multiple DB/DBMS architecture supports use of (multiple) DBMS-specific s coordinated by a Support for vendor-specific extensions distributed TA client P T DB gateway presentation logic resource management DBS1 DBS2 DBS3 DB1 DB2 DB3 6 WS 2008/09 3

Historical Development ODBC: Open Database Connectivity introduced in 1992 by Microsoft quickly became a de-facto standard ODBC s available for almost any DBMS "blueprint" for ISO SQL/CLI standard JDBC introduced in 1997, initially defined by SUN, based on ODBC approach leverages advantages of Java (compared to C) for the API abstraction layer between Java programs and SQL current version: JDBC 4.0 (Dec. 2006) Java JDBC 4.0 SQL-92, SQL:1999, SQL:2003 (object-) relational DBS 7 JDBC Core Interfaces Connection default: <source>.<method> -> <target> createstatement preparestatement preparecall p subclass subclass Statement PreparedStatement CallableStatement IN/OUT/INOUT: CallableStatement. IN: getxxx/setxxx PreparedStatement. setxxx executequery Data Types executequery getxxx getresultset getmoreresults ResultSet 8 WS 2008/09 4

Example: JDBC DriverManager String url = "jdbc:db2:mydatabase"; Connection con = DriverManager.getConnection(url, "dessloch", "pass"); Connection String sqlstr = "SELECT * FROM Employees WHERE dept = 1234"; Statement stmt = con.createstatement( ); Statement ResultSet ResultSet rs = stmt.executequery(sqlstr); while (rs.next() ) { String a = rs.getstring(1); String str = rs.getstring(2); System.out.print(" empno= " + a); System.out.print(" firstname= " + str); System.out.print("\n"); } 9 JDBC Processing Query Results ResultSet getxxx-methods scrollable ResultSets updatable ResultSets Data types conversion functions streams to support large data values with JDBC 2.0 support of SQL:1999 data types LOBS (BLOBS, CLOBS) arrays user-defined data types references 10 WS 2008/09 5

JDBC Additional Functionality Metadata methods for metadata lookup important for generic s Exception Handling Batch Updates Savepoints RowSets... 11 Transactions in JDBC Connection interface transaction-oriented methods for local TAs begin is implicit commit() rollback() get/settransactionisolation() NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE get/setautocommit() Here, the scope of the transaction is a single connection! support for distributed transactions requires additional extensions, interactions with a transaction manager (see subsequent chapters) No 12 WS 2008/09 6

JDBC DataSource DataSource Interface motivation: increase portability by abstracting from -specific connection details uses logical name to obtain connection, interacting with Java Naming and Directory Service (JNDI) connections can be created, registered, reconfigured, directed to another physical DB without impacting the example: connections are set up and managed by an server administrator Steps DataSource object is created, configured, registered with JNDI using administration capability of server outside the component component obtains a DataSource object JNDI lookup no -specific details required obtains a Connection object using DataSource DataSource.getConnection( ) Prof.Dr.-Ing. Stefan Deßloch 13 Middleware for Information Systems Architecture Applications programs using DB-CLI functionality usage Driver connect to data sources execute SQL statements (e.g., queries) over data sources receive (and process) results processes CLI calls communicates SQL requests to DBMS Alternative: does the entire processing of the SQL requests hides heterogeneity of data sources Driver Manager manages interactions between s and s realizes (n:m)-relationship between s and s tasks load/unload mapping data sources to s communication/logging of function/method calls simple error handling Oracle DB2 O/JDBC API 14 WS 2008/09 7

Driver Manager Tasks connect request XYZ disconnect mapping data source-> no loaded? yes not OK error detection OK no still needed? yes load request XYZ ODBC only! unload 15 Driver Tasks and Responsibilities Connection Management Error handling standard error functions/codes/messages,... Translation of SQL requests if syntax of DBMS deviates from standard SQL Data type mapping Meta data functions access (proprietary) system catalogs Information functions provide information about (self), data sources, supported data types and DBMS capabilities Option functions Parameter for connections and statements (e.g., statement execution timeout) 16 WS 2008/09 8

Realization Alternatives ODBC types one-tier two-tier three-tier tier JDBC types Type 1: JDBC-ODBC bridge Type 2: Part Java, Part Native Type 3: Intermediate DB Access Server Type 4: Pure Java Application does not "see" realization alternatives! 17 Single-Tier Driver Used to access flat files, accessing flat files accessing ISAM files ISAM files, desktop or desktop DBs databases Data resides on the same machine as the Functionality: complete SQL processing (parse, optimize, execute) often lacks multi-user and transaction support file I/O calls engine calls ISAM/DTDB engine file I/O calls file system file system 18 WS 2008/09 9

Two-Tier Driver Classical client/server support Direct data protocol support client acts as a client interacting with DBMS (server) through data protocol Implementation alternatives Implementation alternatives 1. direct data protocol support 2. mapping ODBC to DBMS-client API 3. middleware solution Direct data protocol support message-based or RPC-based utilizes DBMS-specific network libraries or RPC runtime two-tier data protocol network libraries or RPC runtime network data protocol DBMS server 19 Two-Tier Driver (continued) Mapping to DBMS-client API client Middleware solution client two-tier DBS-client API DBS runtime library data protocol network libraries or RPC runtime network data protocol DBMS server two-tier (MW-vendor) network library or RPC runtime (middleware vendor) data protocol (MW vendor) server (middleware vendor) DBS runtime library DBMS server 20 WS 2008/09 10

Three-Tier Driver Middleware Server connects and relays requests to one or more DBMS servers Moves the complexity from the client to the middleware server client requires only a single (for the middleware server) Arbitrary number of tiers possible client middleware server server three-tier network lib./rpc runtime data protocol 1 DB request server two-tier addtl. components data protocol 2 DBMS 21 JDBC Driver Types Partial Java Type 1: JDBC-ODBC bridge 2-tier mapping to ODBC API uses Java Native Interface (JNI) requires native binaries at the client Type 2: Native-API Partial-Java 2-tier uses a native DBMS client library requires binaries at the client All-Java Type 3: Net-Protocol All-Java 3-tier on client is pure Java communicates with JDBC server/gateway no native binaries on client required applet-based DB access is possible Type 4: Native-Protocol All-Java 2-tier pure Java implements the network data protocol of the DBMS directly connects to the data source no native binaries on client required applet-based DB access is possible 22 WS 2008/09 11

SQL Object Language Bindings (OLB) aka SQLJ Part 0 Static, embedded SQL in Java Development advantages over JDBC more concise, easier to code static type checking, error checking at precompilation time Permits static authorization Can be used in client code and stored procedures SQLJ translator/customizer framework supports binary compatibility SQLJ translator implemented using JDBC produces statement profiles vendor-specific customizers can add different implementation, to be used instead of default produced by translator potential performance benefits resulting binary contains default and possibly multiple customized implementations Interoperability with JDBC combined use of SQLJ with JDBC for flexibility 23 SQL/OLB Static SQL authorization option Static SQL is associated with "program" Plans/packages identify "programs" to DB Program author's table privileges are used Users are granted EXECUTE on program Dynamic SQL is associated with "user" No notion of "program" End users must have table privileges BIG PROBLEM FOR A LARGE ENTERPRISE!!! Static SQL syntax for Java INSERT, UPDATE, DELETE, CREATE, GRANT, etc. Singleton SELECT and cursor-based SELECT Calls to stored procedures (including result sets) COMMIT, ROLLBACK Methods for CONNECT, DISCONNECT dynamic SQL parse SQL statement check table/view authorization calculate access path execute statement static SQL check authorization for package execute statement 24 WS 2008/09 12

SQL/OLB vs. JDBC: Retrieve Single Row SQL OLB #sql [con] { SELECT ADDRESS INTO :addr FROM EMP WHERE NAME=:name }; JDBC java.sql.preparedstatement ps = con.preparestatement( "SELECT ADDRESS FROM EMP WHERE NAME=?"); ps.setstring(1, name); java.sql.resultset names = ps.executequery(); names.next(); name = names.getstring(1); names.close(); 25 Result Set Iterators Mechanism for accessing the rows returned by a query Comparable to an SQL cursor Iterator declaration clause results in generated iterator class Iterator is a Java object Iterators are strongly typed Generic methods for advancing to next row Assignment clause assigns query result to iterator Two types of iterators Named iterator Positioned iterator 26 WS 2008/09 13

Named Iterators - Example Generated iterator class has accessor methods for each result column #sql iterator Honors ( String name, float grade ); Honors honor; #sql [recs] honor = { SELECT SCORE AS "grade", STUDENT AS "name" FROM GRADE_REPORTS WHERE SCORE >= :limit AND ATTENDED >= :days ORDER BY SCORE DESCENDING }; while (honor.next()) { System.out.println( honor.name() name() + " has grade " + honor.grade() ); } 27 Positioned Iterator Use FETCH statement to retrieve result columns into host variables based on position #sql iterator Honors ( String, float ); Honors honor; String name; float grade; #sql [recs] honor = { SELECT STUDENT, SCORE FROM GRADE_REPORTS WHERE SCORE >= :limit AND ATTENDED >= :days ORDER BY SCORE DESCENDING }; while (true) { #sql {FETCH :honor INTO :name, :grade }; if (honor.endfetch()) break; System.out.println( name + " has grade " + grade ); } 28 WS 2008/09 14

SQLJ - Binary Portability Java as a platform-independent language Use of generic SQLJ-precompiler/translator (avoids DBMS-specific precompiler technology) Generated code uses standard JDBC by default Compiled SQLJ (Java byte code) is portable Customizer technology allows DBMS-specific optimizations after the compilation SQLJ source class ABC { #sql SELECT... } Generic SQLJ translator SQLJ source class ABC { call "stub" } Java Com piler Java byte codes Extracted SQL Optional step: DBM S-specific custom izer Java byte codes Extracted SQL SELECT... host var data SELECT... host var data Extracted SQL SELECT... host var data JDBC default "stub" JD BC "stub" M ost vendors use default JDBC "stub" JD BC "stub" DBM S-specific "s t u b " 29 Summary Gateways ODBC / JDBC support uniform, standardized access to heterogeneous data sources encapsulate/hide vendor-specific aspects multiple, simultaneously active connections to different databases and DBMSs / architecture enabled for distributed transaction processing high acceptance important infrastructure for realizing IS distribution at DB-operation level no support for data source integration JDBC 'for Java, in Java important basis for data access in Java-based middleware (e.g., J2EE) SQLJ combines advantages of embedded SQL with portability, vendor-independence 30 WS 2008/09 15