If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > connection pooling

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-23-03, 09:09
ludmann ludmann is offline
Registered User
 
Join Date: Nov 2003
Posts: 2
connection pooling

connection pooling with dbcp
I am building an application where I need to use connection pooling. I am using Tomcat and mysql. I have previously used connection pooling based on an example, and it used Struts GenericDataSource. This worked well, however I read that it is not production quality, so I am trying to modify it.

Because Tomcat 4.1 now included the commons dbcp, I thought that I will just replace Struts's GenericDataSource with dbcp's BasicDataSource. I am trying not to modify the server.xml and include the parameters for the servlet in the web.xml

The problem is that my java file does not find the org.apache.commons.dbcp.BasicDataSource. All the commons jar files are in the common/lib directory of tomcat. I also placed them in the classpath and in the applications lib directory (just in case)

Can anyone help?

My code looks like this and the DBInitServlet is initialised when the application is uploaded. This and all parameters are specified in the web.xml

package dbase;

//From Beginning JSP Web Development, 2001 Wrox, ISBN 1861002-09-2

import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.sql.SQLException;
import java.sql.Connection;
import javax.sql.DataSource;
//import org.apache.struts.util.GenericDataSource;

import org.apache.commons.dbcp.BasicDataSource;


public class DBInitServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {



super.init(config);
try {
BasicDataSource ds = new BasicDataSource();

ds.setDriverClassName(getInitParameter("driverClas s"));
ds.setUrl(getInitParameter("jdbcURL"));
ds.setMaxActive(Integer.parseInt(getInitParameter( "maxActive")));
ds.setMaxWait(Integer.parseInt(getInitParameter("m axWait")));
ds.setDefaultAutoCommit(false);

ds.getConnection();

ConnectionPool.init(ds);

} catch (SQLException e) {
e.printStackTrace();
throw new ServletException("Unable to open datasource");
}
}
}



---

package dbase;

import java.sql.SQLException;
import java.sql.Connection;
import javax.sql.DataSource;

public class ConnectionPool {

private DataSource ds;
private static ConnectionPool self;

private ConnectionPool(DataSource ds) {
this.ds = ds;
}

public static void init(DataSource ds) {
self = new ConnectionPool(ds);
}

public static ConnectionPool getInstance() {
if (self == null) {
throw new IllegalStateException("Pool not initialised");
}
return self;
}

public Connection getConnection() throws SQLException {
return (ds.getConnection());
}

}


Thank you, Marika Ludmann
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On