BEA WebLogic Server Tuning Guide

4
2.1 Connection Management
A key area of resource usage in multi-tier enterprise applications is the definition and management of connections
between applications and databases.
One simple type of connection is a one-to-one mapping between an application and a database. An application
requests a database connection from the relevant database driver and executes various statements using the
connection. When finished, the application releases the connection.
The following code fragment shows how a typical standalone application might acquire and use JDBC Connections
using the DriverManager interfaces. Java applications (in particular J2EE applications) can also use the Datasource
interfaces to obtain connections. The purpose of the code below is to introduce the user to the management of
connections in application programs prior to the introduction of connection pooling concepts.
An application using the DriverManager interface is provided below, followed by one using the DataSource
interface.
Code Listing 1: Sample JDBC application using DriverManager interface
try
{
String dbdriver = “com.dbvendor.driver”;
String dburl = “jdbc.vendordb.specific-optional-string”;
String username = “scott”;
String password = “tiger”;
// The above values can also be looked up from a JNDI tree
// dynamically to avoid hardcoding them in the source code
Class.forName(dbdriver).newInstance();
Connection dbconn = DriverManager.getConnection(dburl, username, password);
// Execute db statements using the connection
dbconn.close();
}
catch (Exception e)
{
//handle exceptions
}
Code Listing 2: Sample JDBC application using DataSource interface
// As part of application configuration/setup, a Datasource object
// for a database is bound to the JNDI. Now if the database used
// changes from one vendor to another, only the JNDI registration
// part of application environment has to change.
VendorDataSource v = new VendorDataSource();
try
{
Context ctx = new InitialContext();
ctx.bind(“jdbc/warehouseDS”, v)
}
catch (NamingException exc)
{
// handle exceptions
}