On 21/01/11 21:49, cowwoc wrote:

Good point. Is there a way to check if a Connection has already been instantiated?

Also, is there a cleaner way of passing an Injector to ConnectionFilter? Right now I am depending upon the fact that GuiceServletContextFilter sets an attribute with the name "Injector.class.getName()". This is an implementation detail that may change in the future. Is there a way for me to pass an Injector from GuiceServletContextFilter as a filter parameter? I saw thought of passing parameters using:

filter("/*").through(ConnectionFilter.class, param);
Just add the following constructor to your filter:

@Inject
ConnectionFilter(Injector injector) {
    // save injector for later use in an instance field
    this.injector = injector;
}

I think the cleaner approach would be rely on Provider<Connection>.
Same problem as before though (connection might be opened just to get closed).

What you might can do is looking at the current request using:

request.getAttribute(Key.get(Connection.class).toString())

But this relies heavily on the implementation details of ServletScopes.REQUEST.

    but the Injector hasn't been instantiated at that point.

Thanks,
Gili

On 21/01/2011 3:38 PM, Willi Schönborn wrote:
You should be aware that this might create and open
a jdbc connection just to close it right away.

On 21/01/11 21:36, cowwoc wrote:
Hi,

Following up on this earlier discussion:
http://groups.google.com/group/google-guice/browse_frm/thread/7252f908ef00638a/9a59803c16a9583e?lnk=gst&q=clean+%40requestscoped#9a59803c16a9583e

What is the best way to clean up a @RequestScoped JDBC connection? Am
I supposed to register a second ServletFilter after GuiceFilter (shown
below) or is there an easier way?

public class ConnectionFilter implements Filter
{
    private ServletContext context;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
        this.context = filterConfig.getServletContext();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse,
                                             FilterChain chain)
        throws IOException, ServletException
    {
        Injector injector = (Injector)
context.getAttribute(Injector.class.getName());
        Connection connection = injector.getInstance(Connection.class);

        try
        {
            chain.doFilter(servletRequest, servletResponse);
        }
        finally
        {
            connection.close();
        }
    }

    @Override
    public void destroy()
    {
    }
}


Thanks,
Gili




--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to