2010-06-06 134 views
3

我試圖找到一個設置,通過該設置,連接池監控信息將出現在server.log中,只要出現「錯誤分配連接」或「連接關閉」等錯誤時。JDBC連接池監控GlassFish

我發現了一些blog entries,談論這個,但他們從GUI提到它。但是,我想要在連接池本身上進行設置,以便在日誌中顯示連接池監視信息。

有沒有人知道這樣的設置?

在Sun的應用服務器8.X它使用的是perf-monitor

回答

2

我不知道這是否可以幫助你....但你可以詢問連接池通過JMX監測信息。 此代碼將打印應用服務器中所有連接池的最大池大小和已用連接的數量(有多少負載可從MBean中獲取):

MBeanServerConnection conn = getMbeanServerConnection(); 

    //search the jmx register for the specified beans 
    Set<ObjectInstance> connectorPoolSet = conn.queryMBeans(new ObjectName("*:type=jdbc-connection-pool,*"), null); 
    Map<String , ObjectName> configMap = new HashMap<String, ObjectName>(); 
    Map<String , ObjectName> monitorMap = new HashMap<String, ObjectName>(); 

    //get a map of each config & monitor object found for the search 
    for(ObjectInstance oi : connectorPoolSet) { 
     String name = oi.getObjectName().getKeyProperty("name"); 

     //if the category of the mbean is config - put it in the config map - else if it is monitor 
     //place it in the monitor map. 
     String category = oi.getObjectName().getKeyProperty("category"); 
     if("config".equalsIgnoreCase(category)) { 
      configMap.put(name, oi.getObjectName()); 
     } else if("monitor".equalsIgnoreCase(category)){ 
      monitorMap.put(name, oi.getObjectName()); 
     } 
    } 

    //iterate the pairs of config & monitor mbeans found 
    for(String name : configMap.keySet()) { 

     ObjectName configObjectName = configMap.get(name); 
     ObjectName monitorObjectName = monitorMap.get(name); 
     if(monitorObjectName == null) { 
      //no monitor found - throw an exception or something 
     } 

     int maxPoolSizeVal = getAttributeValue(conn, configObjectName, "max-pool-size"); 
     int connectionsInUse = getAttributeValue(conn, monitorObjectName, "numconnused-current"); 

     System.out.println(name + " -> max-pool-size : " + maxPoolSizeVal); 
     System.out.println(name + " -> connections in use : " + connectionsInUse); 


    }