This article guides you through the JDBC features in the Apache Geronimo appplication server. To demonstrate the JDBC features, we use a simple Inventory application which has JSP, Servlets to handle web related features and inbuilt Derby as database.
Inventory Application will use the Service Provider Interface(SPI) method to access it's database. In this method the application uses a JDBC DataSource interface to establish connections with the database. This is the preferred access method for a J2EE application for several reasons:
- Program code will be totally database independent. Driver information, database location, and configuration parameters are stored in the J2EE Server.
- It allows the use of connection pooling. The J2EE Server connection manager effectively manages
connections to greatly improve performance and scalability.
- It enables the database to be used by Enterprise JavaBeans (EJB) to implement business logic as part of the J2EE Server. Implementing an EJB tier, though not required, lays the foundation for creating a highly scalable, distributed application architecture.
After reading this article you should be able get the best out of the JDBC features of Geronimo, such as defining database pools and using DataSources to access databases.
This article is organized in to following sections.
- Overview of JDBC Features
- Application Overview
- Configuring, Building and Deploying the Sample Application
- Testing of the Sample Application
Overview of JDBC Features
JDBC implementation in application servers vary from application server to other. Following table gives a feature list of JDBC in Apache Geronimo.
Geronimo does not have any direct integration with JDBC but supports access through the generic J2CA framework. The TranQL project has J2CA adapters for various databases.
Geronimo supports the JCA 1.5 specification and is backward compatible to the JCA 1.0 specification.
Data sources supported
TranQL has generic wrappers for each data sources.
Data source failover
TranQL has specialized drivers for certain databases (including Apache Derby, Oracle and DB2) that provide a tighter integration with the advanced features of the driver.
Supports XA transactions, Local Transactions, and No transaction.
Connection Manager Configurability
The J2CA framework is interceptor based which allows different parts of the connection framework to be plugged in.
Transaction support is provided through Geronimo Specific Transaction Managing Framework and HOWL.
Connection pooling and management
Custom Geronimo Code and TranQL used for connection pooling and management.
Legacy driver support
Geronimo provides this through the TranQL- connector JDBC to JCA wrapper in Geronimo. Supports JDBC 3.0 and 2.1.
The Inventory application in this article only supports three basic usecases of such applications.
- Add Items to the Stock
- Receive Items
- Issue Items
The application workflow starts with adding item information to the stock. Then it allows enter goods receiving and issuing information. All those updated information are stored in the inbuilt Derby database.
The Inventory Web Application has following list of pages
- Add Item
- Receive Goods
- Issue Goods
The following figure illustrates the application flow.
Welcome page of the application acting as a notice board which displays current stock of each item. Through the Welcome page users can access Add Item, Receive Goods or Issue Goods Pages. Upon successful completion of each activity, the page will be redirected back to the Welcome page with updated stock information. Add Item helps to define items in the stock, then 0 number of items will be added to the stock. Receive and Issue Goods pages represent Goods Receiving and Issuing activities of the application respectively.
The Inventory application consist of following list of packages and classes.
- Item - represents Item in the Inventory.
- InventoryManager - represents list of services offered by the inventory.
- ItemDAO - contains all database access methods.
- DuplicateItemIdException - custom exception to handle duplication item id scenario.
- NotSufficientQuantityException - Custom exception to handle not sufficient quantity situation.
- DBManager - handle database related activities such as issuing database connections.
- AddItemServlet - dispatch add item information to service layer.
- IssueingServlet - dispatch issuing items information to service layer.
- RecievingServlet - dispatch receiving items information to service layer.
The list of web application files in the application is depicted in the following.
Application defines a datasource with the help of geronimo-web.xml and web.xml files. geronimo-web.xml add a link to the database pool that is packaged with the EAR file.
Following is the web.xml of the Inventory application. It uses same name as in the geronimo-web.xml, which is used to create the datasource.
geronimo-application.xml tells the application that there is a database pool that needs to be deployed as well. The db pool is defined in InventoryPool.xml and the driver that is needs in order to be deployed is the tranql-connector-ra-1.3.rar file--these two files will reside on the top level layer of the resultant EAR file.
Next important phase of the application is accessing defined datasource from the source code. This part is handled by the DBManager class.
The sample database that is being used to demonstrate this application is in-built Derby database. The name of the sample database is InventoryDB and it consists of two tables, namely ITEM and ITEM_MASTER. The fields for each of these tables are described below.
ITEM_ID (PRIMARY KEY)
ITEM_ID (PRIMARY KEY)
The ITEM table stores the data related to the items while ITEM_MASTER stores the quantity in hand of each item.
The tools used for developing and building the Inventory sample application are:
Apache Derby, an Apache DB subproject, is a relational database implemented in Java. Its footprint is so small you can easily embed it in any Java-based solution. In addition to its embedded framework, Derby supports a more familiar client/server framework with the Derby Network Server.
Apache Maven 2
Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather than the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts. The declarative, lifecycle-based approach used by Maven 1 is, for many, a radical departure from more traditional build techniques, and Maven 2 goes even further in this regard. Maven 2 can be download from the following URL:
Back to Top
Configuring, Building and Deploying the Sample Application
Download the Inventory application from the following link:
After decompressing the given file, the inventory directory is created.
You can checkout the source code of this sample from SVN:
Configuration of the application consists of creating the database and defining the connection pool to access it.
Creating and Populating Database
After starting Apache Geronimo log into the console and follow the given steps to create the InventoryDB database.
- Select DB Manager link from the Console Navigation panel on the left.
- Give the database name as InventoryDB and click Create button.
- Select InventoryDB to the Use DB field.
- Open InventoryDB.sql in the inventory/inventory-ear/src/main/resources directory from a text editor.
- Paste the content InventoryDB.sql to the SQL Commands text area and press Run SQL button.
Inventory application comes with an pom.xml script to help users to build from source code. Use the command prompt to navigate into the inventory directory and just give mvn install command to build. It will create the inventory-ear-2.0-SNAPSHOT.ear file under the inventory folder. Now, you are ready to deploy Inventory web application in to the Geronimo Application server.
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
- Click the Deploy New link on the Console Navigation panel.
- Load inventory-ear-2.0-SNAPSHOT.ear file from inventory folder into the Archive input box.
- Press Install button to deploy the application in the server.
Testing of the Sample Application
To test the sample application, open a browser and type http://localhost:8080/inventory. The Welcome page of Inventory application which is acts as a notice board will be loaded.
The user can directly access Add Items, Receive Goods and Issue Goods functionalities from the Welcome page.
This article has shown you how to use JDBC features inside the Geronimo Application Server. You followed step-by-step instructions to build, deploy and test a sample application to elaborate these features.
The highlights of this article are: -
- JDBC features in Apache Geronimo.
- Create a database and populate the data in Geronimo with in built Derby Database.
- Deploy a database pool plan to access a database.
- Deploy web archives to access database via the pool defined in Geronimo.