org.hsqldb.sample
Class TriggerSample

java.lang.Object
  extended byorg.hsqldb.sample.TriggerSample
All Implemented Interfaces:
Trigger

public class TriggerSample
extends java.lang.Object
implements Trigger

Sample code for use of triggers in hsqldb. SQL to invoke is:

CREATE TRIGGER triggerSample BEFORE|AFTER INSERT|UPDATE|DELETE ON myTable [FOR EACH ROW] [QUEUE n] [NOWAIT] CALL "myPackage.trigClass"
This will create a thread that will wait for its firing event to occur; when this happens, the trigger's thread runs the 'trigClass.fire' Note that this is still in the same Java Virtual Machine as the database, so make sure the fired method does not hang.

There is a queue of events waiting to be run by each trigger thread. This is particularly useful for 'FOR EACH ROW' triggers, when a large number of trigger events occur in rapid succession, without the trigger thread getting a chance to run. If the queue becomes full, subsequent additions to it cause the database engine to suspend awaiting space in the queue. Take great care to avoid this situation if the trigger action involves accessing the database, as deadlock will occur. This can be avoided either by ensuring the QUEUE parameter makes a large enough queue, or by using the NOWAIT parameter, which causes a new trigger event to overwrite the most recent event in the queue. The default queue size is 1024.

Ensure that "myPackage.trigClass" is present in the classpath which you use to start hsql.

If the method wants to access the database, it must establish a JDBC connection.

When the 'fire' method is called, it is passed the following arguments:
fire (String trigName, String tabName, Object row[])
where 'row' represents the row acted on, with each column being a member of the array. The mapping of row classes to database types is specified in /doc/hsqlSyntax.html#Datatypes For implementation at a later date:
1. jdbc:default:connection: URL for JDBC trigger method connections to the database.
2. arguments to the trigger method.
3. Because they run in different threads, it is possible for an 'after' trigger to run before its corresponding 'before' trigger; the acceptability of this needs to be investigated.

Author:
Peter Hudson

Constructor Summary
TriggerSample()
           
 
Method Summary
 void fire(java.lang.String trigName, java.lang.String tabName, java.lang.Object[] row)
          fire method declaration
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TriggerSample

public TriggerSample()
Method Detail

fire

public void fire(java.lang.String trigName,
                 java.lang.String tabName,
                 java.lang.Object[] row)
fire method declaration

This is a sample implementation that simply prints information about the trigger firing.

Specified by:
fire in interface Trigger
Parameters:
trigName -
tabName -
row -


Copyright © 2001 - 2002 HSQL Development Group. All Rights Reserved.