I don’t understand those connection pools?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Joost_Schouten
Posted On:   Thursday, January 31, 2002 12:06 AM

Does anyone have a really clear example on how to set up a connection pool in my controler Servlet's init() method, and how I can access that pool, and pick up a connection from within my dbConnect Bean. If this is not a proper setup, please comment. Because I'm lost. I've been struggling for a while with setting up a pool, and used the com.javaexchange.dbConnectionBroker package. It is possible to make my dbConnect bean a servlet, and make it the supercalss of my controler, so the controler inherits the global pool. But can I call methods in the dbConnect servlet from my controler servlet? I just want to have some input before I throw the entire thing around again.    More>>

Does anyone have a really clear example on how to set up a connection pool in my controler Servlet's init() method, and how I can access that pool, and pick up a connection from within my dbConnect Bean. If this is not a proper setup, please comment. Because I'm lost.


I've been struggling for a while with setting up a pool, and used the com.javaexchange.dbConnectionBroker package. It is possible to make my dbConnect bean a servlet, and make it the supercalss of my controler, so the controler inherits the global pool. But can I call methods in the dbConnect servlet from my controler servlet?


I just want to have some input before I throw the entire thing around again.

   <<Less

Re: I don&#146;t understand those connection pools?

Posted By:   Peter_Karlsson  
Posted On:   Tuesday, February 5, 2002 11:41 PM

Pooling is dependent on the environment if your not writing the pooling yourself. With resin jsp-engine/webbserver a working test class looks like this:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;

public class TestDatabase extends HttpServlet {
DataSource pool;
public void init()
throws ServletException
{
try {
Context env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/test");
if (pool == null)
throw new ServletException("`jdbc/test' is an unknown DataSource");
} catch (NamingException e) {
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection conn = null;
try {
conn = pool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select NAME, PRICE from BROOMS");
out.println("Brooms:");
while (rs.next()) {
out.print(rs.getString(1));
out.print(" ");
out.print(rs.getInt(2));
out.println("");
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw new ServletException(e);
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}
}
About | Sitemap | Contact