LoginSignup
0
1

More than 3 years have passed since last update.

SpringBoot JPAによるPostgresqlの活動統計情報の取得

Posted at

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();
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1