无法从带有前缀 jdbc/ 的 java 访问 weblogic 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18647666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:49:39  来源:igfitidea点击:

Unable to access weblogic datasource from java with prefix jdbc/

javajdbcweblogicdatasource

提问by raoul.nair

I have created two datasource in weblogic with below jndi names

我在 weblogic 中使用以下 jndi 名称创建了两个数据源

  • jdbc/testDatasource
  • testDatasource1
  • jdbc/测试数据源
  • 测试数据源1

I am able to access datasource testDatasource1 using java but while access dataource jdbc/testDatasource i am getting below mention error

我可以使用 java 访问数据源 testDatasource1,但是在访问数据源 jdbc/testDatasource 时,我遇到了下面提到的错误

javax.naming.NameNotFoundException: While trying to lookup 'jdbc.testDatasource' didn't find subcontext 'jdbc'. Resolved '' [Root exception is javax.naming.NameNotFoundException: While trying to lookup 'jdbc.testDatasource' didn't find subcontext 'jdbc'. Resolved '']; remaining name 'jdbc/testDatasource'

it seem that prefix jdbc/ is giving problem.

似乎前缀 jdbc/ 有问题。

回答by Scary Wombat

java is the root JNDI namespace for resources.

java 是资源的根 JNDI 命名空间。

So maybe you need

所以也许你需要

dataSource = (javax.sql.DataSource) context.lookup("java:jdbc/testDatasource");

回答by Michael Laffargue

May sound stupid, but since it happened to me I'll share it.

可能听起来很愚蠢,但既然它发生在我身上,我就分享一下。

When you create the datasource under Weblogic (at least 10.3.4), don't forget to go through the whole configuration process (The Finishbutton is enabled before the end).

在Weblogic(至少10.3.4)下创建数据源时,不要忘记走一遍整个配置过程(Finish按钮在结束前启用)。

On this very last page, you'll be able to activatethe datasource for a server, not only create it.

在最后一页上,您将能够访问activate服务器的数据源,而不仅仅是创建它。

To check if your DB is up you can look the JNDI tree of the server.

要检查您的数据库是否已启动,您可以查看服务器的 JNDI 树。

回答by bvhoang

In some cases, it is caused by the datasource not in the same target with your servers. The servers that are using the datasource should be added to the same targets.

在某些情况下,它是由与您的服务器不在同一目标中的数据源引起的。使用数据源的服务器应该添加到相同的目标。

回答by David

Try the following, make sure the port in Context.PROVIDER_URL is the port of the weblogic server you are going to run your application.

尝试以下操作,确保 Context.PROVIDER_URL 中的端口是您要运行应用程序的 weblogic 服务器的端口。

Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:8002");

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
out.println("Connecting to DB......");
ctx = new InitialContext(ht);
javax.sql.DataSource ds= (javax.sql.DataSource) ctx.lookup ("jdbc/postdbsrc");
conn = ds.getConnection();

回答by KarthikPon

I faced the same issue and it's fixed now :)

我遇到了同样的问题,现在已经解决了:)

The fix is,

