io.thundra.swark.jdbc.spring.monitoring.MonitoredJdbcTemplate Maven / Gradle / Ivy
package io.thundra.swark.jdbc.spring.monitoring;
import io.thundra.swark.monitoring.ExecutionContext;
import io.thundra.swark.utils.ExceptionUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.jdbc.support.KeyHolder;
import javax.sql.DataSource;
/**
* @author serkan
*/
public class MonitoredJdbcTemplate extends JdbcTemplate {
public MonitoredJdbcTemplate(DataSource dataSource) {
super(dataSource);
}
private DatabaseExecutionInfoGroup getDatabaseExecutionInfoGroup() {
DatabaseExecutionInfoGroup databaseExecutionInfoGroup = null;
ExecutionContext executionContext = ExecutionContext.getActive();
if (executionContext != null) {
databaseExecutionInfoGroup = executionContext.getExecutionInfoGroup(DatabaseExecutionInfoGroup.GROUP_NAME);
}
if (databaseExecutionInfoGroup != null) {
return databaseExecutionInfoGroup;
} else {
return new DatabaseExecutionInfoGroup();
}
}
private enum OperationType {
QUERY,
UPDATE,
}
private OperationType getOperationType(StatementCallback action) {
String className = action.getClass().getSimpleName();
if (className.startsWith("Query") || className.startsWith("Execute")) {
return OperationType.QUERY;
} else if (className.startsWith("Update") || className.startsWith("BatchUpdate")) {
return OperationType.UPDATE;
} else {
return null;
}
}
@Override
public T execute(StatementCallback action) throws DataAccessException {
DatabaseExecutionInfoGroup databaseExecutionInfoGroup = getDatabaseExecutionInfoGroup();
long startTime = System.nanoTime();
OperationType operationType = getOperationType(action);
try {
if (operationType == OperationType.QUERY) {
databaseExecutionInfoGroup.increaseQueryCount();
} else if (operationType == OperationType.UPDATE) {
databaseExecutionInfoGroup.increaseUpdateCount();
}
return super.execute(action);
} catch (Throwable t) {
databaseExecutionInfoGroup.increaseErrorCount();
ExceptionUtils.sneakyThrow(t);
return null;
} finally {
if (operationType == OperationType.QUERY) {
databaseExecutionInfoGroup.increaseQueryDuration(System.nanoTime() - startTime);
} else if (operationType == OperationType.UPDATE) {
databaseExecutionInfoGroup.increaseUpdateDuration(System.nanoTime() - startTime);
}
}
}
@Override
public T query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor rse)
throws DataAccessException {
DatabaseExecutionInfoGroup databaseExecutionInfoGroup = getDatabaseExecutionInfoGroup();
long startTime = System.nanoTime();
try {
databaseExecutionInfoGroup.increaseQueryCount();
return super.query(psc, pss, rse);
} catch (Throwable t) {
databaseExecutionInfoGroup.increaseErrorCount();
ExceptionUtils.sneakyThrow(t);
return null;
} finally {
databaseExecutionInfoGroup.increaseQueryDuration(System.nanoTime() - startTime);
}
}
@Override
protected int update(PreparedStatementCreator psc, PreparedStatementSetter pss)
throws DataAccessException {
DatabaseExecutionInfoGroup databaseExecutionInfoGroup = getDatabaseExecutionInfoGroup();
long startTime = System.nanoTime();
try {
databaseExecutionInfoGroup.increaseUpdateCount();
return super.update(psc, pss);
} catch (Throwable t) {
databaseExecutionInfoGroup.increaseErrorCount();
ExceptionUtils.sneakyThrow(t);
return -1;
} finally {
databaseExecutionInfoGroup.increaseUpdateDuration(System.nanoTime() - startTime);
}
}
@Override
public int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)
throws DataAccessException {
DatabaseExecutionInfoGroup databaseExecutionInfoGroup = getDatabaseExecutionInfoGroup();
long startTime = System.nanoTime();
try {
databaseExecutionInfoGroup.increaseUpdateCount();
return super.update(psc, generatedKeyHolder);
} catch (Throwable t) {
databaseExecutionInfoGroup.increaseErrorCount();
ExceptionUtils.sneakyThrow(t);
return -1;
} finally {
databaseExecutionInfoGroup.increaseUpdateDuration(System.nanoTime() - startTime);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy