List of usage examples for com.google.common.base FinalizableWeakReference FinalizableWeakReference
protected FinalizableWeakReference(T referent, FinalizableReferenceQueue queue)
From source file:eu.esdihumboldt.hale.ui.common.definition.ImageHandles.java
/** * Add an image.//w ww . j av a 2 s . co m * * @param image an image that should be disposed when there are no longer * any references to it */ public synchronized void addReference(Image image) { new FinalizableWeakReference<Image>(image, referenceQueue) { @Override public void finalizeReferent() { try { Image image = get(); if (!image.isDisposed()) { image.dispose(); } } catch (Exception e) { // ignore } } }; }
From source file:com.jolbox.bonecp.CachedConnectionStrategy.java
/** Keep track of this handle tied to which thread so that if the thread is terminated * we can reclaim our connection handle. We also * @param c connection handle to track.// www. j av a 2s. c om */ protected void threadWatch(final ConnectionHandle c) { this.threadFinalizableRefs.put(c, new FinalizableWeakReference<Thread>(Thread.currentThread(), this.finalizableRefQueue) { public void finalizeReferent() { try { if (!CachedConnectionStrategy.this.pool.poolShuttingDown) { logger.debug("Monitored thread is dead, closing off allocated connection."); } c.internalClose(); } catch (SQLException e) { e.printStackTrace(); } CachedConnectionStrategy.this.threadFinalizableRefs.remove(c); } }); }
From source file:org.itas.core.dbpool.ConnectionPartition.java
/** This method is a replacement for finalize() but avoids all the pitfalls of it (see Joshua Bloch et. all). * //from w w w .j av a 2s. co m * Keeps a handle on the connection. If the application called closed, then it means that the handle gets pushed back to the connection * pool and thus we get a strong reference again. If the application forgot to call close() and subsequently lost the strong reference to it, * the handle becomes eligible to garbage connection and thus the the finalizeReferent method kicks in to safely close off the database * handle. Note that we do not return the connectionHandle back to the pool since that is not possible (for otherwise the GC would not * have kicked in), but we merely safely release the database internal handle and update our counters instead. * @param connectionHandle handle to watch */ protected void trackConnectionFinalizer(ConnectionHandle connectionHandle) { if (!this.disableTracking) { Connection con = connectionHandle.getInternalConnection(); final Connection internalDBConnection = con; final BoneCP pool = connectionHandle.getPool(); connectionHandle.getPool().getFinalizableRefs().put(con, new FinalizableWeakReference<ConnectionHandle>( connectionHandle, connectionHandle.getPool().getFinalizableRefQueue()) { public void finalizeReferent() { try { pool.getFinalizableRefs().remove(internalDBConnection); if (internalDBConnection != null && !internalDBConnection.isClosed()) { // safety! logger.warn( "BoneCP detected an unclosed connection and will now attempt to close it for you. " + "You should be closing this connection in your application - enable connectionWatch for additional debugging assistance."); // if (!(internalDBConnection instanceof Proxy)){ // this is just a safety against finding EasyMock proxies at this point. internalDBConnection.close(); //} updateCreatedConnections(-1); } } catch (Throwable t) { logger.error("Error while closing off internal db connection", t); } } }); } }
From source file:com.jolbox.bonecp.ConnectionPartition.java
/** This method is a replacement for finalize() but avoids all its pitfalls (see Joshua Bloch et. all). * //from w w w .j av a 2s.co m * Keeps a handle on the connection. If the application called closed, then it means that the handle gets pushed back to the connection * pool and thus we get a strong reference again. If the application forgot to call close() and subsequently lost the strong reference to it, * the handle becomes eligible to garbage connection and thus the the finalizeReferent method kicks in to safely close off the database * handle. Note that we do not return the connectionHandle back to the pool since that is not possible (for otherwise the GC would not * have kicked in), but we merely safely release the database internal handle and update our counters instead. * @param connectionHandle handle to watch */ protected void trackConnectionFinalizer(ConnectionHandle connectionHandle) { if (!this.disableTracking) { // assert !connectionHandle.getPool().getFinalizableRefs().containsKey(connectionHandle) : "Already tracking this handle"; Connection con = connectionHandle.getInternalConnection(); if (con != null && con instanceof Proxy && Proxy.getInvocationHandler(con) instanceof MemorizeTransactionProxy) { try { // if this is a proxy, get the correct target so that when we call close we're actually calling close on the database // handle and not a proxy-based close. con = (Connection) Proxy.getInvocationHandler(con).invoke(con, ConnectionHandle.class.getMethod("getProxyTarget"), null); } catch (Throwable t) { logger.error("Error while attempting to track internal db connection", t); // should never happen } } final Connection internalDBConnection = con; final BoneCP pool = connectionHandle.getPool(); connectionHandle.getPool().getFinalizableRefs().put(internalDBConnection, new FinalizableWeakReference<ConnectionHandle>(connectionHandle, connectionHandle.getPool().getFinalizableRefQueue()) { @SuppressWarnings("synthetic-access") public void finalizeReferent() { try { pool.getFinalizableRefs().remove(internalDBConnection); if (internalDBConnection != null && !internalDBConnection.isClosed()) { // safety! logger.warn("BoneCP detected an unclosed connection " + ConnectionPartition.this.poolName + "and will now attempt to close it for you. " + "You should be closing this connection in your application - enable connectionWatch for additional debugging assistance."); internalDBConnection.close(); updateCreatedConnections(-1); } } catch (Throwable t) { logger.error("Error while closing off internal db connection", t); } } }); } }