Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC Connection Pooling

 
Reema Patel
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have sufficient experience in Java EE except the EJB part where the heavy weight app servers comes into action. I have made a couple of web apps nd deployed (WAR) on Tomcat. WHat I want 2 do is make a JDBC Connection pool, I browsed through a gud no. of articles but couldn't get it. Following is a code snippet frm JavaRanch journal, the code looks fine but it doesn't explain as how 2 configure it (web.xml-DD entries) on Tomcat:
public void init(ServletConfig config) throws ServletException {
try {
// Look up the JNDI data source only once at init time
Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
datasource = (DataSource) envCtx.lookup("jdbc/MyDataSource");
}
catch (NamingException e) {
e.printStackTrace();
}
}

private Connection getConnection() throws SQLException {
return datasource.getConnection();
}

public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException {
Connection connection=null;
try {
connection = getConnection();
..<do JDBC work>..
}
catch (SQLException sqlException) {
sqlException.printStackTrace();
}
finally {
if (connection != null)
try {connection.close();} catch (SQLException e) {}
}
}
}

Can anybody help me in this regard. I'm using MySQL and Tomcat 5. How can I run the above mentioned code snippet?

Regards,
Reema,
SCJP
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like more of Tomcat configuration issue than a JDBC question.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maximilian Stocker:
This seems like more of Tomcat configuration issue than a JDBC question.

Agreed. Moving to our Tomcat forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic