Chapter 5
Chapter 5
Chapter 5
Chapter 5
Application Design
Agenda
• Database Application Development & SQL
• Defining Transactions
• Locking
• Batch Processing
• Questions
Database Application Development
and SQL
To properly design an application that relies on databases
for persistent data storage, the system designer at a
minimum will need to understand the following issues:
• How data is stored in a relational database
• How to code SQL statements to access and modify data in
the database
• How SQL differs from traditional programming languages
• How to embed SQL statements into a host programming
language
• How to optimize database access by changing SQL and
indexes
• Programming methods to avoid potential database
processing problems
SQL
• SQL is the de facto standard for accessing relational
databases
• SQL is a high-level language that provides a greater
degree of abstraction than do traditional procedural
languages
• SQL focuses on what data you want specify data
is needed, not how to get it. It's a declarative language,
meaning you specify the result you want, and the
database engine decides how to execute the query.
– It does not—indeed it cannot—specify to retrieve it
SQL: English-like
• SQL can be used to retrieve data easily with
an English-like syntax.
• It is easier to understand this:
Result Set ( )
SQL
Statement
Database Tables
Embedding SQL in a Program
• Host language to use SQL to communicate with the database
– COBOL, FORTRAN, Assembler, etc.
– C/C++, Java, PHP, Visual Basic, etc.
• API: allow applications to interface with databases.
– ODBC, JDBC
• Integrated Development Environment (IDE): is a complete
software environment that supports all phases of development.
– Eclipse (for Java, C++, Python, etc.)Visual Studio (for C#, VB.NET, C++,
etc.) PyCharm (for Python)
• A code generator is a tool that automates the
generation of code based on certain inputs, such as
templates or models.
SQL Middleware and APIs
• Application programs require an for issuing SQL to
access or modify data. The interface is used to embed SQL
statements in a host programming language, such as COBOL,
Java, C, or Visual Basic.
• Standard interfaces enable application programs to access
databases using SQL. There are several popular standard
interfaces or APIs (Application Programming Interfaces) for
database programming, including ODBC, JDBC, SQLJ, and OLE DB.
• ODBC
– ODBC is a , or CLI
– Instead of directly embedding SQL in the program, ODBC uses
.
• to allocate and deallocate resources
• control connections to the database
• execute SQL statements
• obtain diagnostic information
• control transaction termination
• obtain information about the implementation
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <iostream>
int main() {
SQLHENV hEnv;
SQLHDBC hDbc;
SQLHSTMT hStmt;
SQLRETURN ret;
// Allocate environment handle
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
// Set the ODBC version environment attribute
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
// Allocate connection handle
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
// Connect to database (replace DSN, user, password accordingly)
SQLConnect(hDbc, (SQLCHAR*)"DSN=mydsn", SQL_NTS, (SQLCHAR*)"username", SQL_NTS, (SQLCHAR*)"password", SQL
// Allocate statement handle
SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);
// Execute an SQL query
SQLExecDirect(hStmt, (SQLCHAR*)"SELECT * FROM my_table", SQL_NTS);
// Fetch and display the data
SQLCHAR columnData[256];
while (SQLFetch(hStmt) == SQL_SUCCESS) {
SQLGetData(hStmt, 1, SQL_C_CHAR, columnData, sizeof(columnData), NULL);
std::cout << "Column Data: " << columnData << std::endl;
} // Clean up
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return 0; }
SQL Middleware and APIs
• JDBC
– JDBC 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.
– There are several types of JDBC middleware,
including the JDBC-to-ODBC bridge, as well as direct
JDBC connectivity to the relational database.
– Anyone familiar with application programming and
ODBC (or any call-level interface) can get up and
running with JDBC quickly
Drivers
• ODBC and JDBC rely on
– A driver provides an optimized interface for a particular DBMS
implementation
• Programs can make use of the drivers to communicate
with any JDBC- or ODBC-compliant database.
• The drivers enable a standard set of SQL statements in
any Windows application to be translated into
commands recognized by a remote SQL-compliant
database.
• There are multiple types of JDBC drivers
Drivers Components
• SQLJ
– SQLJ (SQL for Java) is an extension of the Java programming
language that allows developers to embed SQL statements
directly within Java code
– A precompiler translates the embedded SQL into Java code.
– The Java program is then compiled into bytecodes, and a
database bind operation creates packaged access routines for
the SQL.
– This bytecode is platform-independent, meaning it can run on
any machine with a Java Virtual Machine (JVM). The JVM
interprets or compiles the bytecode into native machine code
for execution.
SQL Middleware and APIs
• OLE DB (Object Linking and Embedding Database)
– OLE DB presents an object-oriented interface for generic
data access.
– COM Architecture: OLE DB is built on the Component Object
Model (COM), which allows for language independence and
supports the creation of reusable software components.
This enables OLE DB to work across different programming
languages.
– OLE DB provides greater flexibility than ODBC because it can
be used to access both relational and nonrelational data.
– OLE DB is conceptually divided into and .
• consumers are the applications that need access to the data
• providers are the software components that implement the interface
and thereby provide the data to the consumer.
Application Infrastructure
• Application infrastructure is the combined hardware and software
environment that supports and enables the application.
• The application infrastructure will vary from organization to
organization, and even from application to application within an
organization.
• From a hardware perspective, the application infrastructure
includes the servers, clients, and networking components.
• From a software perspective, things are a bit more difficult to nail
down. Software components of an application infrastructure can
include database servers, application servers, web servers,
transaction managers, and development frameworks.
Application Infrastructure
Mainframe Distributed
• IBM z Series hardware • Most modern, distributed,
• Running z/OS, DB2, CICS, with
application programs written in non-mainframe application
COBOL. development projects
• Typically, applications consist of
both batch and online workload. typically rely upon
• A modern mainframe application development
infrastructure adds interfaces to
non-mainframe clients, as well as frameworks.
WebSphere Application Server and • The two most
Java programs.
• Most new mainframe development commonly-used
uses IDEs to code modern frameworks are Microsoft .
applications instead of relying
upon COBOL programmers. NET and J2EE.
Microsoft .NET
• ... is a set of Microsoft technologies for
connecting people, systems, and devices
• ... allows Internet Servers to expose functions
to any client named as .NET web services
• … enables software to be delivered as a
service over the web
• … is designed to let many different services
and systems interact
Microsoft .NET Framework
The Microsoft .NET framework provides a comprehensive development
platform for the construction, deployment, and management of applications.
The .NET framework provides CLR (common language runtime) and class
library for building components using a common foundation. This offers
benefits to developers such as support for standard practices, extensibility,
and a tightly integrated set of development tools.
Note that there is much more to Java than is covered in this section.
J2EE and Java
Java
Standalone
Runtime JSP Enterprise
Java JavaBeans
Application
Pages
Database
Browser
Pure Business
HTML Components
Servlets
for Java
Applet
Impact of Java on DBA
• Application tuning
– Must understand Java
• To provide guidance during design reviews
– Is the problem in the SQL or the application
• How can you tune the application if you do not
understand the language (Java)?
– Optimizing SQL is not enough since it may be embedded in
poor application code
– Must understand the SQL techniques used
• JDBC and SQLJ
Java .Net
• ...designed to enable • …designed to enable
applications to be development in
deployed on any multiple languages as
platform as long as long as the application
they are written in Java is deployed on
Windows
Other Application Choices
• There are other choices, including
– Ruby on Rails
– Ajax
– PHP
– C/C++
– And so on…
requirement
transactions.
<CUSTOMER>
<first_name>Craig</first_name>
<middle_initial>S.</middle_initial>
<last_name>Mullins</last_name>
<company_name>Mullins Consulting, Inc.</company_name>
<street_address>15 Coventry Ct.</street_address>
<city>Sugar Land</city>
<state>TX</state>
<zip_code>77479</zip_code>
<country>USA</country>
</CUSTOMER>
http://www.xml.org
Querying XML
• XQUERY
– FLWOR
• FOR, LET, WHERE, ORDER BY, and RETURN.
– Not just for querying, it also allows for new XML
documents to be constructed
• SQL/XML
– Uses functions to access XML data
• XMLDOCUMENT, XMLELEMENT, XMLCONCAT,
XMLAGG, XMLQUERY, XMLTABLE
Select the customer's first name:
xpath
/CUSTOMER/first_name
Workflow
Controller
Transaction
Server Relational
Relational DBMS
DBMS (2)
Relational Disk
DBMS (1)
Disk Disk
Application Servers
• An application server combines the features
of a transaction server with additional
functionality to assist in building, managing,
and distributing database applications.
• Examples:
– WebSphere (IBM)
– Zend Server
– Base4 Application Server (open source)
Transactions and Locking
• The DBMS uses a mechanism to enable
multiple, concurrent users to access and modify data
in the database.
• By using locks, the DBMS automatically guarantees
the integrity of data. The DBMS locking strategies
permit multiple users from multiple environments to
access and modify data in the database at the same
time.
• Locking Granularity
– Row
– Page (or Block)
– Table
– Table Space
– Database
Level of Lock Granularity
High
Access Concurrency
Low
Granularity of Lock
Column Row Page Table Tablespace Database
Types of Locks
• The following types of locks can be taken on database pages or
rows:
– Shared Lock
• Taken when data is read with no intent to update it.
• If a shared lock has been taken on a row, page, or table, other processes or
users are permitted to read the same data.
– eXclusive Lock
• Taken when data is modified.
• If an exclusive lock has been taken on a row, page, or table, other processes
or users are generally not permitted to read or modify the same data.
– Update Lock
• Taken when data must first be read before it is changed or deleted.
• The update lock indicates that the data may be modified or deleted in the
future.
• If the data is actually modified or deleted, the DBMS will promote the update
lock to an exclusive lock.
Intent Locks
• Intent locks are placed on higher-level
database objects when a user or process
takes locks on the data pages or rows.
– Table or Table Space
• An intent lock stays in place for the life of the
lower-level locks.
Lock Compatibility
Lock Timeouts
e r ror
Deadlocks
Process A Process B
. Table X
.
.
Request row 3 .
data… data… data...
. lock .
. .
. Request row 7
. .
Request row 7 data… data… data... .
lock .
Request row 3