问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
java is the root JNDI namespace for resources.
So maybe you need
dataSource = (javax.sql.DataSource) context.lookup("java:jdbc/testDatasource");
回答5:
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.
回答6:
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.
回答7:
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.
回答8:
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);
}
回答9:
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 :
回答10:
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();
来源:https://stackoverflow.com/questions/18647666/unable-to-access-weblogic-datasource-from-java-with-prefix-jdbc