getGeneratedKeys() 不适用于带有 Firebird 的 WildFly 中的 JNDI
getGeneratedKeys() doesn't work with JNDI in WildFly with Firebird
我使用 getGeneratedKeys() 和直接 class 调用,如下所示:
public static Connection getConnection() throws Exception {
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
String sql = "jdbc:firebirdsql:localhost/3050:e:\COMPLEXO140116.FDB?defaultResultSetHoldable=True&encoding=WIN1252";
return DriverManager.getConnection(sql, "SYSDBA", "masterkey");
} catch (ClassNotFoundException e) {
throw new SQLException("Driver nao localizado.");
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Erro na base de dados." + e.getMessage() + " fim msg");
}
}
它工作正常,但在我更改为
之后
public class ConnectionFactory {
private static DataSource dataSource;
static {
try {
dataSource = (DataSource) new InitialContext().lookup("java:jboss/Firebird");
} catch (NamingException e) {
throw new ExceptionInInitializerError("'jndifordbconc' not found in JNDI");
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
它停止工作并给出错误:
org.firebirdsql.jdbc.FBDriverNotCapableException: Generated keys
functionality not available, most likely cause: ANTLR-Runtime not
available on classpath
我正在使用 WildFly 10、Firebird 2.5.5、Jaybird 2.2.9。 antlr-4.5.2-complete.jar 出现在构建路径上,也许这不是原因,因为它在更改为 JNDI 方式之前一直在工作。并且 wildfly 带有它自己的 antlr 2.7.7.
问题可能是类加载问题之一。当您使用 DriverManager.getConnection
时,连接是在您的应用程序的上下文中创建的,但是当您使用数据源时,连接是在应用程序服务器的上下文中创建的,而不是您当前的应用程序。因此,如果 antlr-runtime 在应用程序服务器本身的类路径中不可用,则生成的密钥功能不可用。
Jaybird 需要 antlr-runtime 版本 3.4,据我所知,它不是 antlr 2.7.7 的一部分。 antlr-complete 版本 4.5.2 包含与 antlr-runtime 3.4 兼容的 类(有趣的是,antlr-runtime 4.5.2 没有!),所以如果这些 类 是你的一部分应用程序,那么这将解释为什么它在您的应用程序内部创建连接时起作用。
要在从 JNDI 创建连接时使其正常工作,您需要将 antlr-runtime 添加到 Jaybird 的模块描述符(作为依赖项或资源)。
配置(原来added to the question by erickdeoliveiraleal):
我创建了一个文件夹,里面有 antlr-complete 和一个新的 module.xml,代码如下:
<module xmlns="urn:jboss:module:1.3" name="org.antlr4">
<properties>
<property name="jboss.api" value="private"/>
</properties>
<resources>
<resource-root path="antlr-4.5.2-complete.jar"/>
</resources>
<dependencies>
</dependencies>
</module>
并在 Firebird 模块中添加了 <module name="org.antlr4"/>
。
我使用 getGeneratedKeys() 和直接 class 调用,如下所示:
public static Connection getConnection() throws Exception {
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
String sql = "jdbc:firebirdsql:localhost/3050:e:\COMPLEXO140116.FDB?defaultResultSetHoldable=True&encoding=WIN1252";
return DriverManager.getConnection(sql, "SYSDBA", "masterkey");
} catch (ClassNotFoundException e) {
throw new SQLException("Driver nao localizado.");
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Erro na base de dados." + e.getMessage() + " fim msg");
}
}
它工作正常,但在我更改为
之后public class ConnectionFactory {
private static DataSource dataSource;
static {
try {
dataSource = (DataSource) new InitialContext().lookup("java:jboss/Firebird");
} catch (NamingException e) {
throw new ExceptionInInitializerError("'jndifordbconc' not found in JNDI");
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
它停止工作并给出错误:
org.firebirdsql.jdbc.FBDriverNotCapableException: Generated keys functionality not available, most likely cause: ANTLR-Runtime not available on classpath
我正在使用 WildFly 10、Firebird 2.5.5、Jaybird 2.2.9。 antlr-4.5.2-complete.jar 出现在构建路径上,也许这不是原因,因为它在更改为 JNDI 方式之前一直在工作。并且 wildfly 带有它自己的 antlr 2.7.7.
问题可能是类加载问题之一。当您使用 DriverManager.getConnection
时,连接是在您的应用程序的上下文中创建的,但是当您使用数据源时,连接是在应用程序服务器的上下文中创建的,而不是您当前的应用程序。因此,如果 antlr-runtime 在应用程序服务器本身的类路径中不可用,则生成的密钥功能不可用。
Jaybird 需要 antlr-runtime 版本 3.4,据我所知,它不是 antlr 2.7.7 的一部分。 antlr-complete 版本 4.5.2 包含与 antlr-runtime 3.4 兼容的 类(有趣的是,antlr-runtime 4.5.2 没有!),所以如果这些 类 是你的一部分应用程序,那么这将解释为什么它在您的应用程序内部创建连接时起作用。
要在从 JNDI 创建连接时使其正常工作,您需要将 antlr-runtime 添加到 Jaybird 的模块描述符(作为依赖项或资源)。
配置(原来added to the question by erickdeoliveiraleal):
我创建了一个文件夹,里面有 antlr-complete 和一个新的 module.xml,代码如下:
<module xmlns="urn:jboss:module:1.3" name="org.antlr4">
<properties>
<property name="jboss.api" value="private"/>
</properties>
<resources>
<resource-root path="antlr-4.5.2-complete.jar"/>
</resources>
<dependencies>
</dependencies>
</module>
并在 Firebird 模块中添加了 <module name="org.antlr4"/>
。