Next in my series on Design Patterns and their implementation in PL/SQL – also see previous articles on Dependency Injection of Values, Dependency Injection of Interfaces and the Template Pattern – is a discussion of the Observer Pattern. We will see a fairly elegant implementation of this pattern that provides for a very loosely (runtime) coupled architecture where packages can advise procedures of custom events.
Inside components in your application, whether you built them or someone else did, there may be many things going on. Attempts to update data, calculations, communications with external resources. You may have an interest in specific events inside such a component. You would like to be told about such events when they occur.
The Observer Patterns describes an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically. The Observer pattern is also known as Dependents and Publish-Subscribe. Also see: http://sern.ucalgary.ca/courses/SENG/609.04/W98/lamsh/observerLib.html .
The most explicit example of an implementation of the Observer Pattern in the Oracle Database is through the Database Triggers. You can easily register an interest in DML events that occur on a table, by creating a trigger for the event you are interested in. The database also support system events that you can register an observer for, by creating triggers such as post-create, post-logon and pre-truncate.
You cannot register an observer for less explicit events – business events occurring inside your packages without additional effort. The database does not know about such events. So you need to publish these events to the interested parties yourself. The package is the only one who knows when the event occurs. However, the way interested observers are notified may differ. Let’s look at two ways to implement event publication and consumption. Note: on an even grander scale, we could use Advanced Queues as infrastructure for the events. For this discussion, it would be overkill. Let’s take a look at a simple, elegant and effective implementation of the Observer Pattern in PL/SQL.
Registering Observers directly with the Observable
One way of implementing the Observer Pattern – which is very close to the Java way – is to register observers – event consumers – with the package that generates the events, the observable. Whenever an event occurs, the package will call each of the registered observers, passing along the name and details of the event. How each observer handles the event is of no concern to the package.
A package can publish multiple types of events. Observers can register for a specific event-type or for all events.
Two overloaded procedure to register observers as well as a pair to unregister:
Register_observer and register_observer(p_observer in varchar2). In the first case, the object calling in will be registered itself – using the dbms_utility.format_call_stack whocalledme helper function; it will be assumed that the caller is a package that implements the a notify() procedure with input parameters p_observable in varchar2 – the name of the package that sent the event – p_event_type in varchar2 and p_event_values hashmap with the parameters associated with the event. Unfortunately, PL/SQL does not have the concept of multiple threads – apart from using dbms_job to spawn a background task – so typically the observers will run after each other rather than simultaneously.
Let’s take the HRM_SALARY_RULES package as an example. It can generate a number of events:
- A business rule validation has failed
- The performance of validations has deteriorated below 400 ms
- Someone is attempting a salary decrease
- A salary increase was attempted for the job PRESIDENT
Another interesting event would be: more than 30% of all validations fail. However, that is a conclusion that can only be drawn across all session. When we discuss static variables, we will return to this particular event.
We have the HRM_SALARY_RULES package accept registration of observers in the following way:
Procedures like reg_perf_loss_observer and reg_sal_decrease_observer can be invoked to register an observer for the specific event type. The observer must adhere to a predefined interface – it should be a procedure, stand alone or in a package – that accepts the following parameters:
- p_observable in varchar2 — the name of the package that sent the event
- p_event_type in varchar2
- p_event_values in observer_pattern_helper.hashmap
An unlimited number of observers can be registered for each event type. The observers are stored inside the package in an observer-table, a PL/SQL Collection that is manipulated largely through helper procedures in the generic Observer_Pattern_Helper package.
When specific events occur, such as performance degradation or salary decrease, inside the (observed) package body calls are made to a local procedure on_event. For example:
function verify_salary_raise( p_job in varchar2, p_old_salary in number, p_new_salary in number)<br /> return boolean<br /> is<br /> l_start_time number:= dbms_utility.get_time; -- returns a timestamp in 100ths of a second<br /> l_end_time number;<br /> l_result boolean:= true;<br /> begin <br /> if p_old_salary > p_new_salary<br /> then<br /> on_event<br /> ( C_SALARY_DECREASE_EVENT<br /> , Observer_Pattern_Helper.key_value_string_to_hashmap<br /> ( 'old_salary='||p_old_salary||';'<br /> ||'new_salary='||p_new_salary||';'<br /> ||'user='||USER||';'<br /> )<br /> );<br /> end if;<br /> â€¦<br /> l_end_time:= dbms_utility.get_time;<br /> -- test for total validation time; if more than 400 ms, fire event<br /> if (l_end_time - l_start_time) > 40 -- 40 * one 100th of a second<br /> then<br /> on_event<br /> ( C_PERFORMANCE_LOSS_EVENT<br /> , Observer_Pattern_Helper.key_value_string_to_hashmap<br /> ( 'validation_time='||10*(l_end_time - l_start_time)||' ms;'<br /> )<br /> ); <br /> end if;<br /> â€¦<br /><br />
This on_event procedure calls another generic procedure – notify_observers – in the helper package Observer_Pattern_Helper, passing the event type, the internally held list of registered observers, the observed package and a list of parameters associated with the event.
procedure on_event<br /> ( p_event_type in varchar2<br /> , p_event_parameters in Observer_Pattern_Helper.hashmap<br /> ) is<br /> begin<br /> Observer_Pattern_Helper.notify_observers<br /> ( p_event_type => p_event_type<br /> , p_event_parameters => p_event_parameters<br /> , p_observable => 'HRM_SALARY_RULES'<br /> , p_observers => g_observers<br /> );<br /> end on_event; <br />
This procedure in turn will make calls to all observers for the current event:
procedure notify_observers<br /> ( p_event_type in varchar2<br /> , p_event_parameters in hashmap<br /> , p_observable in varchar2<br /> , p_observers in observer_table_t<br /> ) is<br /> i number;<br /> begin<br /> set_ hashmap(p_hashmap => p_event _parameters);<br /> -- notify all registerd observers that are interested in this event_type<br /> i := p_observers.FIRST; <br /> WHILE i IS NOT NULL LOOP<br /> if nvl(p_observers(i).event_type, p_event_type) = p_event_type<br /> then<br /> execute immediate <br /> 'begin<br /> '||p_observers(i).observer<br /> ||' ( p_observable => :1<br /> , p_event_type => :2<br /> , p_event_values => Observer_Pattern_Helper.get_hashmap<br /> );<br /> end;'<br /> using in p_observable<br /> , in p_event_type<br /> ;<br /> end if;<br /> i := p_observers.NEXT(i); -- get subscript of next element<br /> END LOOP;<br /> end notify_observers;
Let’s see this in action. We register an observer for the PERFORMANCE_LOSS_EVENT in package HRM_SALARY_RULES. Then we have this package validate a salary change. Since we built in a deliberate sleeptime into the validation procedure, the performance loss event will occur and the observers will be notified:
declare<br />procedure test_observer_pattern<br /> is<br /> procedure test_job_sal_raise<br /> ( p_job in varchar2<br /> , p_old_sal in number<br /> , p_new_sal in number<br /> ) is<br /> begin<br /> if hrm_salary_rules.verify_salary_raise(p_job => p_job,p_old_salary => p_old_sal ,p_new_salary => p_new_sal)<br /> then<br /> print( 'Salaryraise for '||p_job||' from '||p_old_sal||' to '||p_new_sal||' is approved.'); <br /> else<br /> print( 'Salaryraise for '||p_job||' from '||p_old_sal||' to '||p_new_sal||' is NOT approved.'); <br /> end if;<br /> end;<br /><br /> begin<br /> Performance_Monitor.set_logger('LOGGER.LOG_MESSAGE');<br /> hrm_salary_rules.set_logger('LOGGER.LOG_MESSAGE');<br /> hrm_salary_rules.reg_perf_loss_observer('PERFORMANCE_MONITOR.PERFORMANCE_LOSS_OBSERVER');<br /> test_job_sal_raise('SALESMAN', 1000, 1020);<br /> test_job_sal_raise('SALESMAN', 1000, 1200);<br /> end test_observer_pattern;<br />
Our Performance Monitor is implemented in a fairly simple way: it logs the fact that performance is below expectations to the logger it has been injected with. The output of the above PL/SQL code is now as follows:
Note the message HRM_SALARY_RULES: Performance issue in HRM_SALARY_RULES: validation_time=520 ms that is the result of the Performance Loss Event being broadcast to all Observers, including the Performance Monitor.
We have now implemented a basic framework for the Observer-Observable Design Pattern. Without direct dependencies we can have specific events in our packages cause custom pieces of code to be executed. The packages indicate that they are able to send notifications for specific events. Observers can be registered with these packages. When the events occur, all observers are notified. Since the call is made through dynamic pl/sql, there is no compile time dependency.
In a next installment, we will take a look at a more refined implementation, based on Database Events and Views with Instead-Of triggers. With that approach we can move much closer to the way DML and System events are dealt with by Oracle. It allows for more decoupling between event generator and event consumer. This approach is based on Instead-Of triggers that consume the events. These triggers are registered on an Events View. Packages publish events by inserting (dummy) values into the Events View; these inserts will fire the instead-of triggers that were registered against it.
A combination of these approaches would be a generic observer that is registered with each package that generates events and that passes the events along by doing a fake-insert on a generic events view. Observers can register instead-of triggers on this events-view, no be notified of the events that take place.
Download the PL/SQL Code for this article: plsqlObserverPattern1.zip.