Java Reference
In-Depth Information
and loads, the problems were significant. In the IBM database lab, we
worked on early connection pools that were deployed at the application level,
and we had some internally developed technical manuals called red books
dedicated to solving the problem. The Internet brings a whole new level of
uncertainty to this planning process. The total number of users is much
harder to predict, and their browsing habits are uncertain.
7.2.1
Creating and terminating with every access
Listing 7.1 shows our ongoing bulletin board example. Though we'll be
accessing the same database and running the same query with the existing
connection, we create and terminate every connection as needed.
Listing 7.1
Creating and terminating the connection per invocation
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
String url = "jdbc:db2:board";
connection = DriverManager.getConnection(url);
connection.close();
o
Make the
connection.
o Close the connection.
For client / server programs, many times a single program works around this
problem by maintaining an active connection through the entire life cycle of
the application. Internet applications can't do that, because the potential user
population is much larger. Instead, the programmer often decides to create
and close connections as needed. This is the approach that we've chosen for all
of our program examples so far. To improve the process, we'll limit the num-
ber of connections and reuse them.
7.2.2
Solution: Reuse connections with a pool
Mike and I reluctantly gather our gear and start hauling it down the narrow,
winding trail. Twice we see other parties; for the most part, pack mules effortlessly
carry their loads. I glare at my partner accusingly, but Mike, who has made this
run twice before, just smiles. As a partner-in-crime for more than five years, he
knows how much I hate to shoulder my boat. We continue to wind between the
cliffs, over the fallen trees, and beside the increasingly closer cliffs. When we reach
the bottom of the canyon, I expect to be right at the river. Instead, the trail contin-
ues overland. I feel as if I've run a marathon when we finally arrive at the river.
Four hours after leaving the campsite, we put our first paddle in the water.
Search WWH ::




Custom Search