解决办法是,

        String DATASOURCE_CONTEXT = prop.getProperty("tcDataSourceContext");
        log("DATASOURCE_CONTEXT.."+DATASOURCE_CONTEXT);
        Properties env = new Properties( ); 
        env.put(Context.INITIAL_CONTEXT_FACTORY,                                                                                                "weblogic.jndi.WLInitialContextFactory"); 
        env.put(Context.PROVIDER_URL,"t3://abc.com:8001");
        Context initialContext = new InitialContext(env);
        DataSource datasource =        (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
        if (datasource != null) {
            conn = datasource.getConnection();
        }
        else{
            log("Failed to lookup datasource.");
        } 

1#. abc.com is the server URL where WebLogic is deployed. 2#. 8001 is the port number where WebLogic Admin server is listening.

1#。abc.com 是部署 WebLogic 的服务器 URL。2#。8001 是 WebLogic 管理服务器正在侦听的端口号。

3#. Make sure the below is configured correctly.

3#。确保以下配置正确。

Wrong one: tcDataSourceContext=java:comp/env/jdbc/datasourcename

错误一:tcDataSourceContext=java:comp/env/jdbc/datasourcename

Correct one: tcDataSourceContext=jdbc/datasourcename

正确一:tcDataSourceContext=jdbc/datasourcename

4#. Also, go to WebLogic server and navigate to /Oracle/Middleware/wlserver_10.3/server/lib/ and execute the below command.

4#。此外,转到 WebLogic 服务器并导航到 /Oracle/Middleware/wlserver_10.3/server/lib/ 并执行以下命令。

Command: java -jar wljarbuilder.jar -profile wlfullclient5

命令:java -jar wljarbuilder.jar -profile wlfullclient5

The above command creates a jar file with all the jar's inside WebLogic server /lib folder and place it in your client java code build path and server/lib folder as well.

上面的命令创建一个 jar 文件,其中包含 WebLogic server /lib 文件夹中的所有 jar,并将其放置在您的客户端 java 代码构建路径和 server/lib 文件夹中。

Hope this helps! Kindly let me know if you have any issues.

希望这可以帮助!如果您有任何问题,请告诉我。

回答by bogdan.rusu

I've faced the same problem. My application is connecting to a Oracle DB that runs on a local Oracle Sql Server (Oracle XE 11g). From other answers I've seen that deleting the Weblogic cache /tmp, /cache and even /logs might work. My problem was that the local service Oracle Service XE was STOPPED. I know it's silly but it might help. So you should do this 3 things:
- clear cache;
- check if the DB server service is running;
- try restarting the WEBLOGIC server; Those things above worked for me.

我遇到了同样的问题。我的应用程序正在连接到在本地 Oracle Sql Server (Oracle XE 11g) 上运行的 Oracle DB。从其他答案中,我看到删除 Weblogic 缓存 /tmp、/cache 甚至 /logs 可能会起作用。我的问题是本地服务 Oracle Service XE 已停止。我知道这很愚蠢,但可能会有所帮助。所以你应该做这三件事:
- 清除缓存;
- 检查数据库服务器服务是否正在运行;
- 尝试重启WEBLOGIC服务器;上面的那些东西对我有用。

回答by vikas.kundz

This error also occurs when we have the same JNDI names defined in two datasources in weblogic (I had created two for testing different properties of Datasource). I had to delete one of them and it worked fine.

当我们在 weblogic 中的两个数据源中定义了相同的 JNDI 名称(我创建了两个用于测试数据源的不同属性)时,也会发生此错误。我不得不删除其中之一,但效果很好。

回答by Kt Mack

In my case, it was due to the datasource was being destroyed during deployment. The solution is to add destroyMethod=""

就我而言,这是由于数据源在部署期间被破坏。解决方法是添加destroyMethod=""

@Bean(name = "dataSource", destroyMethod="")
public DataSource dataSource() {

  JndiDataSourceLookup lookup = new JndiDataSourceLookup();
  return lookup.getDataSource(jndiName);

}

回答by murali y

I also faced same issue in my application while setting up the PROD server.

在设置 PROD 服务器时,我的应用程序中也遇到了同样的问题。

Sol: targeted the data sources in web-logic console (Node or cluster). It works for me.

Sol:针对 Web 逻辑控制台(节点或集群)中的数据源。这个对我有用。

Navigation: In Console: Goto JDBC >Summary of JDBC Data Sources >DataSource1>select the servers or clusters on which you would like to deploy this JDBC data source.

导航: 在控制台中:转到 JDBC > JDBC 数据源摘要 >DataSource1> 选择要部署此 JDBC 数据源的服务器或集群。

回答by Med Ali Difallah

I encountered this exception :

我遇到了这个异常:

javax.naming.NameNotFoundException: While trying to lookup 'jdbc.test_group_ir' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/test_group_ir'
    at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1224)

testing the connection to database from weblogic console gave :

测试从 weblogic 控制台到数据库的连接给出:

 Error weblogic.common.resourcepool.ResourceSystemException: Could not create connection for datasource 'test_group_ir_pool'. The returned message is: ORA-01017: invalid username/password;

It appeard that password was wrong, this won't necessery be your case, as the first exception is generic and may have different causes, some were already explained the previous answers, so I am just adding mine.

看来密码错误,这不一定是你的情况,因为第一个例外是通用的,可能有不同的原因,有些已经在前面的答案中解释过了,所以我只是添加我的。

Fixing password and testing the db connection gave then : enter image description here

修复密码并测试数据库连接然后给出: 在此处输入图片说明