2014-09-24 115 views
2

所有的API調用都需要很長時間才能做出響應,因爲spring需要很長時間才能返回單例bean的緩存實例 - transactionManager。請參閱日誌,這種行爲對於每個請求都是一致的。Spring需要很長時間才能返回單例bean的緩存實例 - transactionManager

014-09-24 08:09:02,239 DEBUG servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/emsp/locations] 
2014-09-24 08:09:02,239 DEBUG annotation.RequestMappingHandlerMapping - Looking up handler method for path /locations 
2014-09-24 08:09:02,239 DEBUG annotation.RequestMappingHandlerMapping - Did not find handler method for [/locations] 
2014-09-24 08:09:02,239 DEBUG servlet.DispatcherServlet - Last-Modified value for [/emsp/locations] is: -1 
2014-09-24 08:09:02,240 DEBUG support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationEvaluationContext' 
2014-09-24 08:09:02,241 DEBUG support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'endpointLookupService' 
2014-09-24 08:09:07,407 DEBUG support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'txManager' 
2014-09-24 08:09:07,407 DEBUG hibernate4.HibernateTransactionManager - Creating new transaction with name [com.***.emsp.service.impl.EndpointLookupServiceImpl.getEndpointLocations]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
2014-09-24 08:09:07,407 DEBUG hibernate4.HibernateTransactionManager - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] orphanRemovals=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] collectionQueuedOps=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction 
2014-09-24 08:09:07,407 DEBUG hibernate4.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] orphanRemovals=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] collectionQueuedOps=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] 
2014-09-24 08:09:07,407 DEBUG internal.LogicalConnectionImpl - Obtaining JDBC connection 
2014-09-24 08:09:07,407 DEBUG resourcepool.BasicResourcePool - trace [email protected] [managed: 1, unused: 0, excluded: 0] (e.g. [email protected]) 
2014-09-24 08:09:07,407 DEBUG internal.LogicalConnectionImpl - Obtained JDBC connection 
2014-09-24 08:09:07,407 DEBUG spi.AbstractTransactionImpl - begin 
2014-09-24 08:09:07,408 DEBUG jdbc.JdbcTransaction - initial autocommit status: true 
2014-09-24 08:09:07,408 DEBUG jdbc.JdbcTransaction - disabling autocommit 
2014-09-24 08:09:07,408 DEBUG hibernate4.HibernateTransactionManager - Exposing Hibernate transaction as JDBC transaction [[email protected]] 
2014-09-24 08:09:07,408 INFO impl.EndpointLookupServiceImpl - EndpointLookupServiceImpl::getEndpointLocations - called (Custom log - after this is almost instantaneous) 

如果你看到這兩行特意在上面的行 - 有一個5秒的延遲 - 這保持了一段時間後增加,但歸結一旦Tomcat啓動。

2014-09-24 08:09:02,241 DEBUG support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'endpointLookupService' 
2014-09-24 08:09:07,407 DEBUG support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'txManager' 

我的Spring配置

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


    <context:component-scan base-package="com.****.emsp" /> 
    <!-- Transaction Manager Declaration --> 
    <bean id="txManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory"> 
      <ref bean="sessionFactory" /> 
     </property> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" /> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 
     <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/emsp" /> 
     <property name="user" value="***" /> 
     <property name="password" value="***" /> 
     <!-- C3P0 properties --> 
     <property name="acquireIncrement" value="1" /> 
     <property name="minPoolSize" value="1" /> 
     <property name="maxPoolSize" value="20" /> 
     <property name="maxIdleTime" value="300" /> 
     <property name="idleConnectionTestPeriod" value="3000" /> 
     <!--property name="testConnectionOnCheckout" value="true" /> <property 
      name="preferredTestQuery" value="select 1;"/--> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.****.emsp.entity" /> 
     <property name="hibernateProperties"> 
      <value> 
       hibernate.dialect=org.hibernate.dialect.MySQLDialect 
       hibernate.show_sql=false 
       hibernate.hbm2ddl.auto=update 
      </value> 
     </property> 
    </bean> 
</beans> 

而且我使用@Transactional註解涉及DB交易我的服務的API。

請建議如何去調試這個。另外讓我知道是否需要任何其他信息。

請求處理代碼:

@Service 
public class EndpointLookupServiceImpl implements EndpointLookupService { 
    private static final Logger logger = Logger 
      .getLogger(EndpointLookupServiceImpl.class); 

    @Autowired 
    EndpointLocationDaoImpl endpointLoctionDao; 
    @Autowired 
    SsidDaoImpl ssidDao; 

    @Override 
    @Transactional 
    public EndpointLocationsResp getEndpointLocations(
      String xJwtAssertionHeader, Map<String, List<String>> reqParams) { 
     logger.info("EndpointLookupServiceImpl::getEndpointLocations - called with xJwtAssertionHeader:" 
       + xJwtAssertionHeader + " reqParams:" + reqParams); 

     ..... 
    } 
} 

使用Spring集成爲控制器調用服務:

<int-http:inbound-gateway id="endpointLocById" 
     request-channel="endpointLocByIdIn" 
     supported-methods="GET" 
     path="/locations/{locationId}" 
     mapped-request-headers="*" 
     payload-expression="#pathVariables.locationId" > 
    </int-http:inbound-gateway> 
    <int:channel id="endpointLocByIdIn"/> 
    <int:service-activator input-channel="endpointLocByIdIn" expression="@endpointLookupService.getEndpointLocationByLocationId(headers['x-jwt-assertion'], payload)" output-channel="out" /> 
+1

請添加請求處理代碼。每次你需要豆子時,你都沒有創建上下文的新實例? – 2014-09-24 08:53:01

+0

@ M.Deinum不,我不是每次都創建一個新的上下文實例。我將請求處理代碼添加到原始消息本身,請看一看。期待您的迴應。 – Darshan 2014-09-24 10:21:47

+0

這是服務而不是我感興趣的控制器/處理程序。另外,您的服務應該可能依賴於接口'EndpointLocationDao'而不是像'EndpointLocationDaoImpl'這樣的具體實現。 – 2014-09-24 10:40:03

回答

0

採取線程轉儲之前,你看到阻塞後,看看你的主線程是什麼在Spring中,Spring在單線程中加載bean,所以你應該能夠看到它被卡住的地方並進行更多的調試。

相關問題