1

I'm checking a java webpage code, actually a Liferay portlet's based website.

I was checking the server-side .java files, and in a DAO pattern file, seeing how the programmer dealt with the DB connections. I'm used to Java SE, where you normally get a Connection object calling to the DriverManager class, but here, things are pretty different:

initContext = new InitialContext();
envContext = (Context) initContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("jdbc/SSMoracle");
conn = ds.getConnection();  

Doing this, the object conn gets a proper connection to the DB, and it works perfectly. I've never seen how it works though, especially the Context class.

What does this class do, and why is it used instead of using a class that calls DriverManager to get the proper connection? I would love to know!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Zerok
  • 1,323
  • 1
  • 24
  • 56

1 Answers1

2

It is part of JNDI, the Java Naming and Directory Interface. This is one of the services that a Java EE container offers.

Applications can lookup things like data sources (for database access) in JNDI. An administrator can define and configure a data source in the administration console of the Java EE container.

The lines of code that you have in your question do exactly that: lookup a DataSource through JNDI, and then get a database connection from the DataSource.

Have a look at, for example, the documentation of Apache Tomcat to see how this works when you would use the Tomcat servlet container: JNDI Resources HOW-TO and JNDI Datasource HOW-TO

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thank you! But, why should I use this rather than simply using a connection retrieving abstract class (which uses DriverManager), per example? In other words, do you think it's neccesary to use JNDI in a J2EE project? – Zerok Apr 11 '16 at 10:47
  • Because this gives the administrator a central place to manage database connections (in the admin console of the Java EE server) and because it makes it possible to do [connection pooling](https://en.wikipedia.org/wiki/Connection_pool). – Jesper Apr 11 '16 at 10:53
  • Is connection pooling good enough to improve perfomance, to consider applying JNDI along with connection pooling to every Java EE project? – Zerok Apr 11 '16 at 11:11
  • @Zerok Using connection pooling is common practice in Java EE applications. You should always do this instead of manually opening database connections. Databases have limits on the concurrent number of connections they can handle. Also, opening a connection is relatively slow. – Jesper Apr 11 '16 at 11:15
  • Thank you! This is a very useful fact. Gotta take a look at the JNDI specification. Do you know about some quick start guide to learn how to implement connection pools (and use them) easily? – Zerok Apr 11 '16 at 11:18
  • You usually don't implement the connection pool yourself. The Java EE container provides it for you. You just look up the `DataSource` and get a database connection from there, as the code you posted does. – Jesper Apr 11 '16 at 11:49
  • Note that JNDI and `javax.sql.DataSource` are two different subjects. – Mark Rotteveel Apr 11 '16 at 11:50
  • Just one more question, can someone describe what the second and third lines do? I mean, I don't know what directory is ("java:/comp/env") aiming at, nor ("jdbc/SSMoracle"); – Zerok Apr 12 '16 at 10:32
  • `java:/comp/env` is a standard prefix for JNDI in Java EE applications; `jdbc/SSMoracle` is the name under which the `DataSource` that you are looking up is registered in JNDI. See, for example: [What is java:comp/env?](http://stackoverflow.com/questions/11631839/what-is-javacomp-env) – Jesper Apr 12 '16 at 12:51