Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - DBCP vs JNDI?

I am a newbie and I have created a few simlpe Java Swing applications. I was able to use apache commons DBCP to create a connection pool and access the datasource.

I have recently started to create java web based applciations using JSP and Servlets. I have learnt to use JNDI to access the datasource. I update the XML files and use InitialContext() and lookup("java:comp/env") and that is it!!!! I am using Apache Tomcat as my Servlet/JSP container.

1. But where is the DB connection pool created?

2. If yes, then does that mean JNDI somehow uses the DBCP internally?

When I have to create a DBCP for Swing applications, I had to first create an instance of GenericObjectPool and then create a connection factory object and finally a PoolableConnectionFactory object to create the Datasource which will be used to get a connection.

like image 917
user547453 Avatar asked Dec 27 '22 08:12

user547453


1 Answers

JNDI is a mechanism to pass objects from one part of the system to another (in technical terms across class loaders). This is most useful for classes and interfaces found in the Java Runtime like String or DataSource.

This means that in your case JNDI is just a transport mechanism and you need to have the actual connection pool defined elsewhere. Most web containers have a mechanism for defining a system wide connection pool, and JNDI then allows you to get to it.

like image 98
Thorbjørn Ravn Andersen Avatar answered Dec 28 '22 22:12

Thorbjørn Ravn Andersen