2016-03-04 15 views
1

我使用的getGeneratedKeys()直接調用類像這樣:的getGeneratedKeys()不使用JNDI在WildFly工作與火鳥

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,火鳥2.5.5,Jaybird 2 .2.9。 antlr-4.5.2-complete.jar存在於構建路徑中,可能這不是原因,因爲它在更改爲JNDI方式之前正在工作。野蠅攜帶它自己的antlr 2.7.7。

回答

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的模塊描述符中(作爲依賴項或作爲資源)。

的配置(如最初added的問題通過erickdeoliveiraleal):

我創建的文件夾與ANTLR完成內部,並用新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"/>

+0

是的,它解決了我的問題。 – erickdeoliveiraleal

+0

@erickdeoliveiraleal如果我將編輯移動到問題的答案中,是否有任何異議? –

+0

不,任何異議。 – erickdeoliveiraleal