JNDI Lookup exception - java

I have created a dynamic web project in WAS 7.0.0.25. I have configured datasource as jdbc/DWLConfig in the WAS. I am trying to lookup this datasource in the servlet from the web project i have created.
if i give java:comp/env/jdbc/DWLConfig, it is giving me NameNotFoundException. But if i give jdbc/DWLConfig, then it is working fine.
Actually, from the servlet, i am calling another project which i dont have access to edit, always looks like java:comp/env/jdbc/DWLConfig. So it is throwing exception for me.
Do i need to add any reference in the web project which i have created.?

The problem might be related to the base JNDI. You will notice why it is not working from the following example:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Right
DataSource ds = (DataSource) envCtx.lookup("jdbc/DWLConfig");
// Wrong. because base JNDI already created.
DataSource ds = (DataSource) envCtx.lookup("java:comp/env/jdbc/DWLConfig");

Related

Tomcat jdbc pool doesn't work

I am having a slight problem with using JDBC tomcat pool. I have properly defined the resource in context.xml, as well as referring to it in web.xml. Now in my Database access method, I would like to somehow get a data source for when a user get something from the database. However, when I type this in:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/testDB");
I get the error message "InitialContext cannot be resolved to a type". What is the problem here?
Be sure to import InitialContext, it sounds like it is missing to the compiler.

How to get Queue using context.lookup() in oracle 11g

I want to create Queue and the MessageDrivenBean in Oracle 11g.
I created the JMS Module on Weblogic and into it I created Queue and ConnectionFactory.
JDBC names looks like:
Queue: jms/EvZahQueue
ConnectionFactory: jms/ConnectionFactory
I tried to get them with:
Context context = new InitialContext();
connectionFactory = (QueueConnectionFactory) context.lookup("jms/QueueConnector");
queue = (Queue) context.lookup("jms/EvZahQueue");
But, I've got an exception like this:
javax.naming.NameNotFoundException: While trying to look up comp/env/jms/QueueConnector in /app/webapp/registri-view/31900933.; remaining name 'comp/env/jms/QueueConnector'
Also, I tried with:
Context context = new InitialContext();
connectionFactory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/QueueConnector");
queue = (Queue) context.lookup("java:comp/env/jms/EvZahQueue");
And I tried to create default properties and to put them into new InitialContext() but nothing changed.
What should I do? Maybe I need to write something in web.xml, ejb-jar.xml, weblogic-ejb-jar.xml?
From the WebLogic console, Environment -> Servers -> View
JNDI Tree.
From there, find the queue, take note of the nested path where it's
located, then use that path in the lookup.
I resolve the problem.
In Weblogic, I must create subdeployment of my JMS module (Oracle said it is not mandatory, but in my case it seems it is) and then everything works fine.

Java: Multiple Data Resources inside the same servlet

"javax.naming.Context" is commonly used inside Java EE development. It's quite convenient to use it to establish dynamical database connection by calling its lookup function with given names of resources inside context.xml. The sample code is shown as following where "db_name" is the name you used to identify the database resource.
Context ctx = new InitialContext();
DataSource ds = ctx.lookup("java:comp/env/jdbc/db_name");
My concern is what are the differences between lookup resources by using the same context and lookup resources by using different contexts. And which approach makes more sense or suitable? Suppose all database resources are defined inside the same context.xml file. For example:
Context ctx = new InitialContext();
DataSource ds1 = ctx.lookup("java:comp/env/jdbc/db_name_ds1");
DataSource ds2 = ctx.lookup("java:comp/env/jdbc/db_name_ds2");
and
Context ctx_ds1 = new InitialContext();
Context ctx_ds2 = new InitialContext();
DataSource ds1 = ctx_ds1.lookup("java:comp/env/jdbc/db_name_ds1");
DataSource ds2 = ctx_ds2.lookup("java:comp/env/jdbc/db_name_ds2");
Thank you for your sharing.
there is no difference except you created an unnecessary extra java object. however, if the jndi server was remote, you would have created two different network connections and iverhead of managing them - definetly not what one should do.

JAVA Datasource

I am trying to use a datasource that I set up on my weblogic server.
datasource JNDI name = thinOracleDataSource
in my code I have the following
public class DAOBean implements java.io.Serializable {
private Connection conn;
public void connect() throws ClassNotFoundException,
SQLException, NamingException {
Context ctx = new InitialContext();
// Lookup using JNDI name.
DataSource ds = (javax.sql.DataSource) ctx.lookup("thinOracleDataSource");
conn = ds.getConnection();
}
But I get this error
javax.naming.NameNotFoundException: While trying to look up /thinOracleDataSource in /app/webapp/PreAssignment2/24911485.; remaining name '/thinOracleDataSource'
am I looking the JNDI name in the right way? or am I missing something? Thanks for any help!!
EDIT:
This is the jndi tree that I can get from the weblogic console
Try naming your datasource jdbc/thisOracleDataSource in Weblogic and reference it as:
DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/thinOracleDataSource");
Also, make sure the datasource is "targeted" to your weblogic Java server. All of this can be done in the Weblogic admin console.
Your JNDI key should look like approximately "java:comp/env/jdbc/thinOracleDataSource".
You can verify it by using Weblogic console that allows access (and probably search) in JNDI. So, you can check this manually before writing the code.

How to get list of all registered DS names (Object) in JBoss programmatically?

I want to check if same schema registered with two different DS name in JBoss, So
How to get list of all registered DS names (Object) in JBoss programmatically?
You can list all Objects bound to the JBoss (JNDI) via following method:
Context initCtx = new InitialContext();
NamingEnumeration<NameClassPair> namedEnum = initCtx.list("java:comp/env");
Then afterwards you may filter by className in NameClassPair
See API
By using JNDI, something like this.
Context context = new InitialContext();
context.listBindings("java:comp/env");

Categories