/** * Removes the last added sql from the thread local stack. We don't need the String object here. */ protected void removeSql() { sqlThreadLocalStack.pop(); }
/** * This method adds an SQL String to the current thread local stack. This is needed so that * created prepared statements can be associated to the SQL Strings. * <p> * So if three times the prepared statement method is called with the same string, the stack * contains the string three times. Now the Prepared Statement is created which results in * calling the {@link #addPreparedStatement(Object, String)} method. The last added String is * taken and associated with the object. * * @param sql * The SQL String. */ protected void addSql(String sql) { sqlThreadLocalStack.push(sql); }
/** * Adds a prepared statement to this storage for later retrieval. * * @param object * The object which will be the key of the mapping. */ public void addPreparedStatement(Object object) { String sql = sqlThreadLocalStack.getLast(); preparedStatements.put(object, new QueryInformation(sql)); if (LOG.isDebugEnabled()) { LOG.debug("Recorded prepared sql statement: " + sql); } }
@Test(dependsOnMethods = "emptyStack") public void getAndRemoveFirst() { Object objectOne = mock(Object.class); Object objectTwo = mock(Object.class); Object objectThree = mock(Object.class); threadLocalStack.push(objectOne); threadLocalStack.push(objectTwo); threadLocalStack.push(objectThree); assertThat(threadLocalStack.getAndRemoveFirst(), is(objectOne)); assertThat(threadLocalStack.getAndRemoveFirst(), is(objectTwo)); assertThat(threadLocalStack.getAndRemoveFirst(), is(objectThree)); verifyZeroInteractions(objectOne); verifyZeroInteractions(objectTwo); verifyZeroInteractions(objectThree); }
@BeforeMethod(firstTimeOnly = true) public void initTestClass() { threadLocalStack = new ThreadLocalStack<Object>(); }
@Test public void emptyStack() { Object object = threadLocalStack.get(); assertThat(object, is(notNullValue())); assertThat(object, is(instanceOf(LinkedList.class))); }
@Test(dependsOnMethods = "emptyStack", invocationCount = 10, threadPoolSize = 10) public void stackTest() { Object objectOne = mock(Object.class); Object objectTwo = mock(Object.class); Object objectThree = mock(Object.class); threadLocalStack.push(objectOne); threadLocalStack.push(objectTwo); threadLocalStack.push(objectThree); Object returnValueOne = threadLocalStack.pop(); Object returnValueTwo = threadLocalStack.pop(); Object returnValueThree = threadLocalStack.pop(); assertThat(returnValueOne, is(objectThree)); assertThat(returnValueTwo, is(objectTwo)); assertThat(returnValueThree, is(objectOne)); verifyZeroInteractions(objectOne); verifyZeroInteractions(objectTwo); verifyZeroInteractions(objectThree); }
/** * {@inheritDoc} */ @Override public void beforeBody(long methodId, long sensorTypeId, Object object, Object[] parameters, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); if (enabled) { threadCpuTimeStack.push(Long.valueOf(threadMXBean.getCurrentThreadCpuTime())); } }
@Test(dependsOnMethods = "emptyStack", expectedExceptions = { NoSuchElementException.class }) public void noSuchElement() { threadLocalStack.pop(); }
/** * {@inheritDoc} */ @Override public void firstAfterBody(long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); if (enabled) { threadCpuTimeStack.push(Long.valueOf(threadMXBean.getCurrentThreadCpuTime())); } }
/** * {@inheritDoc} */ @Override public void secondAfterBody(ICoreService coreService, long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { // NOCHK:8-params double endTime = timeStack.pop().doubleValue(); double startTime = timeStack.pop().doubleValue(); if (threadLast.get().booleanValue()) { threadLast.set(Boolean.FALSE); double duration = endTime - startTime; String sql = parameters[0].toString(); Timestamp timestamp = new Timestamp(System.currentTimeMillis() - Math.round(duration)); long platformId = platformManager.getPlatformId(); SqlStatementData sqlData = new SqlStatementData(timestamp, platformId, sensorTypeId, methodId); sqlData.setPreparedStatement(false); sqlData.setSql(strConstraint.crop(sql)); sqlData.setDuration(duration); sqlData.calculateMin(duration); sqlData.calculateMax(duration); sqlData.setCount(1L); // populate the connection meta data. connectionMetaDataStorage.populate(sqlData, statementReflectionCache.getConnection(object.getClass(), object)); coreService.addDefaultData(sqlData); } }
/** * {@inheritDoc} */ @Override public void firstAfterBody(long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); }
/** * {@inheritDoc} */ @Override public void beforeBody(long methodId, long sensorTypeId, Object object, Object[] parameters, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); threadLast.set(Boolean.TRUE); }
double endTime = timeStack.pop().doubleValue(); double startTime = timeStack.pop().doubleValue(); double duration = endTime - startTime; long cpuEndTime = threadCpuTimeStack.pop().longValue(); long cpuStartTime = threadCpuTimeStack.pop().longValue(); cpuDuration = (cpuEndTime - cpuStartTime) / 1000000.0d;
/** * {@inheritDoc} */ @Override public void beforeBody(long methodId, long sensorTypeId, Object object, Object[] parameters, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); threadLast.set(Boolean.TRUE); }
/** * {@inheritDoc} */ @Override public void firstAfterBody(long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { timeStack.push(new Double(timer.getCurrentTime())); }
double endTime = timeStack.pop().doubleValue(); double startTime = timeStack.pop().doubleValue(); double duration = endTime - startTime; long cpuEndTime = threadCpuTimeStack.pop().longValue(); long cpuStartTime = threadCpuTimeStack.pop().longValue(); cpuDuration = (cpuEndTime - cpuStartTime) / 1000000.0d;