I suspect that this is not a good way to do the connection (basically this code is called each time
a request is made from the client. Is there a better way to do this?
Thanks for any help
R.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The classical solution in webapps (with or without Struts) is to use a Filter. It is guaranteed to be initialized exactly once by the container, with the following configuration in the web.xml deployment descriptor:
public class Filtro implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
try {
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/cocus");
Manager.getInstance().setDataSource(dataSource);
} catch (NamingException ne) {
filterConfig.getServletContext().log("Exception initializing datasource", ne);
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
// nothing specific to do here, just pass on
chain.doFilter(request, response);
}
public void destroy() {
}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I'm using sql2java in several webapp.
So far I use a custom Request Processor in order to init the DB connection.
Basically I have something like this:
try {
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/cocus");
Manager.getInstance().setDataSource(dataSource);
} catch (Exception e) {
logger.error("Exception initializing datasource", e);
return false;
}
I suspect that this is not a good way to do the connection (basically this code is called each time
a request is made from the client. Is there a better way to do this?
Thanks for any help
R.
The classical solution in webapps (with or without Struts) is to use a Filter. It is guaranteed to be initialized exactly once by the container, with the following configuration in the web.xml deployment descriptor:
<web-app id="WebApp_ID">
<filter>
<filter-name>Sql2JavaFilter</filter-name>
<display-name>Sql2JavaFilter</display-name>
<filter-class>net.sourceforge.sql2java.Filtro</filter-class>
</filter>
<filter-mapping>
<filter-name>Sql2JavaFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
...
and with the following code for the filter class:
package net.sourceforge.sql2java;
import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.DataSource;
import org.acme.sample.Manager;
public class Filtro implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
try {
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/cocus");
Manager.getInstance().setDataSource(dataSource);
} catch (NamingException ne) {
filterConfig.getServletContext().log("Exception initializing datasource", ne);
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
// nothing specific to do here, just pass on
chain.doFilter(request, response);
}
public void destroy() {
}
}
Thanks!
It worked fine.