Friday, October 28, 2011

Debugging Hibernate: what database am I connecting to?

If you're looking for interesting reading, skip this post.

If you're trying to find out what database, username, and password Hibernate is using to execute the query, continue. This post is a reference for me, and it might be useful to someone else someday.

Set a debug point anywhere you can get to the Hibernate session. For instance, anywhere a Query is in scope.

The Query has a session, which has a jdbcContext, which has a connectionManager, which has a factory, which has a connectionProvider. Looking at the objects in the debugger, the factory has a settings object which holds the connectionProvider. Or, get to the connectionProvider from the session by executing this (Alt-F8 for execute in Idea):
((QueryImpl) query).getSession().getJDBCContext().getConnectionManager().getFactory().getConnectionProvider()

You may put any expression that returns the session in place of "((QueryImpl) query).getSession()".

This connection provider has a datasource (ds). This is the object you need to explore. Implementations will vary. In my case, the PoolBackedDataSource has a WrapperConnectionPoolDataSource which has a nestedDataSource of type DriverManagerDataSource. Inside here is the jdbcUrl, which shows where it is connection, and a properties member which includes "user" and "password" properties. There's the information I was looking for, with the password in plain text.

Maybe there's an easier way to do this, but this worked.

No comments:

Post a Comment