h2 trigger

141 views
Skip to first unread message

Lian Ort

unread,
May 27, 2022, 10:50:05 AM5/27/22
to H2 Database
Hi,
i want to use a trigger. I have a JUnit test class in my eclipse project where i open a connection to the h2 database file and query tables and so on. In the same package as the JUnit test class i have a public class implementing the Trigger interface.

The connection url to the database file is something like "jdbc:h2:file:....".

The h2 jar is on the eclipse classpath. The h2 database file is in a sub-folder of the eclipse project: src/test/resources/...

When i create the trigger with
stmt.execute("create trigger TEST.trxxx after insert on TEST.TABLE_T1 for each row call \""+TestTrigger.class.getName()+"\"");

i get an class-not-found exception.
Class ""....test.H2Test$TestTrigger"" not found [90086-196]"

I tried several things but i do not understand what is meant with " The class must be available in the classpath of the database engine ".

It would be very nice if someone have a little hint how the classpath of the database engine can be set. it seems to be not the classpath of the eclipse project.

Greetings,
Lian

Evgenij Ryazanov

unread,
May 27, 2022, 10:41:52 PM5/27/22
to H2 Database
Hello!

If you use an embedded database and your application doesn't use multiple classloaders, your trigger needs to be located in its classpath. If it uses multiple classloaders, make sure H2 and this trigger are loaded by the same classloader.
If your tests are loaded separately and other libraries can't see them, you will not be able to load triggers from test cases, you need to move them somewhere else.

If you have a separate H2 server process and your application connects to this server, your triggers should be in classpath of this server.

It's possible to specify a trigger as Java code (CREATE TRIGGER … AS 'source code'), in that case H2 will compile it by itself (H2 will need a working Java compiler, JRE is not enough).

It isn't related to your question, but please note that H2 1.4.196 is an old unsupported version.
Reply all
Reply to author
Forward
0 new messages