I Need an Analogy: Triggers and Events -


For another question, I am running in a misconception that sometimes happens to be born here. Some question-makers think that triggers view the database as an object.

Is there a good analogy to explain to anyone why this is a faulty comparison, and is the result of the wrong reasons?

If you write a trigger, then every event occurs, because there is no way to register or cancel code segments.

Triggers are a way of crossing the synchronous repetitive argument in the thread of execution (i.e. synchonnysis). Events are a way to postpone the argument later (i.e. the implementation of asynchronenality).

There are exceptions and drawbacks in both cases, but the basic patterns of triggers and callback are contrary to most intentions and implementation. Often does not seem completely immersed in distinction (IMHO, YMMV). : D

These are not the same thing, but they are not unrelated.

In both cases, the mechanism can be described as follows:

  • Some blocks of code declare "interest" for change in the state.
  • Your application affects some changes.
  • The system runs the block of code in response to changes.

Perhaps a database trigger is more like a callback function which is interested in any particular event registered.

There is a similarity here: This incident is a rubber ball that you throw. The trigger is a dog that follows after a thrown ball.

If there is any other difference that is in your mind that makes it "dangerous" (Note: OP has selected the choice of word outside this question) To compare triggers and events, You can describe what you say.


Trigger execution thread is a way of crossing synchronously repetitive arguments (i.e. synchonnysis). The incidents are a means of postponement of the argument until that (ie apply asynchronity).

OK, I see what you say more clearly but I think it is in some cases of implementation. I will not assume that an event handler is to be assigned to itself; It depends on the system you are using. For example, a UNIX Signal Handler has to stop itself from catching a new signal, while it is already dealing with one. But inside a Tomcat container a Java servlet should be thread-protected because it can be called concurrently by multiple threads. They are both different types of event handlers.

Event handler can be synchronous or asynchronous Whether a handler read message in a published / subscription system that was posted recently, but was registering its interest before handler? Or only messages have been posted together?


Another important reason to treat different triggers from the event operator is: I often recommend against doing against anything that triggers out The state affects the database.

For example, sending an email, writing a file, posting on a web service, or forging a process is inappropriate inside the trigger. If the transaction generated from the transcript can be rolled back for any other reason, but you can not roll back those external effects. You can not even use clear transactions, but say that you send an email before the trigger, but the operation fails due to a tap barrier or for some reasons.

Instead, all this work should be done by code in an application, after that someone has confirmed that the SQL operation was successful and the transaction was committed.

It is very bad that people are trying to do inappropriate work inside the trigger, there are senior developers in MySQL, who encourage UDF to read and write data in the memcache. Wow - I just saw this !! Shocking!

Then there is another attempt in an equality, compare the triggers and events for the criminal trial process:

  • There is a charge before the trigger.
  • After the trigger is an indictment.
  • Comité is a conviction after a guilty verdict.
  • Rollback is innocent after an innocent decision.

You only want to put the culprit in jail after being convicted.

  • While one incident is a crime, one incident is a crime.

Comments