4

我使用 Hibernate 4、Spring 3、JSF 2.0 和 Weblogic 10.3.6 作为服务器。

我在 Weblogic 服务器上创建了数据源,在 applicationContext.xml 中我将数据源定义为

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

如果我想使用 P6Spy 来记录 SQL 参数,我应该如何以及在何处在 applicationcontext.xml 中添加以下内容?

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

任何帮助都是非常可观的。

谢谢

4

2 回答 2

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>
于 2013-11-15T06:12:06.850 回答
1

您需要在 applicationContext.xml 文件中创建会话工厂的 bean,如下所示:

<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>

有关 P6Spy 库的更多信息,请参阅:http ://www.mkyong.com/hibernate/how-to-display-hibernate-sql-parameter-values-solution/。

我们可以省略“dataSource”bean,直接写属性。参考:如何为 sql server 配置休眠配置文件

于 2013-07-11T09:22:13.893 回答