4

I am using Hibernate 4, Spring 3, JSF 2.0 and Weblogic 10.3.6 as server.

I have created datasource on Weblogic server and in applicationContext.xml I have defined datasource as

<!-- Data Source Declaration -->    
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/​myDS"/>   
</bean>

If I would want to use the P6Spy for logging SQL parameters, how can and where I should add the following in applicationcontext.xml?

  <property name="hibernate.connection.driver_class">com.p6spy.engine.spy.
  P6SpyDriver</property>

Any help is highly appreciable.

Thanks

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • would official docs help? http://p6spy.github.io/p6spy/install.html The Websphere version might be outdated, but information might still be relevant. – Peter Butkovic Sep 09 '13 at 07:49
  • @PeterButkovic Whatever mentioned for Websphere should be configured in Websphere application server isn't it? – Jacob Sep 09 '13 at 08:11
  • I guess, I missed your point. What do you mean? From my point of view all you need to do is create new datasource (p6spy one) that would later proxy the real one (jdbc/​myDS). And the applicationcontext.xml would just refer to p6spy one. That should be it I guess. – Peter Butkovic Sep 09 '13 at 08:16

2 Answers2

6

The easiest way to integrate p6spy using spring is to use the P6DataSource class. The P6DataSource class is just a proxy for the real data source. This lets you obtain the real data source using any of the spring data source factory implementations.

<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
  <constructor-arg>
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="jdbc/​myDS"/>   
    </bean>
  </constructor-arg>
</bean>

If you are using an XADatasource, just change the classname to P6ConnectionPoolDataSource as shown below. Note: P6ConnectionPoolDataSource implements the ConnectionPoolDataSource and XADataSource interfaces.

<bean id="dataSource" class="com.p6spy.engine.spy.P6ConnectionPoolDataSource">
  <constructor-arg>
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="jdbc/​myDS"/>   
    </bean>
  </constructor-arg>
</bean>
quintonm
  • 838
  • 1
  • 7
  • 20
  • 1
    Correct, you would need to use P6ConnectionPoolDataSource for XA support. I have updated my answer to include this information as well. – quintonm Mar 28 '14 at 17:18
1

You need to create bean of session factory in applicationContext.xml file as follows:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.p6spy.engine.spy.
  P6SpyDriver" />
        <property name="url" value="jdbc\:mysql\://localhost\:3306/testdb" />
        <property name="username" value="my_username" />
        <property name="password" value="my_password" />
    </bean>

Please refer to: http://www.mkyong.com/hibernate/how-to-display-hibernate-sql-parameter-values-solution/ for more about P6Spy library.

We can omit "dataSource" bean and directly write properties. Ref: how to configure hibernate config file for sql server

Community
  • 1
  • 1
RaviSam
  • 130
  • 1
  • 8
  • 1
    If `jndi` data source is being used for database connection, then why there is a need to hardcode username and password and other database connection details in `applicationContext.xml`? – Jacob Jul 11 '13 at 10:15