matiasf
matiasf

Reputation: 1118

Get Hibernate transaction status

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:

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?

Upvotes: 0

Views: 1668

Answers (1)

Péter Török
Péter Török

Reputation: 116266

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.

Upvotes: 2

Related Questions