views:

114

answers:

1

I'm using Hibernate 3.3 in a Swing application. I'd like to retrieve some stats on the performed transaction and make that info available to my views. For example:

  • Execution time
  • Number of rows touched
  • Type of transaction (update, delete, create, select etc)

I have several helper classes. I'm thinking of having some public fields like executionTime, noRows, transactionStatus etc and populate them in the finalize() method.

Does Hibernate provide a method to access this kind of data?

+1  A: 

Yes, Hibernate provides live statistics. Usage example from the Hibernate book:

Statistics stats = HibernateUtil.getSessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
...

stats.getSessionOpenCount();
stats.logSummary();

EntityStatistics itemStats = stats.getEntityStatistics("auction.model.Item");
itemStats.getFetchCount();

The statistics interfaces are Statistics for global information, EntityStatistics for information about a particular entity, CollectionStatistics for a particular collection role, QueryStatistics for SQL and HQL queries, and SecondLevelCacheStatistics for detailed runtime information about a particular region in the optional second-level data cache. A convenient method is logSummary(), which prints out a complete summary to the console with a single call. If you want to enable the collection of statistics through the configuration, and not programmatically, set the hibernate.generate_statistics configuration property to true. See the API documentation for more information about the various statistics retrieval methods.

If you are running in an app server, you can also configure JMX to provide you statistics.

Péter Török
Thanks, but I can't seem to find a method for getting the total transaction execution time.
matiasf