In web applications database queries are usually made under
program control.
Controlling programs include the Java beans making up
the
model part of the
MVC architecture.
Java programs can make database queries using the
Java DataBase Connectivity (JDBC) interface.
- JDBC makes database connections from within a program.
- For efficiency, database connections must be managed through database
connection pools.
- JDBC accesses connection pools through data sources
Before JDBC can be used in a Java program, connection pools and data
sources must be set up in advance within the
application server, in
our case, Glassfish.
If you have NetBeans open and have already (successfully) run a web
app, then the Glassfish server is running.
If not, you can manually start Glassfish from within the
NetBeans
Services tab by opening the
Servers node,
right clicking
Glassfish Server 3.1.1, and
choosing
Start:
Once the Glassfish server is running, you can get to the web-based
Glassfish administration console by pointing your browser
to
localhost:4848.
You can also get there from the NetBeans interface:
The Glassfish administration console appears as below.
Problem:
Opening a connection to a database can be time consuming; you cannot
open a new connection for every page request.
On the other hand, you cannot open a connection for each user and keep it
open; databases put a limit on open connections.
Solution:
Create a pool of always-open connections and reuse them when necessary:
- "Opening a connection" in a Java program means getting a
connection from the pool
- "Closing a connection" in a Java program means returning the
connection to the pool
This section describes how to use Glassfish to configure a connection
pool.
It assumes that the database has already been created — see
Using Derby in NetBeans.
In the task tree under
Resources open the
JDBC node and
click
JDBC Connection Pools.
You should see some built-in pools listed.
Click
New... to create a new pool:
We will create a connection pool for the
Cars database
called
CarPool.
Make sure the resource type is
javax.sql.Datasource and
the vendor is
Derby. Click
Next.
The general settings, pool settings, and transaction settings can be
left with their default values.
The built-in security mechanism, however, causes problems so we need to
delete it.
Scroll down to the
Additional Properties table,
check
Security Mechanism and click
Delete Properties:
Three property values need to be set:
- DatabaseName (example: Cars)
- User (example: app)
- Password (example: password)
Then click
Finish.
You should now see the new
CarPool listed:
Now test the new pool's connection to the database.
Click on
CarPool to bring you back to the configuration window
and click
Ping.
If you don't see
Ping Succeeded then most likely the database
has not been started (connected to) in NetBeans, or the authentication
information is incorrect.
Database driver vendors (Derby, MySQL, Oracle, etc.) can offer
connection pooling by implementing the
javax.sql.DataSource interface, which is part of the JDBC interface.
A database resource, or data source, hides the details of connection
pooling from the programs that need to access the database.
To use data sources,
- They must first be configured, and
- Web apps must be set up to access them through resource
injection
In the task tree under
Resources open the
JDBC node and
click
JDBC Resources.
You should see some built-in resources listed.
Click
New... to create a new resource:
The
JNDI name must begin with "
jdbc/".
Our example will be
jdbc/cars.
Make sure this resource is connected with the
CarPool. Click
OK.
You should now see the new
jdbc/cars listed:
In order to use a data source, Java code can
inject it in a
manner similar to injecting beans.
All you need is the
JNDI name of a data source and you can
inject it as in this example:
@Resource(name = "jdbc/cars")
private DataSource carsSource; // field is automagically instantiated
...
Connection carsConnection = carsSource.getConnection();
This code obtains a database connection from the
CarPool
associated with
jdbc/cars.
What to do with the connection is the subject of
Introduction to JDBC.