Unable to access weblogic datasource from java with prefix jdbc/
Asked Answered
L

11

12

I have created two datasource in weblogic with below jndi names

  • jdbc/testDatasource
  • testDatasource1

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

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.

Linwoodlinz answered 5/9, 2013 at 23:41 Comment(0)
E
21

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.

Ecclesia answered 17/4, 2014 at 15:50 Comment(2)
Yes, that's most common reason of this error. Navigate into: "Home >Summary of JDBC Data Sources >JDBC Data Source-0" : Target tab and assign server (e.g. AdminServer) as targetBorate
in addition to the above, make sure that the JDNI name does not have any extra spacing, which also throw this error "avax.naming.NameNotFoundException: While trying to lookup "Wiggly
S
8

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

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

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

Stonybroke answered 11/2, 2014 at 16:38 Comment(3)
helped lead me to my answer!Acidify
Worst part is that I saw that button aside the next one and I got a gut feeling but calm myself down saying, "what else could it possibly need for me to define". As soon as I saw your answer I was like... "yeah thats me"Thou
This is actually the answer. You need to target the particular server instance.Verge
R
3

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.

Radiolucent answered 2/5, 2016 at 11:58 Comment(0)
S
1

java is the root JNDI namespace for resources.

So maybe you need

dataSource = (javax.sql.DataSource) context.lookup("java:jdbc/testDatasource");
Sulfonmethane answered 6/9, 2013 at 0:13 Comment(6)
getting same error javax.naming.NameNotFoundException: While trying to lookup 'java:jdbc.testDatasource' didn't find subcontext 'java:jdbc'. Resolved '' [Root exception is javax.naming.NameNotFoundException: While trying to lookup 'java:jdbc.testDatasource' didn't find subcontext 'java:jdbc'. Resolved '']; remaining name 'java:jdbc/testDatasource'Linwoodlinz
Do you want to post you jndi declarations?Sulfonmethane
prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); prop.put(Context.PROVIDER_URL, "t3://localhost:8001/"); prop.put(Context.SECURITY_PRINCIPAL, "weblogic"); prop.put(Context.SECURITY_CREDENTIALS, "welcome1"); Context ctx = new InitialContext(prop); String dsName = "java:jdbc/testDatasource"; DataSource ds=(DataSource)ctx.lookup(dsName);Linwoodlinz
No, this is not the jndi declaration. Look for the xml file.Sulfonmethane
Hi sorry i didnt got you. I am running this in main method may i know where this xml file is located as i haven't created it.Linwoodlinz
Maybe speak to the person who did create it then. You need it to exist in weblogic before you can use it.Sulfonmethane
D
1

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.

Deformity answered 7/10, 2016 at 6:37 Comment(0)
U
1

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

Sol: targeted the data sources in web-logic console (Node or cluster). 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.

Unopened answered 5/11, 2017 at 7:49 Comment(0)
C
0

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.

3#. Make sure the below is configured correctly.

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

Correct one: tcDataSourceContext=jdbc/datasourcename

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

Command: 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.

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

Cable answered 26/2, 2016 at 22:31 Comment(0)
N
0

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

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

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

}
Nomadize answered 9/10, 2017 at 15:38 Comment(0)
I
0

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 :

 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

Irra answered 4/6, 2019 at 14:15 Comment(0)
E
0

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. Make sure that you stop the server befor doing so.

Elixir answered 28/2, 2020 at 10:52 Comment(0)
V
-1

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 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();
Vaticinal answered 10/12, 2015 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.