首页 文章

哪个JNDI数据源名称适用于Wildfly和Websphere?

提问于
浏览
0

我有一个java / spring网络应用程序,需要在Wildfly和Websphere上部署为war文件

该应用程序正在使用带有JNDI名称的数据源:

WebConfig.java 包含:

public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("jdbc/myDS");

        return dataSource;
    }

...并在Websphere上完美运行,其中JNDI数据源名称为jdbc / myDS .

Wildfly JNDI name has to start with 'java:/' or 'java:jboss/'

改变 WebConfig.java 做的工作:

DataSource dataSource = dsLookup.getDataSource("java:/myDS");

哪个JNDI数据源名称在Wildfly和Websphere上都有效(可能在其他应用程序服务器上?)

1 回答

  • 0

    如果使用资源引用进行查找,它们将相对于Liberty和Wildfly中的java:comp / env .

    有两种方法可以定义数据源 . 一个是使用javax.annotation.Resource注释 . 这可以用于类型,方法或字段定义 .

    您也可以使用resource-ref元素在web.xml或ejb-jar.xml中执行此操作:

    <resource-ref>
        <description />
    
        <res-ref-name>myRef</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    如果您在应用程序代码中提供用户标识/密码,则 res-auth 元素应包含 Application

相关问题