PL/SQL Coding Style Guidelines Introducing Error Handling, Tracing and Coding Standards. Author: Vishal Gupta



Similar documents
OPP ODTUG Kaleidoscope. An ODTUG SP* Oracle PL/SQL Programming Conference. WOW-Wide Open World, Wide Open Web!

Making the Most of Oracle PL/SQL Error Management Features

Oracle PL/SQL Best Practices

Error Management in Oracle PL/SQL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Bullet Proof Your PL/SQL


Handling PL/SQL Errors

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Oracle For Beginners Page : 1

Best Practices For PL/SQL Development in Oracle Application Express

Oracle Database 10g: Program with PL/SQL

6b Continuing Past Exceptions

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Change Management for Rational DOORS User s Guide

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

Oracle Database: SQL and PL/SQL Fundamentals

Training Guide. PL/SQL for Beginners. Workbook

Oracle 11g PL/SQL training

Oracle Database: SQL and PL/SQL Fundamentals NEW

Approvals Management Engine R12 (AME) Demystified

Oracle Database: Program with PL/SQL

Handling PL/SQL Errors

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: Program with PL/SQL

Oracle Database: Develop PL/SQL Program Units

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Program with PL/SQL

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Database Programming with PL/SQL: Learning Objectives

Answers to the Try It Yourself Sections

Best Practices for Dynamic SQL

Oracle Data Miner (Extension of SQL Developer 4.0)

Virtual Private Database Features in Oracle 10g.

Oracle Database 11g: Program with PL/SQL

Intellicus Cluster and Load Balancing (Windows) Version: 7.3

Custom Web ADI Integrators

How To Set Up An Intellicus Cluster And Load Balancing On Ubuntu (Windows) With A Cluster And Report Server (Windows And Ubuntu) On A Server (Amd64) On An Ubuntu Server

Oracle Database: Program with PL/SQL

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Developing Software in a Private workspace PM PMS

ODEX Enterprise. Introduction to ODEX Enterprise 3 for users of ODEX Enterprise 2

Oracle Database: Program with PL/SQL

Netop Remote Control Security Server

Oracle Approvals Management (AME) Case Studies for AP, PO and HR

Is ETL Becoming Obsolete?

Kentico CMS 5 Developer Training Syllabus

Qualys API Limits. July 10, Overview. API Control Settings. Implementation

Amman Jordan Mob: Tel:

PROCEDURES, FUNCTIONS AND PACKAGES

Xcode Project Management Guide. (Legacy)

Sahana Training Program Day Schedule v0.5

Dbvisit Replicate. Dbvisit Replicate Overview

How to Use Oracle Account Generator for Project-Related Transactions

Enhanced Project Management for Embedded C/C++ Programming using Software Components

Manufacturing. Manufacturing challenges of today and how. Navision Axapta solves them- In the current explosive economy, many

Introduction. SQL Developer Data Modeler Overview. The Reporting Schema. SQL Developer Data Modeler 3.0 Under The Covers: The Reporting Schema

Software Architecture Document

CA Service Desk Manager

FreeForm Designer. Phone: Fax: POB 8792, Natanya, Israel Document2

Stored Documents and the FileCabinet

An Oracle White Paper October Oracle Data Integrator 12c New Features Overview

Oracle Network Logistics

OpenLDAP Oracle Enterprise Gateway Integration Guide

ACD Manual. Version 3.1 for SV8100 R8

Sound Customizing. Clemens de Vos, Epicenter

CSCI E 98: Managed Environments for the Execution of Programs

Chapter 13: Program Development and Programming Languages

BarTender Integration Methods. Integrating BarTender s Printing and Design Functionality with Your Custom Application WHITE PAPER

Documentum Developer Program

RSA Event Source Configuration Guide. McAfee Database Security

How To Test Your Web Site On Wapt On A Pc Or Mac Or Mac (Or Mac) On A Mac Or Ipad Or Ipa (Or Ipa) On Pc Or Ipam (Or Pc Or Pc) On An Ip

Smarter Balanced Assessment Consortium. Recommendation

