How do I make JNDI names compatible with both GlassFish and WildFly

前端 未结 3 1999
花落未央
花落未央 2021-02-06 06:37

I am developing a Java EE 7 application and have a requirement for the application to be deployed onto application servers running either GlassFish 4.0 or WildFly 8.1.0. The iss

3条回答
  •  情深已故
    2021-02-06 07:06

    I haven't hit the mail-dilemma just yet. But I've ran into the same problem your having when it comes to data source definition and my solution has been to not setup the data sources using the server's console, but make them deployable together with your archive using the @DataSourceDefinition annotation. Turns out WildFly won't complain about java:app/blabla.. if the data source is setup during deployment!

    Here is a real world example for you that works on both GlassFish and WildFly:

    https://github.com/martinanderssondotcom/java-ee-concepts/../ArquillianDS.java

    Note that the data source JNDI name declared is:

    java:app/env/ArquillianDS

    And here is the related persistence.xml file (don't mind the name of the file in this repository, the repository represents a test project that build archives during runtime and the app will change the name of the file in the archive to persistence.xml):

    https://github.com/MartinanderssonDotcom/java-ee-concepts/../persistence-update.xml

    Also note that the persistence unit need a data source located using this JNDI name:

    java:app/env/ArquillianDS

    This deployment works perfectly fine with both GlassFish and WildFly. I've noted that if we declare the data source during deployment, then we pay the price of not seeing the data source listed anywhere in the admin gui/console. For me, that is a small price to pay in order to have a truly portable application. As an added bonus, I don't have to write lengthy installation/setup instructions. For all my projects, the data source is an intrinsic part of the application and I don't mind having a class file in the archive that represents the data source.

    The above data source is using a Java DB (or "Apache Derby" for old school people). As some comments in the ArquillianDS.java file describe: GlassFish has problems using a simple URL connection string combined with Java DB. Hence I resorted to specifying all attributes of the @DataSourceDefinition explicitly. Recently in another project of mine (alas not a public one), I used the same construct of deployment time data source definition but targeting MySQL. Here's that data source definition and it works on both servers:

    @DataSourceDefinition(
            name = "java:app/env/maLivechatDS",
            url = "jdbc:mysql://localhost:3306/malivechat_db?createDatabaseIfNotExist=true&user=root&password",
            className = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
    )
    @ManagedBean
    public class MySQLDataSource { }
    

    Note that the driver is MysqlDataSource and not MysqlXADataSource. One point in my application uses a rather complex transaction scheme and GlassFish ran into problems if I used the XA-driver. However, the non-XA driver used by my application still work properly with JTA transactions so for me, it was just a cheap trick to get the boat floating. You should probably use the XA-driver.

提交回复
热议问题