JPAによるpg_stat_activity統計情報の取得方法です。
PgStatRepository.java
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
@Repository
public class PgStatRepository {
@PersistenceContext
EntityManager entityManager;
/**
* 接続数の取得
* @return 接続数
*/
public int countClient() {
String query = "SELECT count(application_name) FROM pg_stat_activity WHERE datname = 'spring-sample';";
return ((Number)entityManager.createNativeQuery(query).getSingleResult()).intValue();
}
}