Prof. Dr.-Ig. Stefa Deßloch AG Heterogee Iformatiossysteme Geb. 36, Raum 329 Tel. 0631/205 3275 dessloch@iformatik.ui-kl.de Chapter 3 DB-Gateways
Outlie Couplig DBMS ad programmig laguages approaches requiremets Programmig Model (JDBC) overview DB coectio model trasactios DB-Gateways architectures ODBC JDBC SQL/OLB embedded SQL i Java Summary Prof.Dr.-Ig. Stefa Deßloch 2 Middleware for Heterogeeous ad Distributed Iformatio Systems
Static Embedded SQL Static SQL queries are embedded i the programmig laguage programmig laguage is "exteded", usig prefix for SQL operatios cursors to bridge so-called impedace mismatch (sets of results) host variables for query parameters ad results Example: exec sql declare c cursor for SELECT empo FROM Employees WHERE dept = :depto_var; exec sql ope c; exec sql fetch c ito :empo_var; Preprocessor/precompiler coverts SQL ito fuctio calls of the programmig laguage potetial performace advatages (early query compilatio) vedor-specific precompiler ad target iterface fuctio calls ivoke DBMS-specific APIs iitial source code is (usually) portable, but code resultig from precompilatio is ot! Prof.Dr.-Ig. Stefa Deßloch 3 Middleware for Heterogeeous ad Distributed Iformatio Systems
Dyamic Embedded SQL SQL queries ca be created dyamically by the program character strigs iterpreted as SQL statemets by a SQL system Example: strcpy(stmt, "SELECT empo FROM Employees WHERE dept =?"); exec sql prepare s1 from :stmt; exec sql declare c cursor for s1; exec sql ope c usig :depto_var; exec sql fetch c ito :empo_var; Preprocessor is still required oly late query compilatio same drawbacks regardig portability as for static embedded Prof.Dr.-Ig. Stefa Deßloch 4 Middleware for Heterogeeous ad Distributed Iformatio Systems
Call-Level Iterface (CLI) Stadard library of fuctios that ca be liked to the program Same capabilities as (dyamic) embedded SQL queries are strig parameters of fuctio ivocatios Example: strcpy(stmt, "SELECT empo FROM Employees WHERE dept =?"); SQLPrepare(st_hadle, stmt, ); SQLBidParam(st_hadle, 1,, &depto_var, ); SQLBidCol(st_hadle, 1,, &empo_var, ); SQLExecute(st_hadle); SQLFetch(st_hadle); Avoids vedor-specific precompiler, allows to write/produce biary-portable programs Prof.Dr.-Ig. Stefa Deßloch 5 Middleware for Heterogeeous ad Distributed Iformatio Systems
Stadard Call Level Iterfaces - Requiremets Uiform database access query laguage (SQL) meta data (both query results ad DB-schema) Alterative: SQL Iformatio Schema programmig iterface Portability call level iterface (CLI) o vedor-specific pre-compiler applicatio biaries are portable but: icreased applicatio complexity dyamic bidig of vedor-specific ru-time libraries Dyamic, late bidig to specific DB/DBS late query compilatio flexibility vs. performace Prof.Dr.-Ig. Stefa Deßloch 6 Middleware for Heterogeeous ad Distributed Iformatio Systems
Additioal Requiremets for DB-Gateways Remote data access Multiple simultaeously active DB-coectios withi the same applicatio thread to the same DB to differet DBs withi the same (distributed) trasactio Simultaeous access to multiple DBMS architecture supports use of (multiple) DBMS-specific drivers coordiated by a driver maager Support for vedor-specific extesios distributed TA DBS1 cliet P T DB gateway DBS2 DB1 DB2 DB3 presetatio applicatio logic resource maagemet DBS3 Prof.Dr.-Ig. Stefa Deßloch 7 Middleware for Heterogeeous ad Distributed Iformatio Systems
Historical Developmet ODBC: Ope Database Coectivity itroduced i 1992 by Microsoft quickly became a de-facto stadard ODBC drivers available for almost ay DBMS "blueprit" for ISO SQL/CLI stadard JDBC itroduced i 1997, iitially defied by SUN, based o ODBC approach leverages advatages of Java (compared to C) for the API abstractio layer betwee Java programs ad SQL curret versio: JDBC 4.1 (July 2011) Java applicatio JDBC 4.1 SQL-92, SQL:1999, SQL:2003 (object-) relatioal DBS Prof.Dr.-Ig. Stefa Deßloch 8 Middleware for Heterogeeous ad Distributed Iformatio Systems
JDBC Core Iterfaces Coectio default: <source>.<method> -> <target> createstatemet preparestatemet preparecall subclass subclass Statemet PreparedStatemet CallableStatemet IN: PreparedStatemet. setxxx IN/OUT/INOUT: CallableStatemet. getxxx/setxxx executequery Data Types executequery getxxx getresultset getmoreresults ResultSet Prof.Dr.-Ig. Stefa Deßloch 9 Middleware for Heterogeeous ad Distributed Iformatio Systems
Example: JDBC Strig url = "jdbc:db2:mydatabase"; Coectio co = DriverMaager.getCoectio(url, "dessloch", "pass"); Strig sqlstr = "SELECT * FROM Employees WHERE dept = 1234"; Statemet stmt = co.createstatemet( ); ResultSet rs = stmt.executequery(sqlstr); while (rs.ext() ) { Strig a = rs.getstrig(1); Strig str = rs.getstrig(2); System.out.prit(" empo= " + a); System.out.prit(" firstame= " + str); System.out.prit("\"); } Prof.Dr.-Ig. Stefa Deßloch 10 Middleware for Heterogeeous ad Distributed Iformatio Systems
JDBC Processig Query Results ResultSet getxxx-methods scrollable ResultSets updatable ResultSets Data types coversio fuctios streams to support large data values with JDBC 2.0 came support of SQL:1999 data types LOBS (BLOBS, CLOBS) arrays user-defied data types refereces Prof.Dr.-Ig. Stefa Deßloch 11 Middleware for Heterogeeous ad Distributed Iformatio Systems
JDBC Additioal Fuctioality Metadata methods for metadata lookup importat for geeric applicatios Exceptio Hadlig SQLExceptio class (hierarchy) carries SQL error code, descriptio Itegrated with Java (chaied) exceptio hadlig Batch Updates multiple statemets ca be submitted at oce to improve performace RowSets... Ca hold a (discoected) copy of a result set Modificatios ca be buffered ad explicitly sychoized with the database later Prof.Dr.-Ig. Stefa Deßloch 12 Middleware for Heterogeeous ad Distributed Iformatio Systems
No Trasactios i JDBC Coectio iterface trasactio-orieted methods for local TAs begi is implicit commit() rollback() get/settrasactioisolatio() NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE get/setautocommit() Here, the scope of the trasactio is a sigle coectio! support for distributed trasactios is provided usig additioal extesios, iteractios with a trasactio maager (see subsequet chapters) same i ODBC Prof.Dr.-Ig. Stefa Deßloch 13 Middleware for Heterogeeous ad Distributed Iformatio Systems
DB Gateway Architecture Applicatios Driver programs usig DB-CLI fuctioality usage coect to data sources execute SQL statemets (e.g., queries) over data sources receive (ad process) results processes CLI calls hides heterogeeity of data sources may traslate SQL to vedor-specific dialect commuicates SQL requests to DBMS Alterative: does the etire processig of the SQL requests Driver Maager maages iteractios betwee applicatios ad drivers realizes (:m)-relatioship betwee applicatios ad drivers tasks load/uload driver mappig data sources to drivers commuicatio/loggig of fuctio/method calls simple error hadlig applicatio driver maager Oracle driver DB2 driver O/JDBC API Prof.Dr.-Ig. Stefa Deßloch 14 Middleware for Heterogeeous ad Distributed Iformatio Systems
Driver Maager Tasks applicatio coect driver maager applicatio request XYZ driver maager applicatio discoect driver maager mappig data source-> driver o driver loaded? yes ot OK error detectio OK o driver still eeded? yes load driver driver request XYZ driver uload driver driver ODBC oly! Prof.Dr.-Ig. Stefa Deßloch 15 Middleware for Heterogeeous ad Distributed Iformatio Systems
Driver Tasks ad Resposibilities Coectio Maagemet Error hadlig stadard error fuctios/codes/messages,... Traslatio of SQL requests if sytax of DBMS deviates from stadard SQL Data type mappig Meta data fuctios access (proprietary) system catalogs Iformatio fuctios provide iformatio about driver (self), data sources, supported data types ad DBMS capabilities Optio fuctios Parameter for coectios ad statemets (e.g., statemet executio timeout) Prof.Dr.-Ig. Stefa Deßloch 16 Middleware for Heterogeeous ad Distributed Iformatio Systems
Realizatio Alteratives ODBC driver types oe-tier two-tier three-tier JDBC driver types Type 1: JDBC-ODBC bridge Type 2: Part Java, Part Native Type 3: Itermediate DB Access Server Type 4: Pure Java Applicatio does ot "see" realizatio alteratives! Prof.Dr.-Ig. Stefa Deßloch 17 Middleware for Heterogeeous ad Distributed Iformatio Systems
Sigle-Tier Driver Used to access flat files, ISAM files, desktop databases accessig flat files accessig ISAM files or desktop DBs Data resides o the same machie as the driver Fuctioality: complete SQL processig (parse, optimize, execute) applicatio driver maager driver applicatio driver maager driver egie calls ofte lacks multi-user ad trasactio support file I/O calls ISAM/DTDB egie file I/O calls file system file system Prof.Dr.-Ig. Stefa Deßloch 18 Middleware for Heterogeeous ad Distributed Iformatio Systems
Two-Tier Driver Classical cliet/server support driver acts as a cliet iteractig with DBMS (server) through data protocol Implemetatio alteratives 1. direct data protocol support 2. mappig ODBC to DBMS-cliet API 3. middleware solutio Direct data protocol support applicatio driver maager two-tier driver data protocol cliet (1) Direct data protocol support message-based or RPC-based utilizes DBMS-specific etwork libraries or RPC rutime etwork libraries or RPC rutime etwork data protocol DBMS server Prof.Dr.-Ig. Stefa Deßloch 19 Middleware for Heterogeeous ad Distributed Iformatio Systems
Two-Tier Driver (cotiued) (2) Mappig to DBMS-cliet API (3) Middleware solutio cliet cliet applicatio driver maager two-tier driver DBS-cliet API DBS rutime library data protocol etwork libraries or RPC rutime etwork data protocol DBMS applicatio driver maager two-tier driver (MW-vedor) etwork library or RPC rutime (middleware vedor) etwork data protocol (MW vedor) server applicatio (middleware vedor) DBS rutime library DBMS server server Prof.Dr.-Ig. Stefa Deßloch 20 Middleware for Heterogeeous ad Distributed Iformatio Systems
Three-Tier Driver Middleware Server gateway server cliet applicatio coects ad relays requests to oe or more DBMS servers Moves the complexity from the cliet to the middleware server cliet requires oly a sigle driver (for the middleware server) Arbitrary umber of tiers possible middleware server driver maager three-tier driver etwork lib./rpc rutime data protocol 1 DB request server driver maager two-tier driver addtl. compoets data protocol 2 server DBMS Prof.Dr.-Ig. Stefa Deßloch 21 Middleware for Heterogeeous ad Distributed Iformatio Systems
JDBC Driver Types Partial Java (requires ative biaries o cliet) Type 1: JDBC-ODBC bridge 2-tier mappig to ODBC API uses Java Native Iterface (JNI) Type 2: Native-API Partial-Java driver 2-tier uses a ative DBMS cliet library All-Java (does ot require ative biaries o cliet) Type 3: Net-Protocol All-Java driver 3-tier driver o cliet is pure Java commuicates with JDBC server/gateway Type 4: Native-Protocol All-Java driver 2-tier pure Java implemets the etwork data protocol of the DBMS directly coects to the data source Prof.Dr.-Ig. Stefa Deßloch 22 Middleware for Heterogeeous ad Distributed Iformatio Systems
SQL Object Laguage Bidigs (OLB) aka SQLJ Part 0 Static, embedded SQL i Java Developmet advatages over JDBC more cocise, easier to code static type checkig, error checkig at precompilatio time Permits static authorizatio Ca be used i cliet code ad stored procedures Goal: SQLJ traslator/customizer framework supports biary compatibility (ulike traditioal embedded SQL) SQLJ traslator implemeted usig JDBC produces statemet profiles vedor-specific customizers ca add differet implemetatio, to be used istead of default produced by traslator potetial performace beefits resultig biary cotais default ad possibly multiple customized implemetatios Iteroperability with JDBC combied use of SQLJ with JDBC for flexibility Prof.Dr.-Ig. Stefa Deßloch 23 Middleware for Heterogeeous ad Distributed Iformatio Systems
SQL/OLB Static SQL authorizatio optio Static SQL is associated with "program" Plas/packages idetify "programs" to DB Program author's table privileges are used dyamic SQL static SQL check authorizatio for package Users are grated EXECUTE o program Dyamic SQL is associated with "user" No otio of "program" parse SQL statemet Ed users must have table privileges BIG PROBLEM FOR A LARGE ENTERPRISE!!! Static SQL sytax for Java check table/view authorizatio INSERT, UPDATE, DELETE, CREATE, GRANT, etc. calculate access path Sigleto SELECT ad cursor-based SELECT Calls to stored procedures (icludig result sets) execute statemet execute statemet COMMIT, ROLLBACK Methods for CONNECT, DISCONNECT Prof.Dr.-Ig. Stefa Deßloch 24 Middleware for Heterogeeous ad Distributed Iformatio Systems
SQL/OLB vs. JDBC: Retrieve Sigle Row SQL OLB #sql [co] { SELECT ADDRESS INTO :addr FROM EMP WHERE NAME=:ame }; JDBC java.sql.preparedstatemet ps = co.preparestatemet( "SELECT ADDRESS FROM EMP WHERE NAME=?"); ps.setstrig(1, ame); java.sql.resultset ames = ps.executequery(); ames.ext(); ame = ames.getstrig(1); ames.close(); Prof.Dr.-Ig. Stefa Deßloch 25 Middleware for Heterogeeous ad Distributed Iformatio Systems
Result Set Iterators Mechaism for accessig the rows retured by a query Comparable to a SQL cursor Iterator declaratio clause results i geerated iterator class Iterator is a Java object Iterators are strogly typed Geeric methods for advacig to ext row Assigmet clause assigs query result to iterator Two types of iterators Named iterator Positioed iterator Prof.Dr.-Ig. Stefa Deßloch 26 Middleware for Heterogeeous ad Distributed Iformatio Systems
Named Iterators - Example Geerated iterator class has accessor methods for each result colum #sql iterator Hoors ( Strig ame, float grade ); Hoors hoor; #sql [recs] hoor = { SELECT SCORE AS "grade", STUDENT AS "ame" FROM GRADE_REPORTS WHERE SCORE >= :limit AND ATTENDED >= :days ORDER BY SCORE DESCENDING }; while (hoor.ext()) { System.out.pritl( hoor.ame() + " has grade " + hoor.grade() ); } Prof.Dr.-Ig. Stefa Deßloch 27 Middleware for Heterogeeous ad Distributed Iformatio Systems
Positioed Iterator Use FETCH statemet to retrieve result colums ito host variables based o positio #sql iterator Hoors ( Strig, float ); Hoors hoor; Strig ame; float grade; #sql [recs] hoor = { SELECT STUDENT, SCORE FROM GRADE_REPORTS WHERE SCORE >= :limit AND ATTENDED >= :days ORDER BY SCORE DESCENDING }; while (true) { #sql {FETCH :hoor INTO :ame, :grade }; if (hoor.edfetch()) break; System.out.pritl( ame + " has grade " + grade ); } Prof.Dr.-Ig. Stefa Deßloch 28 Middleware for Heterogeeous ad Distributed Iformatio Systems
SQLJ - Biary Portability Java as a platform-idepedet laguage Use of geeric SQLJ-precompiler/traslator (avoids DBMS-specific precompiler techology) Geerated code uses stadard JDBC by default Compiled SQLJ applicatio (Java byte code) is portable Customizer techology allows DBMS-specific optimizatios after the compilatio SQLJ source class ABC { #sql SELECT... } Geeric SQLJ traslator SQLJ source class ABC { call "stub" } Java Compiler Java byte codes Extracted SQL Optioal step: DBMS-specific customizer Java byte codes Extracted SQL SELECT... host var data SELECT... host var data Extracted SQL SELECT... host var data JDBC default "stub" JDBC "stub" Most vedors use default JDBC "stub" JDBC "stub" DBMS-specific "stub" Prof.Dr.-Ig. Stefa Deßloch 29 Middleware for Heterogeeous ad Distributed Iformatio Systems
Summary Couplig approaches static ad dyamic embedded SQL call-level iterface (CLI) Gateways JDBC SQLJ ODBC / JDBC support uiform, stadardized access to heterogeeous data sources ecapsulate/hide vedor-specific aspects multiple, simultaeously active coectios to differet databases ad DBMSs driver/driver maager architecture eabled for distributed trasactio processig (see subsequet chapters) high acceptace importat ifrastructure for realizig IS distributio at DB-operatio level o support for data source itegratio 'for Java, i Java importat basis for data access i Java-based middleware (e.g., J2EE) combies advatages of embedded SQL with biary portability, vedor-idepedece Prof.Dr.-Ig. Stefa Deßloch 30 Middleware for Heterogeeous ad Distributed Iformatio Systems