Skip to content

Instantly share code, notes, and snippets.

@ishults
Last active December 16, 2015 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishults/10560085 to your computer and use it in GitHub Desktop.
Save ishults/10560085 to your computer and use it in GitHub Desktop.
private static final String START_TIME = 'Hibernate_Start_Time'
def sessionFactory
// ...
logHibernateStats(controller: '*', action: '*') {
before = {
String enabledString = grailsApplication.config.logHibernateStats
Statistics stats = sessionFactory.statistics
if (enabledString == 'ALWAYS' || (enabledString == 'ALLOWED' && params.logHibernateStats)) {
log.info "\n### In action: $controllerName/$actionName ###"
if (!stats.statisticsEnabled) {
stats.statisticsEnabled = true
}
request[START_TIME] = System.currentTimeMillis()
} else if (enabledString == 'ALLOWED' && stats.statisticsEnabled) {
stats.statisticsEnabled = false // We assume no one else is using stats
}
}
afterView = {
String enabledString = grailsApplication.config.logHibernateStats
if (enabledString == 'ALWAYS' || (enabledString == 'ALLOWED' && params.logHibernateStats)) {
Long end = System.currentTimeMillis()
Long start = request[START_TIME]
Statistics stats = sessionFactory.statistics
log.info """
############## Hibernate Stats ##############
Action: /${controllerName}/${actionName}
Transaction Count: ${stats.transactionCount}
Flush Count: ${stats.flushCount}
Prepared Statement Count: ${stats.prepareStatementCount}
Total time: ${end - start} ms
#############################################
"""
stats.clear() // We assume no one else is using stats
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment