springmvc+spring4+mybatis3,service调用mapper查询数据库有点慢

大付 发布于 2015/12/22 12:19
阅读 1K+
收藏 0
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"        
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
<!-- 引入属性文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
<property name="locations">  
<list>  
                 <value>classpath:dbconfig.properties</value>  
            </list>  
        </property>  
</bean>
<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
<context:component-scan base-package="com.fzy.**.service">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 阿里 druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
         <!-- 数据库基本信息配置 -->
         <property name="url" value="${jdbc.url}" />  
         <property name="username" value="${jdbc.username}" />  
         <property name="password" value="${jdbc.password}" />  
         <property name="driverClassName" value="${jdbc.driver}" />  
         <property name="filters" value="${filters}" />  
    <!-- 最大并发连接数 -->
         <property name="maxActive" value="${maxActive}" />
         <!-- 初始化连接数量 -->
         <property name="initialSize" value="${initialSize}" />
         <!-- 配置获取连接等待超时的时间 -->
         <property name="maxWait" value="${maxWait}" />
         <!-- 最小空闲连接数 -->
         <property name="minIdle" value="${minIdle}" />  
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
         <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />  
         <property name="validationQuery" value="${validationQuery}" />  
         <property name="testWhileIdle" value="${testWhileIdle}" />  
         <property name="testOnBorrow" value="${testOnBorrow}" />  
         <property name="testOnReturn" value="${testOnReturn}" />  
         <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
         <!-- 打开removeAbandoned功能 -->
         <property name="removeAbandoned" value="${removeAbandoned}" />
         <!-- 1800秒,也就是30分钟 -->
         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
         <!-- 关闭abanded连接时输出错误日志 -->   
         <property name="logAbandoned" value="${logAbandoned}" />
</bean>

<!-- 配置mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property>
        <property name="typeAliasesPackage" value="com.fzy.system.entity,com.fzy.project.entity,com.fzy.cms.entity" />
    </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!--指定扫描的包名-->
       <property name="basePackage" value="com.fzy.*.mapper"/>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>    
<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception"/>
<tx:method name="insert" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
<tx:method name="update" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
<tx:method name="save" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
        <aop:pointcut id="pc" expression="execution(* com.fzy..*.service.impl..*ServiceImpl.*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
    </aop:config>
<!-- 配置druid监控spring jdbc -->
    <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>com.fzy.*.service</value>
            </list>
        </property>
    </bean>
     
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    </aop:config>

</beans>

第一次执行查询 3917ms

2015-12-22 12:14:18 -29815 [http-bio-8080-exec-3] DEBUG   - DispatcherServlet with name 'spring' processing GET request for [/projectController.do]
2015-12-22 12:14:18 -29834 [http-bio-8080-exec-3] DEBUG   - Looking up handler method for path /projectController.do
2015-12-22 12:14:18 -29923 [http-bio-8080-exec-3] DEBUG   - Returning handler method [public java.lang.String com.fzy.project.controller.ProjectController.list(javax.servlet.http.HttpServletRequest) throws java.lang.Exception]
2015-12-22 12:14:18 -29926 [http-bio-8080-exec-3] DEBUG   - Returning cached instance of singleton bean 'projectController'
2015-12-22 12:14:18 -29932 [http-bio-8080-exec-3] DEBUG   - Last-Modified value for [/projectController.do] is: -1
2015-12-22 12:14:19 -30246 [http-bio-8080-exec-3] DEBUG   - Creating new transaction with name [com.fzy.project.service.impl.ProjectServiceImpl.findProjectList]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2015-12-22 12:14:19 -30297 [http-bio-8080-exec-3] DEBUG   - Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] for JDBC transaction
2015-12-22 12:14:19 -30380 [http-bio-8080-exec-3] DEBUG   - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] to manual commit
2015-12-22 12:14:19 -30891 [http-bio-8080-exec-3] DEBUG   - Creating a new SqlSession
2015-12-22 12:14:19 -30943 [http-bio-8080-exec-3] DEBUG   - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@704319f8]
2015-12-22 12:14:19 -31033 [http-bio-8080-exec-3] DEBUG   - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] will be managed by Spring
2015-12-22 12:14:20 -31328 [http-bio-8080-exec-3] DEBUG   - ==>  Preparing: select * from PROJECT order by CREATEDATE DESC 
2015-12-22 12:14:21 -32749 [http-bio-8080-exec-3] DEBUG   - ==> Parameters: 
2015-12-22 12:14:22 -33969 [http-bio-8080-exec-3] DEBUG   - <==      Total: 3
2015-12-22 12:14:22 -33975 [http-bio-8080-exec-3] DEBUG   - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@704319f8]
2015-12-22 12:14:22 -33979 [http-bio-8080-exec-3] DEBUG   - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@704319f8]
2015-12-22 12:14:22 -33983 [http-bio-8080-exec-3] DEBUG   - {conn-10001, pstmt-20000} enter cache
2015-12-22 12:14:22 -33987 [http-bio-8080-exec-3] DEBUG   - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@704319f8]
2015-12-22 12:14:22 -33991 [http-bio-8080-exec-3] DEBUG   - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@704319f8]
2015-12-22 12:14:22 -33994 [http-bio-8080-exec-3] DEBUG   - Initiating transaction commit
2015-12-22 12:14:22 -33997 [http-bio-8080-exec-3] DEBUG   - Committing JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0]
2015-12-22 12:14:22 -34004 [http-bio-8080-exec-3] DEBUG   - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] after transaction
2015-12-22 12:14:22 -34007 [http-bio-8080-exec-3] DEBUG   - Returning JDBC Connection to DataSource
dddddddd3917
2015-12-22 12:14:22 -34058 [http-bio-8080-exec-3] DEBUG   - Invoking afterPropertiesSet() on bean with name 'project/test'
2015-12-22 12:14:22 -34062 [http-bio-8080-exec-3] DEBUG   - Rendering view [org.springframework.web.servlet.view.JstlView: name 'project/test'; URL [/webpage/project/test.jsp]] in DispatcherServlet with name 'spring'
2015-12-22 12:14:23 -34117 [http-bio-8080-exec-3] DEBUG   - Forwarding to resource [/webpage/project/test.jsp] in InternalResourceView 'project/test'
2015-12-22 12:14:23 -34381 [http-bio-8080-exec-3] DEBUG   - Successfully completed request
2015-12-22 12:14:23 -34389 [http-bio-8080-exec-3] DEBUG   - Returning cached instance of singleton bean 'sqlSessionFactory'


