简体   繁体   English

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

[英]Unable to access weblogic datasource from java with prefix jdbc/

I have created two datasource in weblogic with below jndi names我在 weblogic 中使用以下 jndi 名称创建了两个数据源

  • jdbc/testDatasource jdbc/测试数据源
  • testDatasource1测试数据源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/ 有问题。

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.使用数据源的服务器应该添加到相同的目标。

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 Finish button is enabled before the end).在Weblogic(至少10.3.4)下创建数据源时,不要忘记走一遍整个配置过程(最后才启用Finish按钮)。

On this very last page, you'll be able to activate the 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 树。

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).我的应用程序正在连接到在本地 Oracle Sql Server (Oracle XE 11g) 上运行的 Oracle DB。 From other answers I've seen that deleting the Weblogic cache /tmp, /cache and even /logs might work.从其他答案中,我看到删除 Weblogic 缓存 /tmp、/cache 甚至 /logs 可能会起作用。 My problem was that the local service Oracle Service XE was STOPPED.我的问题是本地服务 Oracle Service XE 已停止。 I know it's silly but it might help.我知道这很愚蠢,但可能会有所帮助。 So you should do this 3 things:所以你应该做这3件事:
- clear cache; - 清除缓存;
- check if the DB server service is running; - 检查数据库服务器服务是否正在运行;
- try restarting the WEBLOGIC server; - 尝试重启WEBLOGIC服务器; Those things above worked for me.上面的那些东西对我有用。

java is the root JNDI namespace for resources. java 是资源的根 JNDI 命名空间。

So maybe you need所以也许你需要

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

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).当我们在 weblogic 中的两个数据源中定义了相同的 JNDI 名称(我创建了两个用于测试数据源的不同属性)时,也会发生此错误。 I had to delete one of them and it worked fine.我不得不删除其中之一,但效果很好。

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). Sol:针对 Web 逻辑控制台(节点或集群)中的数据源。 It works for me.这个对我有用。

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 数据源的服务器或集群。

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#. 1#。 abc.com is the server URL where WebLogic is deployed. abc.com 是部署 WebLogic 的服务器 URL。 2#. 2#。 8001 is the port number where WebLogic Admin server is listening. 8001 是 WebLogic 管理服务器正在侦听的端口号。

3#. 3#。 Make sure the below is configured correctly.确保以下配置正确。

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

Correct one: tcDataSourceContext=jdbc/datasourcename正确一:tcDataSourceContext=jdbc/datasourcename

4#. 4#。 Also, go to WebLogic server and navigate to /Oracle/Middleware/wlserver_10.3/server/lib/ and execute the below command.此外,转到 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.如果您有任何问题,请告诉我。

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);

}

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 :修复密码并测试数据库连接然后给出: 在此处输入图片说明

What worked for me was clearing the WebLogic cache by deleting the Temp folder in C:\\Oracle\\Middleware\\Oracle_Home\\user_projects\\domains\\base_domain\\servers\\AdminServer .对我C:\\Oracle\\Middleware\\Oracle_Home\\user_projects\\domains\\base_domain\\servers\\AdminServer是通过删除C:\\Oracle\\Middleware\\Oracle_Home\\user_projects\\domains\\base_domain\\servers\\AdminServer的 Temp 文件夹来清除WebLogic缓存。 Make sure that you stop the server befor doing so.确保在这样做之前停止服务器。

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();

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

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