Java: Multiple Data Resources inside the same servlet - java

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

Related

JDBC and JNDI code explanation Jave EE

I am new to Java EE world, in my application I would like to connect to the Database. I was able to accomplish this task with the code below, but can someone explains it to me? What each line does?
code:
try {
InitialContext initContext = new InitialContext();
Context env = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) env.lookup("jdbc/test2");
} catch (NamingException e) {
throw new ServletException();
}
I also found out that I can use the annotation below in my JSP using tomcat which accomplish the same result as above. Can I use this annotation with any web server, ex GlassFish or Jboos ?
Anotation code:
#Resource(name = "jdbc/test2")
private DataSource ds;
The Java Naming and Directory Interface™ (JNDI) is an application
programming interface (API) that provides naming and directory
functionality to applications written using the Java™ programming
language. 1
The Context object provides the methods for binding names to objects, unbinding names from objects, renaming objects and listing the bindings.
JDNI performs all naming operations relative to a context. Therefore the JDNI defines an InitialContext, which provides a starting point for naming and directory operations. Once you have an initial context, you can use it to look up other contexts and objects.
Many methods in the JDNI package throw a NamingException when they need to indicate that the operation requested cannot be performed. The JDNI has a rich exception hierarchy stemming from the NamingException class. The class names of the exceptions are self-explanatory and are listed here.
You can use the #Resource annotation to inject resources. You can find more information on the correct use here.
Sources:
https://docs.oracle.com/javase/tutorial/jndi/overview/index.html
http://www.javaworld.com/article/2076888/core-java/jndi-overview--part-1--an-introduction-to-naming-services.html

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.

JNDI Lookup exception

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

what is java:comp/env? [duplicate]

This question already has answers here:
What does java:comp/env/ do?
(3 answers)
Closed 2 years ago.
what is meant by java:comp/env ?
What does the look up like :
Context envContext = (Context)initContext.lookup("java:comp/env");
do ?
I understand that a look-up like :
(DataSource)envContext.lookup("jdbc/MyDatasource")
looks up for the name MyDatasource in the context.xml or web.xml to get the URL of the database. Is it so ? !! But what does the former look up do ?
java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB).
Context envContext = (Context)initContext.lookup("java:comp/env");
allows defining a variable pointing directly to this node. It allows doing
SomeBean s = (SomeBean) envContext.lookup("ejb/someBean");
DataSource ds = (DataSource) envContext.lookup("jdbc/dataSource");
rather than
SomeBean s = (SomeBean) initContext.lookup("java:comp/env/ejb/someBean");
DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/dataSource");
Relative paths instead of absolute paths. That's what it's used for.
It's an in-memory global hashtable where you can store global variables by name.
The "java:" url scheme causes JNDI to look for a javaURLContextFactory class, which is usually provided by your app container, e.g. here is Tomcat's implementation javadoc
See also NamingManager.getURLContext
I know I'm far late, but I was asking the same question, and I think I came some answer. So, if I may put my two cents.
java:comp/env/jdbc/myDataSource
java: is just like jdbc: from connection string. Acts as a protocol.
comp is the root for all JNDI contexts.
env is the subcontext for all resource related. There is another for user. Check this out.
jdbc is the subcontext for jdbc resources. There are types. Check the link from the previous bullet.
myDataSource is the name of your jdbc resource.

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