Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>To inject a data source, you probably don't need to be bound to a single data source instance since the database you are connecting to features in the url. Using Guice, it is possible to force programmers to provide a binding to a DataSource implementation (<a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/AbstractModule.html#requireBinding%28java.lang.Class%29" rel="nofollow noreferrer">link</a>) . This data source can be injected into a ConnectionProvider to return a data source.</p></li> <li><p>The connection has to be in a thread local scope. You can even implement your <a href="http://code.google.com/p/google-guice/wiki/CustomScopes" rel="nofollow noreferrer">thread local scope</a> but all thread local connections must be closed &amp; removed from ThreadLocal object after commit or rollback operations to prevent memory leakage. After hacking around, I have found that you need to have a hook to the Injector object to remove ThreadLocal elements. An injector can easily be injected into your Guice AOP interceptor, some thing like this:</p></li> </ol> <pre> protected void visitThreadLocalScope(Injector injector, DefaultBindingScopingVisitor visitor) { if (injector == null) { return; } for (Map.Entry, Binding> entry : injector.getBindings().entrySet()) { final Binding binding = entry.getValue(); // Not interested in the return value as yet. binding.acceptScopingVisitor(visitor); } } /** * Default implementation that exits the thread local scope. This is * essential to clean up and prevent any memory leakage. * * <p>The scope is only visited iff the scope is an sub class of or is an * instance of {@link ThreadLocalScope}. */ private static final class ExitingThreadLocalScopeVisitor extends DefaultBindingScopingVisitor { @Override public Void visitScope(Scope scope) { // ThreadLocalScope is the custom scope. if (ThreadLocalScope.class.isAssignableFrom(scope.getClass())) { ThreadLocalScope threadLocalScope = (ThreadLocalScope) scope; threadLocalScope.exit(); } return null; } } </pre> <p>Make sure you call this after the method has been invoked and closing the connection. Try this to see if this works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload