Jakarta Commons datbase connection pool eclipse
I had a hard time getting the database connection pool to work with Tomcat 5.5.
In the end this is what I did.
[code]
This is a quick tutorial on what I did to get data pool connections to work with Tomcat 5.5 and jakarta commons.
First you will need to go here and put these files:
C:\Archivos de programa\apache-tomcat-5.5.25\common\lib
classes12.jar also put the same file in there renaming it to classes12.zip
commons-collections-3.2.jar
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
All of those files are included in this package.
Note: the above will only work in 5.5, in tomcat 5.5 and back you will have to put the config in the server.xml file for the Tomcat configuration.
Once you have those files in the library open up a new .war file
The example here is called DataPoolConnection.war and it has the code you need to get a connection.
NOTE: you can change the amount of connections if you want that an app can make, etc…
The most important part of this is that you add a context.xml file in your web app in Eclipse under WebContent -> META-INF -> context.xml
put this in the file:
removeAbandonedTimeout=”30″ maxActive=”100″
maxIdle=”30″ maxWait=”10000″ username=”username”
password=”mypassword”
driverClassName=”oracle.jdbc.driver.OracleDriver”
url=”jdbc:oracle:oci:userdb/passdbll@instancedb”/>
You will need to make a connection class:
package org.app.apache.connection;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnection{
public static Connection getDBConnection() throws SQLException, NamingException{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(”java:comp/env/jdbc/OracleDB”);
return ds.getConnection();
}
}
Make sure that you add the resource ref in the web.xml file:
Then to access the connection class:
package org.app.apache.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.app.apache.connection.DBConnection;
import javax.sql.DataSource;
import java.sql.Connection;
public class TheAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
DBConnection dc = new DBConnection();
if(!dc.getDBConnection().isClosed()){
System.out.println(”connection open: JOHN OPEN”);
}
else{
System.out.println(”connection closed: JOHN CLOSED”);
}
System.out.println(”connection open: JOHN WHAT”);
return mapping.findForward(”success”);
}
}
There you have it your own connection pool.
[/code]