Use JNDI access to connection pools for data sources, Java messaging services, mail sessions, and URL connections
Understanding JNDI
JNDI is an application programming interface (API) or library that provides an application with methods to associate names with objects and to find objects in the directory based on the name of the object. This article is the first part of a series of tutorials that will give you an idea of how Geronimo, Jndi, and data source connection pools are interconnected, how to build data source connections, and how to leverage JNDI in a simple Geronimo application called the Customer Service Utility) to access the connection.
A data source is an object or factory that is used to generate a connection to an actual data source (typically a database) within an application. In Java Platform, Enterprise Edition (Java EE), this data source refers to a database connection pool, a set of readily available database connections. The reason for creating a data source or database pool is that it can improve application performance, because even opening a connection can be very CPU intensive.
The Customer Service utility (available in the Downloads section of this article) is a simple WEB application that allows basic customer information to be saved to the database. You will use the Apache Ant 1.6.5 and Java 1.4.2_10 to build the utility, deploy it to Geronimo 1.1 with Tomcat, and use the Apache Derby database that is bound to Geronimo. The next section describes how to use the Geronimo console to create this database and data source.
Set up Geronimo
The Geronimo console can manage Java EE components in a user-friendly manner. When Geronimo is started, you can access the Geronimo console located in Http://localhost:8080/console. The default user name and password are system and manager respectively.
Start Step:
Click the DB Manager link located in the lower-left corner of the Console navigation panel. This link represents the Derby database manager.
Create the customer service database by typing customerservicedatabase in the Create DB field, and then click the Create button.
Create a database table by selecting Customerservicedatabase in the Use DB pull-down menu.
Then paste the contents of the SQL script shown in Listing 1 into the table and click the Run SQL button. Please note that the SQL script in Listing 1 is included in the source code.
Listing 1. SQL scripts
CustomerService-Part1.sql
create table customers (
customerid varchar(10) primary key,
fullname varchar(30),
emailaddress varchar(30),
interests varchar(100)
);
insert into customers values ('A100','John Doe10','Doe10@work.com','Java,Open Source,
Computer Graphics');
insert into customers values ('b100','Jane Doe20','Doe20@home.net','Budget Travel, New
Zealand, Martial Arts');
The customer service database should now be created in Derby. The database has only one table Customers, and there are only two entries in the table. Figure 1 shows the normally initialized table that appears after clicking the Application link, and then clicking the View Contents link.
Figure 1. Customer Service Database after initialization