【问题标题】:getGeneratedKeys() doesn't work with JNDI in WildFly with FirebirdgetGeneratedKeys() 不适用于 WildFly 和 Firebird 中的 JNDI
【发布时间】:2016-06-17 16:19:37
【问题描述】:

我使用 getGeneratedKeys() 直接调用类,如下所示:

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:生成的密钥 功能不可用,最可能的原因是:ANTLR-Runtime not 在类路径中可用

我正在使用 WildFly 10、Firebird 2.5.5、Jaybird 2.2.9。 antlr-4.5.2-complete.jar 存在于 buildpath 中,也许这不是原因,因为它在更改为 JNDI 方式之前就已经工作了。而且wildfly自带antlr 2.7.7。

【问题讨论】:

    标签: java jndi wildfly firebird jaybird


    【解决方案1】:

    问题可能是类加载之一。当您使用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 的模块描述符(作为依赖项或作为资源)。

    配置(原addederickdeoliveiraleal的问题):

    我创建了一个包含 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 模块中添加了&lt;module name="org.antlr4"/&gt;

    【讨论】:

    • @erickdeoliveiraleal 如果我将您对您的问题的编辑移至我的回答中,有任何异议吗?
    • 谢谢,wildfly 18 和 Jaybird 3.0.8 解决了同样的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多