public Tracking(String typeName) { this.typeName = typeName; this.createTrace = new ResourceCreationLocation("This is where the " + typeName + " was allocated"); }
AgnosticLightResultSet selectLightResultSet(Connection c, FinalSQLString finalSql, Object... vs) throws PalantirSqlException { return basicSql.selectLightResultSetSpecifyingDBType( c, finalSql, vs, DBType.getTypeFromConnection(c), null); }
static FinalSQLString getByKey(String key, Connection connection) throws PalantirSqlException { DBType type = DBType.getTypeFromConnection(connection); return getByKey(key, type); }
AgnosticLightResultSet selectLightResultSet(Connection c, RegisteredSQLString sql, Object... vs) throws PalantirSqlException, PalantirInterruptedException { DBType dbType = DBType.getTypeFromConnection(c); return basicSql.selectLightResultSetSpecifyingDBType( c, SQLString.getByKey(sql.getKey(), dbType), vs, dbType, null); }
AgnosticResultSet selectResultSetUnregisteredQuery(Connection c, String sql, Object... vs) throws PalantirSqlException, PalantirInterruptedException { return basicSql.selectResultSetSpecifyingDBType( c, SQLString.getUnregisteredQuery(sql), vs, DBType.getTypeFromConnection(c)); }
AgnosticLightResultSet selectLightResultSetUnregisteredQueryWithFetchSize(Connection c, String sql, @Nullable Integer fetchSize, Object... vs) { return basicSql.selectLightResultSetSpecifyingDBType( c, SQLString.getUnregisteredQuery(sql), vs, DBType.getTypeFromConnection(c), fetchSize); } AgnosticLightResultSet selectLightResultSet(Connection c, String key, Object... vs)
/** * Explicitly passes the given ThreadConfinedProxy to a new thread. If the proxy passed in is not a ThreadConfinedProxy, but is a * different type of proxy that also uses a {@linkplain DelegatingInvocationHandler}, this method will recursively apply to the * delegate. This means that this method can handle arbitrarily nested DelegatingInvocationHandlers, including nested * ThreadConfinedProxy objects. * */ public static void changeThread(Object proxy, Thread oldThread, Thread newThread) { Validate.notNull(proxy, "Proxy argument must not be null"); if (Proxy.isProxyClass(proxy.getClass())) { InvocationHandler handler = Proxy.getInvocationHandler(proxy); changeHandlerThread(handler, oldThread, newThread); } else if (proxy instanceof Delegator) { changeThread(((Delegator) proxy).getDelegate(), oldThread, newThread); } }
@Test public void testPropagateExceptions() throws SQLException { IThingThatThrows thing = ThreadConfinedProxy.newProxyInstance(IThingThatThrows.class, new ThingThatThrows(), ThreadConfinedProxy.Strictness.VALIDATE); assertEquals(1, thing.doStuff(IThingThatThrows.Behavior.RETURN_ONE)); try { thing.doStuff(IThingThatThrows.Behavior.THROW_RUNTIME); fail("Should throw Runtime Exception"); } catch (RuntimeException e) { // OK } try { thing.doStuff(IThingThatThrows.Behavior.THROW_SQL); fail("Should throw SQL Exception"); } catch (SQLException e) { // OK } }
AgnosticResultSet selectResultSet(Connection c, String key, Object... vs) throws PalantirSqlException, PalantirInterruptedException { DBType dbType = DBType.getTypeFromConnection(c); return basicSql.selectResultSetSpecifyingDBType( c, SQLString.getByKey(key, dbType), vs, dbType); }
@Override public void close() { timerKey.stop(); try { stmt.close(); results.close(); hasBeenClosed = true; long elapsed = System.currentTimeMillis() - creationLocation.getCreatingThreadInfo().getTimestamp(); log.debug("Closed {} after {}ms", this, elapsed); } catch(SQLException sqlex) { log.error("Caught SQLException", sqlex); //$NON-NLS-1$ } }
@Test public void testCurrentThreadCanCreateAndUseSubject() { @SuppressWarnings("unchecked") List<String> subject = ThreadConfinedProxy.newProxyInstance(List.class, new ArrayList<String>(), ThreadConfinedProxy.Strictness.VALIDATE); subject.add(testString); assertEquals(testString, Iterables.getOnlyElement(subject)); }
/** * Creates a new ThreadConfinedProxy with the given Strictness (ASSERT_AND_LOG or VALIDATE), initially assigned to the current thread. */ public static <T> T newProxyInstance(Class<T> interfaceClass, T delegate, Strictness strictness) { return newProxyInstance(interfaceClass, delegate, strictness, Thread.currentThread()); }
private static void changeHandlerThread(InvocationHandler handler, Thread oldThread, Thread newThread) { if (handler instanceof ThreadConfinedProxy) { ((ThreadConfinedProxy) handler).changeThread(oldThread, newThread); } if (handler instanceof DelegatingInvocationHandler) { changeThread(((DelegatingInvocationHandler) handler).getDelegate(), oldThread, newThread); } }
public ResourceCreationLocation(String message, Exception cause) { super(message, cause); this.creatingThreadInfo = ThreadInfo.from(Thread.currentThread()); }
private synchronized void changeThread(Thread oldThread, Thread newThread) { checkThreadChange(oldThread, newThread); threadId = newThread.getId(); threadName = newThread.getName(); }
public static ThreadInfo from(Thread t) { return new ThreadInfo(t.getName(), t.getId()); }
/** * Looks up a DBType by name. Names are not case-sensitive. * * @param strName the name of the type to lookup, typically corresponds * to the type name, e.g. DBType.ORACLE.toString(); * @return the property DBType, or null if none exists */ @Nullable public static DBType getTypeByName(@Nullable String strName) { if (strName == null) { return null; } return DBType.valueOf(strName.toUpperCase()); }
private synchronized void checkThreadChange(Thread oldThread, Thread newThread) { if (oldThread.getId() != threadId) { String message = String.format( "Thread confinement violation: tried to change threads from thread %s (ID %s) to thread %s (ID %s), but we expected thread %s (ID %s)", oldThread.getId(), oldThread.getName(), newThread.getId(), newThread.getName(), threadName, threadId); fail(message); } }
@Override protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { checkThread(method); try { return method.invoke(delegate, args); } catch (InvocationTargetException e) { throw e.getCause(); } catch (IllegalAccessException e) { throw Throwables.propagate(e); } }
/** * Creates a new ThreadConfinedProxy with the given Strictness (ASSERT_AND_LOG or VALIDATE), initially assigned to the given thread. */ @SuppressWarnings("unchecked") public static <T> T newProxyInstance(Class<T> interfaceClass, T delegate, Strictness strictness, Thread current) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ThreadConfinedProxy(delegate, strictness, current.getName(), current.getId())); }