1 package org.hibernate.jmx; 3 4 import javax.naming.InitialContext ; 5 import javax.naming.NameNotFoundException ; 6 import javax.naming.NamingException ; 7 import javax.naming.Reference ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.SessionFactory; 12 import org.hibernate.impl.SessionFactoryObjectFactory; 13 import org.hibernate.stat.CollectionStatistics; 14 import org.hibernate.stat.EntityStatistics; 15 import org.hibernate.stat.QueryStatistics; 16 import org.hibernate.stat.SecondLevelCacheStatistics; 17 import org.hibernate.stat.Statistics; 18 import org.hibernate.stat.StatisticsImpl; 19 20 52 public class StatisticsService implements StatisticsServiceMBean { 53 54 56 SessionFactory sf; 57 String sfJNDIName; 58 Log log = LogFactory.getLog(StatisticsService.class); 59 Statistics stats = new StatisticsImpl(); 60 61 64 public void setSessionFactoryJNDIName(String sfJNDIName) { 65 this.sfJNDIName = sfJNDIName; 66 try { 67 Object obj = new InitialContext ().lookup(sfJNDIName); 68 if (obj instanceof Reference ) { 69 Reference ref = (Reference ) obj; 70 setSessionFactory( (SessionFactory) SessionFactoryObjectFactory.getInstance( (String ) ref.get(0).getContent() ) ); 71 } 72 else { 73 setSessionFactory( (SessionFactory) obj ); 74 } 75 } 76 catch (NameNotFoundException e) { 77 log.error("No session factory with JNDI name " + sfJNDIName, e); 78 setSessionFactory(null); 79 } 80 catch (NamingException e) { 81 log.error("Error while accessing session factory with JNDI name " + sfJNDIName, e); 82 setSessionFactory(null); 83 } 84 catch (ClassCastException e) { 85 log.error("JNDI name " + sfJNDIName + " does not handle a session factory reference", e); 86 setSessionFactory(null); 87 } 88 } 89 90 95 public void setSessionFactory(SessionFactory sf) { 96 this.sf = sf; 97 if (sf == null) { 98 stats = new StatisticsImpl(); 99 } 100 else { 101 stats = sf.getStatistics(); 102 } 103 104 } 105 108 public void clear() { 109 stats.clear(); 110 } 111 114 public EntityStatistics getEntityStatistics(String entityName) { 115 return stats.getEntityStatistics(entityName); 116 } 117 120 public CollectionStatistics getCollectionStatistics(String role) { 121 return stats.getCollectionStatistics(role); 122 } 123 126 public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) { 127 return stats.getSecondLevelCacheStatistics(regionName); 128 } 129 132 public QueryStatistics getQueryStatistics(String hql) { 133 return stats.getQueryStatistics(hql); 134 } 135 138 public long getEntityDeleteCount() { 139 return stats.getEntityDeleteCount(); 140 } 141 144 public long getEntityInsertCount() { 145 return stats.getEntityInsertCount(); 146 } 147 150 public long getEntityLoadCount() { 151 return stats.getEntityLoadCount(); 152 } 153 156 public long getEntityFetchCount() { 157 return stats.getEntityFetchCount(); 158 } 159 162 public long getEntityUpdateCount() { 163 return stats.getEntityUpdateCount(); 164 } 165 168 public long getQueryExecutionCount() { 169 return stats.getQueryExecutionCount(); 170 } 171 public long getQueryCacheHitCount() { 172 return stats.getQueryCacheHitCount(); 173 } 174 public long getQueryExecutionMaxTime() { 175 return stats.getQueryExecutionMaxTime(); 176 } 177 public long getQueryCacheMissCount() { 178 return stats.getQueryCacheMissCount(); 179 } 180 public long getQueryCachePutCount() { 181 return stats.getQueryCachePutCount(); 182 } 183 186 public long getFlushCount() { 187 return stats.getFlushCount(); 188 } 189 192 public long getConnectCount() { 193 return stats.getConnectCount(); 194 } 195 198 public long getSecondLevelCacheHitCount() { 199 return stats.getSecondLevelCacheHitCount(); 200 } 201 204 public long getSecondLevelCacheMissCount() { 205 return stats.getSecondLevelCacheMissCount(); 206 } 207 210 public long getSecondLevelCachePutCount() { 211 return stats.getSecondLevelCachePutCount(); 212 } 213 216 public long getSessionCloseCount() { 217 return stats.getSessionCloseCount(); 218 } 219 222 public long getSessionOpenCount() { 223 return stats.getSessionOpenCount(); 224 } 225 228 public long getCollectionLoadCount() { 229 return stats.getCollectionLoadCount(); 230 } 231 234 public long getCollectionFetchCount() { 235 return stats.getCollectionFetchCount(); 236 } 237 240 public long getCollectionUpdateCount() { 241 return stats.getCollectionUpdateCount(); 242 } 243 246 public long getCollectionRemoveCount() { 247 return stats.getCollectionRemoveCount(); 248 } 249 252 public long getCollectionRecreateCount() { 253 return stats.getCollectionRecreateCount(); 254 } 255 258 public long getStartTime() { 259 return stats.getStartTime(); 260 } 261 262 265 public boolean isStatisticsEnabled() { 266 return stats.isStatisticsEnabled(); 267 } 268 269 272 public void setStatisticsEnabled(boolean enable) { 273 stats.setStatisticsEnabled(enable); 274 } 275 276 public void logSummary() { 277 stats.logSummary(); 278 } 279 280 public String [] getCollectionRoleNames() { 281 return stats.getCollectionRoleNames(); 282 } 283 284 public String [] getEntityNames() { 285 return stats.getEntityNames(); 286 } 287 288 public String [] getQueries() { 289 return stats.getQueries(); 290 } 291 292 public String [] getSecondLevelCacheRegionNames() { 293 return stats.getSecondLevelCacheRegionNames(); 294 } 295 296 public long getSuccessfulTransactionCount() { 297 return stats.getSuccessfulTransactionCount(); 298 } 299 public long getTransactionCount() { 300 return stats.getTransactionCount(); 301 } 302 303 public long getCloseStatementCount() { 304 return stats.getCloseStatementCount(); 305 } 306 public long getPrepareStatementCount() { 307 return stats.getPrepareStatementCount(); 308 } 309 } 310 | Popular Tags |