[Schmitzm-commits] r1726 - trunk/schmitzm-core/src/main/java/de/schmitzm/postgres

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Sep 12 20:20:55 CEST 2011


Author: mojays
Date: 2011-09-12 20:20:55 +0200 (Mon, 12 Sep 2011)
New Revision: 1726

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java
Log:
PGUtil: Method to determine the current count of open sessions

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java	2011-09-11 11:18:23 UTC (rev 1725)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java	2011-09-12 18:20:55 UTC (rev 1726)
@@ -1,6 +1,7 @@
 package de.schmitzm.postgres;
 
 import java.sql.Connection;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
@@ -462,4 +463,37 @@
 		return key;
 	}
 
+	/**
+	 * Liefert die Anzahl der offenen Sessions zur Datenbank.
+	 * @param c DB-Verbindung
+	 * @param dbName Name der Datenbank fuer die die Sessions gezaehlt
+	 *               werden (kann {@code null} sein)
+     * @param userName Name des Users fuer den die Sessions gezaehlt
+     *                 werden (kann {@code null} sein)
+	 */
+	public static int getOpenSessionCount(Connection c, String dbName, String userName) throws SQLException {
+	  String stmtSql = "SELECT COUNT(*) FROM pg_stat_activity WHERE TRUE";
+	  
+	  if ( dbName != null )
+	    stmtSql += " AND datname = ?";
+      if ( userName != null )
+        stmtSql += " AND usename = ?";
+      
+	  PreparedStatement stmt = c.prepareStatement(stmtSql);
+      if ( dbName != null )
+        stmt.setString(1, dbName);
+      if ( userName != null )
+        stmt.setString(2, userName);
+	  stmt.execute();
+      
+      ResultSet result = stmt.getResultSet();
+      int count = 0;
+      if ( result.next() )
+        count = result.getInt(1);
+      
+      result.close();
+      stmt.close();
+      return count;
+	}
+	
 }



More information about the Schmitzm-commits mailing list