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);
but the Injector hasn't been instantiated at that point.
Thanks,
Gili
How do you do that? Can you please post some code that shows
cleaning up Closeables on scope shutdown?
Thank you,
Gili
I would assume that in production at least you are using a connection
pool for which the cost of "opening a connection" is mitigated. When
you do this, you can effectively open/close connections on a
per-statement or per-transaction basis without the performance cost
that would normally imply, instead of using request scoped connection.
One thing your filter does which is convenient, though, is ensure that
your connections are "closed" (i.e., returned to the connection pool)
without having to rely on remembering to do so. A useful library that
provides an alternative is JDBI:
https://github.com/brianm/jdbi
With this you can inject a @Singleton instance of DBI and use it thusly:
dbi.withHandle( new HandleCallback<Void>() {
// the DBI instance will open a connection, wrap in a Handle object and pass
// it to the callback:
public Void withHandle(Handle handle) throws Exception {
// Do your SQL operations using the handle
return null;
}
// Handle is closed by the DBI before returning.
});
It's slightly more code than you'd normally need when using a
Connection object directly, but you can hide all that within a DAO
wrapper object of some kind and inject that instead.
YMMV,
Christopher
2011/1/21 Willi Schönborn <w.scho...@googlemail.com>:
> I successfully used custom scope implementations which check for
> instances which require proper destruction (Closeables, @PreDestroy, ...).
> This requires of course some kind of a strictly defined exit point.
>
> 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);
>>
>> but the Injector hasn't been instantiated at that point.
>>
>> 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 google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
@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.