BasicDataSource 的 Spring 拦截器

发布于 2025-01-07 19:34:34 字数 3411 浏览 5 评论 0原文

我使用以下配置连接到我的数据库(一个 mysql,一个 oracle)。我想知道是否有任何方法可以配置 spring 拦截器以允许我审核对数据库的访问和请求的性能?感谢您的意见。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>   
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:database.properties</value> 
            <value>${config}</value>
        </list>
    </property>
</bean>

<!-- EntityManagerFactory -->
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="userPersistenceUnitManager"/> 
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
    <property name="jpaProperties">
        <props> 
            <prop key="eclipselink.logging.level">FINE</prop>
            <prop key="eclipselink.logging.timestamp">true</prop>
            <prop key="eclipselink.logging.session">true</prop>
            <prop key="eclipselink.logging.thread">true</prop>
            <prop key="eclipselink.logging.exceptions">true</prop>
            <prop key="eclipselink.weaving">false</prop>
        </props>
    </property>
</bean>

<!-- See http://commons.apache.org/dbcp/configuration.html  
-->
<bean id="userDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    <property name="url" value="${database.url}"/> 

    <property name="username" value="${database.username}"/> 
    <property name="password" value="${database.password}"/> 

    <!-- performance tuning -->
    <property name="initialSize" value="{database.minConnections}" />
    <property name="maxActive" value="{database.maxConnections}" /> 
    <property name="maxIdle" value="10"/>
    <property name="minIdle" value="1"/>

    <!-- The main purpose for the validation here is to avoid reusing a
         expired DB connection -->
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testWhileIdle" value="false"/>
    <property name="testOnReturn" value="false"/>
</bean>

<bean id="userPersistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath:META-INF/user_persistence.xml</value>
        </list>
    </property>
    <property name="defaultDataSource" ref="userDataSource"/>
</bean>

I use the following configuration to connect to my databases (one mysql, one oracle). I am wondering if there is any way to configure a spring interceptor to allow me to audit access to the database and performance of the requests ? Thanks for your input.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>   
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:database.properties</value> 
            <value>${config}</value>
        </list>
    </property>
</bean>

<!-- EntityManagerFactory -->
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="userPersistenceUnitManager"/> 
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
    <property name="jpaProperties">
        <props> 
            <prop key="eclipselink.logging.level">FINE</prop>
            <prop key="eclipselink.logging.timestamp">true</prop>
            <prop key="eclipselink.logging.session">true</prop>
            <prop key="eclipselink.logging.thread">true</prop>
            <prop key="eclipselink.logging.exceptions">true</prop>
            <prop key="eclipselink.weaving">false</prop>
        </props>
    </property>
</bean>

<!-- See http://commons.apache.org/dbcp/configuration.html  
-->
<bean id="userDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    <property name="url" value="${database.url}"/> 

    <property name="username" value="${database.username}"/> 
    <property name="password" value="${database.password}"/> 

    <!-- performance tuning -->
    <property name="initialSize" value="{database.minConnections}" />
    <property name="maxActive" value="{database.maxConnections}" /> 
    <property name="maxIdle" value="10"/>
    <property name="minIdle" value="1"/>

    <!-- The main purpose for the validation here is to avoid reusing a
         expired DB connection -->
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testWhileIdle" value="false"/>
    <property name="testOnReturn" value="false"/>
</bean>

<bean id="userPersistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath:META-INF/user_persistence.xml</value>
        </list>
    </property>
    <property name="defaultDataSource" ref="userDataSource"/>
</bean>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2025-01-14 19:34:34

这在技术上与 Spring 无关,有几个 DataSource 装饰器可以完成这项工作:

  • http://code.google.com/p/jdbcdslog

    <块引用>

    该项目的目标是为 JDBC 应用程序创建一个高性能且易于使用的 SQL 查询日志记录工具。

  • http://code.google.com/p/log4jdbc

    <块引用>

    log4jdbc 是一个 Java JDBC 驱动程序,可以记录其他 JDBC 驱动程序的 SQL 和/或 JDBC 调用(以及可选的 SQL 计时信息)

This is not technically related to Spring, there are several DataSource decorators that do the job:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文