如何让 Apache Derby 和 Hibernate 在 Java EE 应用程序中打印英文异常消息?

How to get Apache Derby and Hibernate to print english exception messages in a Java EE application?

我是 运行 一个 Java EE 应用程序,它使用 Hibernate 5.2。10.Final 和 Payara 4.1.1.172 上的 Apache Derby 存储后端。我看到类似

的错误消息
Caused by: java.sql.SQLDataException: A truncation error was encountered trying to shrink VARCHAR () FOR BIT DATA '(Binärer Datenwert wird nicht angezeigt)' to length 255.

这表明 Hibernate 或 Derby 或两者都没有在消息的所有部分使用英语错误消息。

我试过了

我看到异常消息的开头是英文。我希望完整的消息是英文的,包括所有可能的部分。我对解决异常没有兴趣,这是一个例子。我不是在寻找邮件德语部分的翻译。

非英语消息部分很可能是由德语 Ubuntu 18.04 服务器启动引起的。更改 OS 的系统区域设置或服务器可以包装到的容器是一种解决方法,但不是解决方案。

可以在 https://gitlab.com/krichter/derby-embedded-data-source-locale-j4ee and example output of the CI at https://gitlab.com/krichter/derby-embedded-data-source-locale-j4ee/-/jobs/83525395 找到 SSCCE。 SSCCE 仅包含围绕服务器启动的样板文件和导致德语错误消息的简单无效本机查询,没有比问题中提供的更多信息。

如果可能,请与 SSCCE 核实答案。

我的动机是有英文异常消息,以便在开发过程中更容易通过搜索引擎找到解决方案,而不会篡改系统语言。

为什么不直接使用 JAVA_TOOL_OPTIONS 环境变量?设置 export LC_ALL=de_DE.UTF-8 后(在您的 .gitlab-ci.yml 中)您可以设置 export JAVA_TOOL_OPTIONS="-Duser.language=en"enjoy 您的英文错误消息。

关于“-Duser.language”不适用于 derby 的 startNetworkServer:startNetworkServer 是一个执行脚本(以最简单的形式)java -jar derby.jar。因此,您不能简单地执行 bin/startNetworkServer -Duser.language=en。但是如果您查看 startNetworkServer 脚本,您会发现使用了环境变量 DERBY_OPTS。因此,指定 export DERBY_OPTS="-Duser.language=en" 也可能有效,尽管我没有那样尝试。 (正如您的评论所述,它不会

样本.gitlab-ci.yml有效:

main:
    image: ubuntu:18.04
    script:
        - apt-get update && apt-get install --yes language-pack-de wget openjdk-8-jdk maven
        - export LC_ALL=de_DE.UTF-8
        - export JAVA_TOOL_OPTIONS="-Duser.language=en"
        - wget http://www-eu.apache.org/dist//db/derby/db-derby-10.14.2.0/db-derby-10.14.2.0-bin.tar.gz && tar xf db-derby-10.14.2.0-bin.tar.gz
        - cd db-derby-10.14.2.0-bin && bin/startNetworkServer &
        - mvn --batch-mode install
        - java -jar target/derby-embedded-data-source-locale-1.0-SNAPSHOT-jar-with-dependencies.jar

输出:

$ java -jar target/derby-embedded-data-source-locale-1.0-SNAPSHOT-jar-with-dependencies.jar
Picked up JAVA_TOOL_OPTIONS: -Duser.language=en
running main
Exception in thread "main" java.sql.SQLNonTransientConnectionException: The connection was refused because the database database was not found.
        at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
        at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
        at de.richtercloud.derby.embedded.data.source.locale.Main.main(Main.java:12)
Caused by: ERROR 08004: The connection was refused because the database database was not found.
        at org.apache.derby.client.net.NetConnectionReply.parseRDBNFNRM(Unknown Source)
        at org.apache.derby.client.net.NetConnectionReply.parseAccessRdbError(Unknown Source)
        at org.apache.derby.client.net.NetConnectionReply.parseACCRDBreply(Unknown Source)
        at org.apache.derby.client.net.NetConnectionReply.readAccessDatabase(Unknown Source)
        at org.apache.derby.client.net.NetConnection.readSecurityCheckAndAccessRdb(Unknown Source)
        at org.apache.derby.client.net.NetConnection.flowSecurityCheckAndAccessRdb(Unknown Source)
        at org.apache.derby.client.net.NetConnection.flowUSRIDPWDconnect(Unknown Source)
        at org.apache.derby.client.net.NetConnection.flowConnect(Unknown Source)
        at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
        at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl.newNetConnection(Unknown Source)
        ... 4 more
ERROR: Job failed: exit code 1
FATAL: exit code 1

根据您的描述,Derby 显然忽略了 JVM 的用户区域设置(即使在强制时也是如此),而是使用在 OS 级别定义的区域设置。

你说:

Changing the system locale of the OS or a container the server could be wrapped into is a workaround, but not a solution.

我完全理解,但是更改它(使用导出 LC_ALL),不是在容器上,而是仅在启动 Derby 的 shell 脚本内,这不是更可接受的解决方法吗?