Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

18.3.1 Creating a Database Event Trigger

The syntax used to create these triggers is quite similar to that used for DDL triggers:

1 CREATE [OR REPLACE] TRIGGER trigger name

2 {BEFORE | AFTER} {database event} ON {DATABASE | SCHEMA}

3 DECLARE

4 Variable declarations

5 BEGIN

6 ... some code...

7 END;

There are restrictions regarding what events can be combined with what BEFORE and AFTER attributes. Some situations just don't make sense:

No BEFORE STARTUP triggers

Even if such triggers could be created, when would they fire? Attempts to create triggers of this type will be met by this straightforward error message:

ORA-30500: database open triggers and server error triggers cannot have BEFORE type

No AFTER SHUTDOWN triggers

Again, when would they fire? Attempts to create such triggers are deflected with this message:

ORA-30501: instance shutdown triggers cannot have AFTER type

No BEFORE LOGON triggers

It would require some amazingly perceptive code to implement these triggers: "Wait, I think someone is going to log on—do something!" Being strictly reality-based, Oracles stops these triggers with this message:

ORA-30508: client logon triggers cannot have BEFORE type

No AFTER LOGOFF triggers

"No wait, please come back! Don't sign off!" Attempts to create such triggers are stopped with this message:

ORA-30509: client logoff triggers cannot have AFTER type

No BEFORE SERVERERROR

These triggers would be every programmer's dream! Think of the possibilities . . .

CREATE OR REPLACE TRIGGER BEFORE_SERVERERROR

BEFORE SERVERERROR ON DATABASE

BEGIN

diagnose_impending_error;

fix_error_condition;

continue_as_if_nothing_happened;

END;

Unfortunately, our dreams are shattered by this error message:

ORA-30500: database open triggers and server error triggers cannot have BEFORE type

18.3.2 The startup Trigger

Startup triggers execute during database startup processing. This is a perfect place to perform housekeeping steps, such as pinning objects in the shared pool so that they do not "age out" with the least-recently-used algorithm.

In order to create startup event triggers, users must have been granted the ADMINISTER DATABASE TRIGGER privilege.

Here is an example of creating a STARTUP event trigger:

CREATE OR REPLACE TRIGGER startup_pinner

AFTER STARTUP ON DATABASE

BEGIN

pin_plsql_packages;

pin_application_packages;

END;

18.3.3 The shutdown Trigger

BEFORE SHUTDOWN triggers execute before database shutdown processing is performed. This is a great place to gather system statistics. Here is an example of creating a SHUTDOWN event trigger:

CREATE OR REPLACE TRIGGER after_shutdown

AFTER SHUTDOWN ON DATABASE

BEGIN

gather_system_stats;

END;

SHUTDOWN triggers execute only when the database is shut down using NORMAL or IMMEDIATE mode. They do not execute when the database is shut down using ABORT mode or when the database crashes.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]