environment variable is being considered "implicitly relative" by systemd?

55 views
Skip to first unread message

philfrei1

unread,
Jan 23, 2025, 3:53:26 PMJan 23
to H2 Database
I have a Spring Boot project meant to run on a remote server. The project interacts with the H2 database correctly when I run the following command from the ssh shell:
    java -jar shiftstarts-0.0.1.jar

When I run this same command using systemd (sudo systemctl start shiftstarts) I get the following error:
unable to obtain isolated JDBC connection [A file path that is implicitly relative to the current working directory is not allowed in the database URL "jdbc:h2:file:${PATH_TO_SHIFTSTARTSDB}". Use an absolute path...

The application.properties file has the following line:
    spring.datasource.url=jdbc:h2:file:${PATH_TO_SHIFTSTARTSDB}

PATH_TO_SHIFTSTARTSDB is defined as "/var/lib/shiftstarts/ShiftStartDB".

Any thoughts on why the error message turns up on the systemd start but is fine when executing the java -jar command from the ssh shell? Are there additional lines I need to add to the configuration? Is the syntax for the use of the environment variable correct?

What exactly does "implicitly relative" mean in this context? The address given, assuming the environment variable is properly expanded, is absolute, is it not? Is the variable not expanding when running from the systemd wrapper?

I wonder if this might be more a systemd issue than an H2 issue, though, since the expansion only fails (afaik) during the systemd start.

Noel Grandin

unread,
Jan 24, 2025, 1:01:16 AMJan 24
to h2-da...@googlegroups.com
That sounds like our code for checking for relative url is not working great when it contains an environment variable. I am surprised that the environment variable has not been replaced before it gets to the h2 code

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/h2-database/3ae263c6-c5ff-4734-aec8-7062ec84e4fan%40googlegroups.com.

Evgenij Ryazanov

unread,
Jan 24, 2025, 3:27:51 AMJan 24
to H2 Database
Hello!

JDBC URLs passed to database drivers usually can't contain any variables, so there is nothing to do on H2 side.

But Spring Boot provides multiple ways for such configurations:
https://docs.spring.io/spring-boot/reference/features/external-config.html
For example, you can have a configuration file on the system like many other system services have.
System environment variables are also supported, but environment of the service process is not the same as environment of your user, so maybe your variable is not defined for your service or something else is wrong.

philfrei1

unread,
Jan 24, 2025, 10:43:16 PMJan 24
to H2 Database
Thanks for your help, and for the link to the "external-config.html" section. I am having a great deal of difficulty discerning what is relevant to my situation from this document!

I hope to use the same jar for my home/test Ubuntu OS and my remote server Ubuntu OS. I thought that by providing that absolute address via an environment variable I'd be able to locate the .jar and database in appropriate but different locations for the two system. If there is another, more appropriate way to do this...?


"environment of the service process is not the same as environment of your user"
The environment variable "PATH_TO_SHIFTSTARTSDB" is set in /env/environment. This makes the variable visible for the entire system, yes? If so, the environment of the user should not matter, correct?

FWIW here is the shiftstarts.service file. I probably should have posted it from the start of this thread.

[Unit]
Description=Service ShiftStartsUPS
After=syslog.target

[Service]
ExecStart=/usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar /var/lib/shiftstarts-ups/shiftstarts-0.0.1.jar
SuccessExitStatus=143
User=phil

[Install]
WantedBy=multi-user.target

### End of file ###

And what is most puzzling to me, if I simply manually type in the java -jar command that is in the ExecStart, I don't get the error. The program runs as designed. In fact, one can see the point where the environment variable is expanded:

... com.zaxxer.hikari.pool.HikariPool     : HikariPool-1 - Starting...
... com.zaxxer.hikari.pool.HikariPool     : HikariPool-1 - Added connection conn0: url=jdbc:h2:file:/var/lib/shiftstarts-ups/StartTimeDB user=UPSER

In the journalctl for the failed run, we have the following:

... com.zaxxer.hikari.pool.HikariPool     : HikariPool-1 - Starting...
... o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 90011, SQLState: 90011
... o.h.engine.jdbc.spi.SqlExceptionHelper   : A file path that is implicitly relative ... etc.


I will next see if I can locate documentation about "HikariPool".

philfrei1

unread,
Jan 25, 2025, 12:34:20 AMJan 25
to H2 Database
SOLVED: For whatever reason, a system-level environment variable is not accessible for a service running using systemd. Takes me by surprise, I thought system-level environment variables would be visible to all users.

But, one can define the environment variable in the .service file, using either Environment= or EnvironmentFile= . This article explains it pretty well "Using environment variables in systemd units"

After adding the line
    Environment=PATH_TO_SHIFTSTARTSDB=/var/lib/shiftstarts/ShiftStartsDB
the invocation within application.properties is expanding, and the application is running and connecting to the persistent H2 database.
Reply all
Reply to author
Forward
0 new messages