I am trying to implement data pooling in a Tomcat7 container. My understanding is I have to use the following code to retrieve a data connection
Context initContext;
DataSource datasource = null;
try {
initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
datasource = (DataSource) envContext.lookup("jdbc/bolsms");
} catch (NamingException ex) {
Logger.getLogger(ReceiveC2DMRegistration.class.getName()).log(Level.SEVERE, null, ex);
}
My Question
For Context do I have to import javax.naming.context or org.apache.catalina.Context and for DataSource do I have to import javax.sql.DataSource or org.apache.tomcat.jdbc.pool.DataSource?
Declaring against Tomcat-specific classes/interfaces would make your webapp tight coupled to Tomcat and thus make it unable to run on other servers. You don’t want to have that.
Always declare against standard Java SE/EE classes/interfaces wherever possible. This way your webapp will be portable across all server makes (Tomcat, Glassfish, JBoss AS, Jetty, etc).