Elixir Schedule Designer User Manual

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8

EVALUATION ONLY. WA2088 WebSphere Application Server 8.5 Administration on Windows. Student Labs. Web Age Solutions Inc.

WebSphere Business Monitor

Setting up the Oracle Warehouse Builder Project. Topics. Overview. Purpose

1. Introduction Auditing Handlers and Audit Trails Configure the Built-In Handler Create a Custom Audit Handler...

CHAPTER 4 Enhanced Automated Attendant

Mary E. Shacklett President Transworld Data

Oracle Business Intelligence 11g OPN Advanced Workshop

Construction Principles and Design Patterns. Flyweight, Bridge, Builder

Code:1Z Titre: Oracle WebLogic. Version: Demo. Server 12c Essentials.

Automating Software License Management

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

Remote Access Platform. Architecture and Security Overview

Transcription:

PL/SQL Coding Style Guidelines Introducing Error Handling, Tracing and Coding Standards Author: Vishal Gupta

Introduction Presenter: Vishal Gupta Topics: Reasons for guidelines Standards for error handling Standards for trace handling Header comments and versions Questions and answers 2

Reasons for guidelines Advantages of using coding guidelines: Uniform look and feel of all of the source code Framework can be used for implementing new modules Faster ramp-up time for new developers Proven methods and structures are reused which leads to more stable code 3

Standards for Error Handling All PL/SQL code has to conform to the error handling standards Error handler in each procedure/function. In large procedures multiple error handlers should be defined Secondary error handlers should be defined in case an error can occur inside of an error handler Procedures should forward errors to the calling procedure Standard error_struct (structure) should be used for error handling Each error that is raised in code should be documented in configuration table Documentation and example 4

Categorization of Error Handling All Defined errors should be categorized as follows: I = Info (These are just for information level Process continues in regular flow) W = Warning (Warning Exceptions/Logs should work like to handle certain situations of processing steps these are not Business Critical and process continues) E = Error (Any Exception that reports Application Error Should Terminate processing) F = Fatal error (Any Undefined Exception, that is trapped and handled external to application using WHEN OTHERS should be recorded as FATAL error and should terminate the execution of process) 5

Error Handling Cont Error Handling Configuration will look like as: FIELD_NAME EXAMPLE DESCRIPTION ERROR_CODE ERROR_TYPE ERROR_TEXT USER_CREAT TED CREATE_DAT E DROP-000001 I, W, E, F No data found in <%s1>! 123 12/12/2008 20.12.00PM This is the unique identifier of Error Code referenced in code exception I = Info, W = Warning, E = Error, F = Fatal error Where No data found in is generic text and %s1 will be replaced by parameters in generic error handling package passed from the code. ID of the application user who created this record should references to user tables Date time record created 6

Error Dependencies: Primary errors (P) This is the error that should occurs at first. It gets a 'Master-ID'. Example ID Master-ID Kind of Error 1 1 P 2 2 P Secondary errors (S) These are errors that happen in any kind of cleanup or exception handling of a primary error. This error should reference to the 'Master-ID'. 3 4 2 2 F F 5 5 P 6 5 S Forwarded errors (F) These are errors that should be just passed through the exception handlers on the way up to the caller (in most of the cases the application). 7 8 9 10 5 5 5 5 S F F F 7

Key information recorded for Error Log: MACHINE OS_USER SESSION_USER DATE MODULE FUNCTION ORIGINATION_TYPE ERROR_CODE PARAMETER_LIST CONTEXT 8

Avoid hard-coding of -20,NNN Errors PACKAGE errnums IS en_general_error CONSTANT NUMBER := -20000; exc_general_error EXCEPTION; PRAGMA EXCEPTION_INIT (exc_general_error, -20000); Should give numbers names and associate them with named exceptions. en_must_be_18 CONSTANT NUMBER := -20001; exc_must_be_18 EXCEPTION; PRAGMA EXCEPTION_INIT (exc_must_be_18, -20001); en_sal_too_low CONSTANT NUMBER := -20002; exc_sal_too_low EXCEPTION; PRAGMA EXCEPTION_INIT (exc_sal_too_low, -20002); max_error_used CONSTANT NUMBER := -20002; END errnums; 9

