Acquiring a DB connection.

Enterprise Java
Replatforming a Java app
by Thomas Balla and Brian Yost
Listing 1. Acquiring a DB connection.


// Get DB connection
String dataSource = (String) entityContext.
  getEnvironment().get(this.dataSrcProperty);
String userID = (String) entityContext.
  getEnvironment().get(this.userIDProperty);
String password = (String) entityContext.
  getEnvironment().get(this.passwordProperty);

Connection connection = null;

try {
  connection = ((DataSource)
    getContext().lookup(dataSource)).
    getConnection(userID, password);
}
catch (javax.naming.NamingException
          namingException) {
  // [exception handling code omitted
  // for brevity]
  throw new RemoteServerException(...)
}
catch (java.sql.SQLException sqlException) {
  // [exception handling code omitted
  // for brevity]
  throw new RemoteServerException(...)
}