Skip to content

Instantly share code, notes, and snippets.

@schophil
Created April 18, 2023 10:58
Show Gist options
  • Save schophil/f6e5284e3ec63509ce6888ece0a74f67 to your computer and use it in GitHub Desktop.
Save schophil/f6e5284e3ec63509ce6888ece0a74f67 to your computer and use it in GitHub Desktop.
Jpa statistics logger
import lombok.CustomLog;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Component
@CustomLog(topic = "JPA")
public class JpaStatisticsLogger {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void log() {
Session session = entityManager.unwrap(Session.class);
log.info("Entity count: {}",session.getStatistics().getEntityCount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment