简体   繁体   English

javax.naming.NameNotFoundException:在WebSphere Liberty上

[英]javax.naming.NameNotFoundException: on WebSphere Liberty

I'm trying to use a datasource configurated as a JNDI on a WebSphere Application Server Liberty however I'm getting the follwoing error: 我正在尝试使用在WebSphere Application Server Liberty上配置为JNDI的数据源,但是出现以下错误:

javax.naming.NameNotFoundException: java:comp/env/jdbc/myapp/master

The configuration for the datasource in Websphere application server is: Websphere应用程序服务器中数据源的配置为:

<dataSource commitOrRollbackOnCleanup="commit" id="jdbc/myapp/master" jdbcDriverRef="ojdbc7" jndiName="jdbc/myapp/master">
        <properties.oracle URL="jdbc:oracle:thin:@127.0.0.1:1521:xe" oracleRACXARecoveryDelay="0" password="xxxxxxxx" user="app_master"> 
         </properties.oracle>
         <connectionManager maxPoolSize="50"/>
    </dataSource>

The connection to database is made via this code inside a servlet (jndi=jdbc/myapp/master): 通过以下代码在servlet(jndi = jdbc / myapp / master)中建立与数据库的连接:

Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup(jndi);
            setConnection(ds.getConnection());
            System.out.println(getConnection().toString() );    

What am I doing wrong? 我究竟做错了什么?

The java:comp/env requires resource reference. java:comp/env需要资源参考。 You have following options to solve that: 您可以使用以下选项来解决此问题:

1) Use resource injection - so instead of looking up it directly (via InitialContext) just add the following in your servlet class 1)使用资源注入-因此,无需直接(通过InitialContext)查找资源,只需在Servlet类中添加以下内容

@Resource(lookup = "jdbc/myapp/master", name="jdbc/myapp/master")
private DataSource dataSource;

2) Define resource reference in your web.xml like 2)在您的web.xml定义资源引用,例如

<resource-ref>
   <description>my datasource</description>
   <res-ref-name>jdbc/myapp/master</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>CONTAINER</res-auth>
</resource-ref>

or you can create just reference also via annotation in the code. 或者您也可以通过代码中的注释来创建引用。

3) Use direct JNDI, without reference (not a Java EE best practice) 3)使用直接JNDI,不参考(不是Java EE最佳实践)

 DataSource ds = (DataSource) initCtx.lookup("jdbc/myapp/master");

In addition to what is already stated in the other answer, you should also check that you have enabled the jndi-1.0 feature. 除了在其他答案中已经说明的内容之外,您还应该检查是否已启用jndi-1.0功能。 This is a common cause of lookups not working in Liberty. 这是查找在Liberty中不起作用的常见原因。

For example, in server.xml, 例如,在server.xml中,

<featureManager>
  <feature>jdbc-4.2</feature>
  <feature>jndi-1.0</feature>
  ... other features that you use
</featureManager>

And if this, too, is not enough to get it working, you should also check the configuration of resources upon which the dataSource depends, such as the jdbcDriver with id of ojdbc7 which is referenced in the config snippet that you provided. 并且,如果这还不足以使其正常工作,则还应该检查dataSource所依赖的资源的配置,例如在提供的config片段中引用的id为ojdbc7的jdbcDriver。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM