I have created the data source in Websphere server and its name is myDataSource(JNDI name is oracleDS). Now I want to access it through the stand alone application.
I just wrote the bellow code to access the data sorce.
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
Context initialContext = new InitialContext(pdEnv);
DataSource dataSource = (DataSource) initialContext
.lookup("java:comp/env/jdbc/oracleDS");
But when I run the application I am getting the bellow exception.
Aug 6, 2013 11:33:41 PM null null
SEVERE: javaAccessorNotSet
javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.
Please let me know do I have to do any configuration changes in websphere to access the data source or any other code level changes do I have to do?
Please confirm me can we access the datasource from outside of the container(websphere)?
Update1:
I followd the way which PhilipJ mentioned. Then I am gtting the bellow exception.
SEVERE: Cannot get connection: javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
at com.ibm.ws.naming.jndicos.CNContextImpl.mapNotFoundException(CNContextImpl.java:4365)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1794)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1749)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1500)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:637)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:165)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:436)
at com.nyl.connection.ConnectionUtil.getConnection(ConnectionUtil.java:38)
at com.nyl.main.Main.main(Main.java:9)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:95)
at com.ibm.WsnOptimizedNaming._NamingContextStub.resolve_complete_info(_NamingContextStub.java:506)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2797)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2793)
at com.ibm.ws.naming.util.CommonHelpers.retry(CommonHelpers.java:763)
at com.ibm.ws.naming.jndicos.CNContextImpl.cosResolve(CNContextImpl.java:2791)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1790)
... 8 more
Update2:
I found the way to avoid the exception. The code should be bellow,
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
Context initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("oracleDS");
if (datasource != null) {
connection = datasource.getConnection("admin","admin");
} else {
LOGGER.info("Failed to lookup datasource.");
}
But the problem here is I am giving the database credintials to creat the connection. I don't want to give it. Can any one please let me know how to create the connection without giving the database credintials?
I found the solution for this issue.
When we creat the InitialContext we have to set two environment variables correctly as bellow,
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");
Context initialContext = new InitialContext(pdEnv);
Note:
We can find the listening port of iiop protocol by using the bellow way,
Login to Webaphere admin console => Server => Server Types => Webspher application servers => Click on the name of the server which we use => Communication => Ports.
Then you can find the port for Port name BOOTSTRAP_ADDRESS.
Working code to create the connection using the datasource
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");
Context initialContext = new InitialContext(pdEnv);
DataSource datasource = (DataSource) initialContext.lookup("testDS");
if (datasource != null) {
connection = datasource.getConnection("admin","admin"); // DB credintials
} else {
LOGGER.info("Failed to lookup datasource.");
}
Note: I created the data source in websphere(Version 7) and the JNDI name is testDS
and we have to add com.ibm.ws.ejb.thinclient_7.0.0.jar jar in the class path.
Thanks Sean F and PhilipJ for your support regarding this issue.
DataSource dataSource = (DataSource) initialContext
.lookup("jdbc/oracleDS");
try this way.
look here
Related
I am trying to deploy an ear file to a Weblogic 12 server. The ear file contains a HttpServlet. During deployment, the HttpServlet is trying to initialize and fails with this error:
Target state: deploy failed on Cluster
javax.naming.NameNotFoundException: While trying to lookup
'jdbc.' didn't find subcontext 'jdbc'. Resolved
'' at
weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1180)
I tested the data source connection on the weblogic servers and it is successful.
The data source is defined in a properties file that is being accessed because the datasource name is correct. The webserver URL is also correct.
This is the init() method:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
Context jndiContext = null;
Hashtable ht = new Hashtable();
try
{
PropertyManager.getInstance(PROPS_FILE);
PropertyManager.getInstance();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, PropertyManager.getProperty("WL_WEBSERVER_URL"));
jndiContext = new InitialContext(ht);
ds = (javax.sql.DataSource) jndiContext.lookup(PropertyManager.getProperty("SEC_DATASOURCE"));
}
}
I don't understand why it does not recognize the subcontext of jdbc.
I tried removing jdbc. But the error is a NameNotFoundException without jdbc.
UPDATE
I tried changing the data source name to java:jdbc/scantDS. I received a different NameNotFoundException:
". javax.naming.NameNotFoundException: While
trying to look up /jdbc/scantDS in /app/webapp/Load/421643657.;
remaining name '/jdbc/scantDS' at
weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1180)
The problem was that I was trying to deploy using the wrong port number. I was using the port number that the console application was using and I needed a specific port that has the JNDI.
I'm trying get the data base connection using JNDI Name. I'm sing Websphere application server.
Please find below code.
public class GetDbConnection {
private static Connection getConnection() throws Exception {
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://10.70.52.228:30305/");
Connection connection = null;
try{
Context ctx = new InitialContext(pdEnv);
System.out.println("Initial Context: "+ctx.getNameInNamespace());
DataSource ds = (DataSource) ctx.lookup("jdbc/siperian-mdmtrg01-cmx_ors-ds");
System.out.println("DataSource Object : "+ds.getClass());
connection = ds.getConnection();
System.out.println("Connection established ::" +connection);
}catch(Exception ex){
ex.printStackTrace();
}
return connection; }
I'm getting below error.This is the full print stack trace.
Initial Context: MDMTRG2Cell01/clusters/InformaticaMDM_Cluster
J2CA0036E: An exception occurred while invoking method
setDataSourceProperties on
com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl used by
resource jdbc/siperian-mdmtrg01-cmx_ors-ds :
java.lang.NoClassDefFoundError:
com/ibm/websphere/appprofile/accessintent/AccessIntent
Please help me in this reagrd.
The cause of the problem is that the WebSphere Application Server Java™ virtual machine (JVM) could not find the JDBC JAR file.
Example if we are using DB2 datasource we need DB2 JAR, in websphere classpath that contains the implementation class.
The most likely cause is that the classpath of the JDBC Provider configuration has used a WebSphere Application Server variable, for example, DB2_JDBC_DRIVER_PATH, which has not been set for the corresponding WebSphere Application Server jvm process.
This could also be caused on Unix based servers by the userID not having permission to access the jar that contains the class.
I have wrote Java Client application (bean for Oracle form) to access the data through "jdbcdatasource" created on Weblogic 12c. It works fine when I am running on desktop, but when I am embedding on oracle forms as a bean, it gives following error:
java.lang.ClassNotFoundException: weblogic.jdbc.common.internal.RmiDataSource_12210_WLStub
java bean is an executable jar file includes all the dependency jar file, and it is executed independently by double click.
Here is a code:
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3:" + url + ":7001");
if(sUser != null && sPwd != null){
ht.put(Context.SECURITY_PRINCIPAL, sUser);
ht.put(Context.SECURITY_CREDENTIALS, sPwd);
}
ctx = new InitialContext(ht);
System.out.println("!!! WebLogic Server Connection Done!");
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("myDatasource");
java.sql.Connection conn = ds.getConnection();
System.out.println("!!! DataSource Connection Done!");
In the environment of Oracle forms it connect to the weblogic server but could not access the data source by displaying above error.
Any suggestion?
Just to make things clear to me.
When you say :
"to access the data through "jdbcdatasource" created on Weblogic 12c."
and you code shows:
"javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("myDatasource");"
The lookup parameter value shouldn't be exactly like "jdbcdatasource" instead of "myDataSource' as you stated before or it was just to explain your situation ?
I need to write a simple java class which send messages to a Sonic Topic. I can use the JNDI values for lookup.
Here is the code.
Hashtable<String, String> properties = new Hashtable<>();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sonicsw.jndi.mfcontext.MFContextFactory");
properties.put(Context.PROVIDER_URL, "tcp://Devserver:7002");
properties.put(Context.SECURITY_PRINCIPAL, "username");
properties.put(Context.SECURITY_CREDENTIALS, "password");
properties.put("com.sonicsw.jndi.mfcontext.domain", "dmDEV");
Context jndiContext = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("TopicConnectionFactory");
Topic topic = (Topic) jndiContext.lookup("testtopic");
This throws an error
javax.naming.NameNotFoundException: /testtopic not found in the specified context
When I debug the code, I can see "connectionFactory" variable has following fields and values which are totally different from the values I specify above in properties.
brokerHostName "MyMachine" (id=55)
brokerPort 0
brokerProtocol "tcp" (id=59)
brokerURL "" (id=66)
clientID null
connectID null
defaultPassword "" (id=67)
defaultUserName "Administrator" (id=68)
I need to know how to write a simple Java client to connect to a Sonic topic.
I used the following which resolved my issue. Here is it in case you face the same issue. topic = session.createTopic(topicName);
I'm trying to get an LDAP-Connection with the attributes provided by a glassfish custom-resource.
My jndi settings:
Resourcetype: javax.naming.directory.Directory
Factory-Class: com.sun.jndi.ldap.LdapCtxFactory
Parameters:
java.naming.security.credentials = myPassword
java.naming.security.principal = cn=ldapUser,ou=myOrganization,dc=myDomain,dc=net
URL = ldap://ldapserver/ou=myOrganization,dc=myDomain,dc=net
This is how I get the connection in Java:
Context initCtx = new InitialContext();
DirContext ctx = (DirContext) initCtx.lookup("CMDB2LDAP");
This works perfectly with OpenLDAP but when I try to connect to an AD 2003 I get the following Exception:
javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece]; remaining name ''
When I create my own DirContext and put exactly the same properties in it, the connection works flawlessly.
I would prefer to get the connection settings from glassfish for the sake of easy administration.
Try adding java.naming.referral = follow as another attribute and see if that works.