Question

I am using Glassfish 4 and Hibernate 4.3.

How can I log every query from Hibernate?

My first try involved SLF4J 1.7.7 and Logback 1.1.2. I have tried some tutorials using SLF4J and Logback, but with no results. One tutorial said to change some files manually and add some options to glassfish, but I dislike this way.

In my second try I have used P6SPY 2.0.2. To make this library work I need to change my connection to use P6SPY drivers and configure spy.properties to use the real driver. Since I have configured my connection pool on Glassfish, I tried the P6SPY installation tutorial to configure it there, but I haven't managed to make my new DataSource to work. In P6SPY installation tutorial, there's a way to configure using java.sqlDriver and javax.sql.ConnectionPoolDataSource, but I am using javax.sql.DataSource.

So, is this the only way to use SLF4J with Glassfish? Do you know a tutorial or how to configure it?

If SLF4J won't work, how can I configure P6SPY to log my queries using the Glassfish connection pool?

Are there a better option?

Thanks in advance.

Was it helpful?

Solution

To use P6Spy with a DataSource on Glassfish, you will need to setup a second DataSource. The way that you do this is very similar to the instructions for setting up the ConnectionPoolDataSource. The only difference is that you set res-type to javax.sql.DataSource.

Example:

<jdbc-connection-pool datasource-classname="com.p6spy.engine.spy.P6DataSource" res-type="javax.sql.DataSource" name="p6spyPool">
  <property name="realDataSource" value="jdbc/myrealds"></property>
  <property name="Password" value=""></property>
  <property name="User" value="sa"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="p6spyPool" jndi-name="jdbc/p6spy"></jdbc-resource>   

Note: You will need to replace jdbc/myrealds with the jndi name of your real data source.

Also - When using the data source proxies, you do not need to configure a real driver in spy.properties.

If you still have problems with the configuration, please post a message to p6spy-users@googlegroups.com.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top