第二次是116ms

2015-12-22 12:14:22 -34058 [http-bio-8080-exec-3] DEBUG   - Invoking afterPropertiesSet() on bean with name 'project/test'
2015-12-22 12:14:22 -34062 [http-bio-8080-exec-3] DEBUG   - Rendering view [org.springframework.web.servlet.view.JstlView: name 'project/test'; URL [/webpage/project/test.jsp]] in DispatcherServlet with name 'spring'
2015-12-22 12:14:23 -34117 [http-bio-8080-exec-3] DEBUG   - Forwarding to resource [/webpage/project/test.jsp] in InternalResourceView 'project/test'
2015-12-22 12:14:23 -34381 [http-bio-8080-exec-3] DEBUG   - Successfully completed request
2015-12-22 12:14:23 -34389 [http-bio-8080-exec-3] DEBUG   - Returning cached instance of singleton bean 'sqlSessionFactory'
2015-12-22 12:15:02 -73186 [http-bio-8080-exec-5] DEBUG   - DispatcherServlet with name 'spring' processing GET request for [/projectController.do]
2015-12-22 12:15:02 -73191 [http-bio-8080-exec-5] DEBUG   - Looking up handler method for path /projectController.do
2015-12-22 12:15:02 -73221 [http-bio-8080-exec-5] DEBUG   - Returning handler method [public java.lang.String com.fzy.project.controller.ProjectController.list(javax.servlet.http.HttpServletRequest) throws java.lang.Exception]
2015-12-22 12:15:02 -73224 [http-bio-8080-exec-5] DEBUG   - Returning cached instance of singleton bean 'projectController'
2015-12-22 12:15:02 -73228 [http-bio-8080-exec-5] DEBUG   - Last-Modified value for [/projectController.do] is: -1
2015-12-22 12:15:02 -73236 [http-bio-8080-exec-5] DEBUG   - Creating new transaction with name [com.fzy.project.service.impl.ProjectServiceImpl.findProjectList]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2015-12-22 12:15:02 -73242 [http-bio-8080-exec-5] DEBUG   - Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] for JDBC transaction
2015-12-22 12:15:02 -73246 [http-bio-8080-exec-5] DEBUG   - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] to manual commit
2015-12-22 12:15:02 -73251 [http-bio-8080-exec-5] DEBUG   - Creating a new SqlSession
2015-12-22 12:15:02 -73255 [http-bio-8080-exec-5] DEBUG   - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f9a41d]
2015-12-22 12:15:02 -73259 [http-bio-8080-exec-5] DEBUG   - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] will be managed by Spring
2015-12-22 12:15:02 -73264 [http-bio-8080-exec-5] DEBUG   - ==>  Preparing: select * from PROJECT order by CREATEDATE DESC 
2015-12-22 12:15:02 -73269 [http-bio-8080-exec-5] DEBUG   - ==> Parameters: 
2015-12-22 12:15:02 -73311 [http-bio-8080-exec-5] DEBUG   - <==      Total: 3
2015-12-22 12:15:02 -73317 [http-bio-8080-exec-5] DEBUG   - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f9a41d]
2015-12-22 12:15:02 -73321 [http-bio-8080-exec-5] DEBUG   - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f9a41d]
2015-12-22 12:15:02 -73326 [http-bio-8080-exec-5] DEBUG   - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f9a41d]
2015-12-22 12:15:02 -73329 [http-bio-8080-exec-5] DEBUG   - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f9a41d]
2015-12-22 12:15:02 -73334 [http-bio-8080-exec-5] DEBUG   - Initiating transaction commit
2015-12-22 12:15:02 -73337 [http-bio-8080-exec-5] DEBUG   - Committing JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0]
2015-12-22 12:15:02 -73344 [http-bio-8080-exec-5] DEBUG   - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4bb47df0] after transaction
2015-12-22 12:15:02 -73347 [http-bio-8080-exec-5] DEBUG   - Returning JDBC Connection to DataSource
dddddddd116
2015-12-22 12:15:02 -73357 [http-bio-8080-exec-5] DEBUG   - Rendering view [org.springframework.web.servlet.view.JstlView: name 'project/test'; URL [/webpage/project/test.jsp]] in DispatcherServlet with name 'spring'
2015-12-22 12:15:02 -73362 [http-bio-8080-exec-5] DEBUG   - Forwarding to resource [/webpage/project/test.jsp] in InternalResourceView 'project/test'
2015-12-22 12:15:02 -73375 [http-bio-8080-exec-5] DEBUG   - Successfully completed request

表里就3条记录,数据很小,不知道什么原因导致查询很慢,我重新做了一个一模一样的配置的工程,只留一个controller,一个service,mapper的时候,第一次查询100多ms,第二次是8ms,是正常的,不知道什么原因导致这么慢,求大家帮忙看看

加载中
返回顶部
顶部