Using the standard raise program Rather than have individual programmers call RAISE_APPLICATION_ERROR, simply call the standard raise program. Benefits: Easier to avoid hard-coding of numbers. Support positive error numbers! 10

Raising errors by name Use an error name (literal value). The code compiles now. Later, define that error in the repository. No central point of failure. Downsides: risk of typos, runtime notification of "undefined error." 11

Summary: an Exception Handling Architecture Make sure it is understood how it all works Exception handling is tricky stuff Set standards before start coding It's not the kind of thing can easily added in later stages Use standard infrastructure components Everyone and all programs need to handle errors the same way Take full advantage of error management features. SAVE EXCEPTIONS, DBMS_ERRLOG, DBMS_UTILITY.FORMAT_ERROR_BACKTRACE... Don't accept the limitations of Oracle's current implementation. lots can be done to improve the situation. 12

Trace Handling Tracing should be used for tracking error situations and process analysis during development (But can be activated in production on need from configuration tables). It should be implemented with in PL/SQL packages and has to be used in a strong defined way in every other PL/SQL package/function or procedure. Hence It should be highly configurable and easy to use. All objects belonging to the tracing module need to be placed in a common schema 13

Standards for Trace Handling All PL/SQL code has to conform to the trace handling standards Trace handling using the Tracing package As a minimum trace of procedure entry/exit Use appropriate trace level for the trace statement being implemented Configuring the trace setup (What, When, & Module piece of code need to be traced, at what level and from which machine/host) Reviewing the trace output (Table, File) Documentation and example 14

Levels of Trace Handling Definition of Trace Levels: Trace levels can be defined in a range from 0 to 8, where 0 is the same as "No Trace Trace should be used using pre-defined trace levels for the following parts of the application. Level 1: has to be set in any case of an error, this goes hand in hand with the call of the error handling functions Level 2: at the beginning and end of every procedure or function trace2('entering procedure my_proc'); trace2('leaving procedure my_proc'); Level 3: list of procedure parameters trace3('p_param1: ' p_param1); trace3('p_param2: ' p_param2); Level 4: any local variables trace4('v_start_seq: ' v_start_seq); trace4('v_datestamp: ' to_char(v_datestamp, 'mm.dd.yyyy hh24:mi')); Level 5: before and after calling a sub-procedure trace5('calling my_proc'); my_proc(4711, sysdate, p_error_rec); trace5('return from my_proc'); 15

Levels of Trace Handling Level 6: variables inside loops with depth 1 loop trace6('loop nr. ' to_char(v_loop_counter)); <do something> v_loop_counter := v_loop_counter+1; if ( v_loop_counter > 100 ) then trace6('loop finished'); end if; end loop; Level 7: variables inside loops with depth 2 Level 8: variables inside loops with depth 3 16

Header comments and versions All PL/SQL packages, functions and procedures have to contain a standard header Package header with copyright information Function and procedure header contains: Description and parameter list definition Revision history including author, date, CR, change description Documentation and examples 17

Bottleneck of traditional Exception Handling 18

Hard to avoid code repetition in handlers WHEN NO_DATA_FOUND THEN INSERT INTO errlog VALUES ( SQLCODE, 'No company for id ' TO_CHAR ( v_id ), 'fixdebt fixdebt', SYSDATE, USER ); WHEN OTHERS THEN INSERT INTO errlog VALUES (SQLCODE, SQLERRM, 'fixdebt fixdebt', SYSDATE, USER ); RAISE; END; If every developer writes exception handler code on their own, you end up with an unmanageable situation. Different logging mechanisms, no standards for error message text, inconsistent handling of the same errors, etc. 19

Questions? 20

Authors This presentation was prepared by: Vishal Gupta, 10200 Park Meadows Dr. Lone Tree CO, 80124 Tel: +1.646.203.3520 E-mail: gupt.vishal@gmail.com 21

Recommendations Introduce PL/SQL Coding Guidelines 22

23