对不起我的英语不好。
第一次使用 Spring 4,我开始怀疑它是真的有帮助还是只是令人不安。当会话到期时,会在 36 到 60 秒之间重新连接。
使用:Spring-boot with Hibernate4 + JPA2 + HikariCP (pool) + Hibernate.spatial
我在瓶颈的这条线上加上了一个星号。
2014-07-08 17:59:14 DEBUG o.s.s.w.u.m.AntPathRequestMatcher:145 - Checking match of request : '/rest/restaurant/c1f15300-8bfc-496b-b90c-cf57596c8319/detail'; against '/rest/restaurant'
2014-07-08 17:59:14 DEBUG o.s.s.w.u.m.AntPathRequestMatcher:145 - Checking match of request : '/rest/restaurant/c1f15300-8bfc-496b-b90c-cf57596c8319/detail'; against '/rest/restaurant/**'
2014-07-08 17:59:14 DEBUG o.s.s.w.FilterChainProxy:180 - /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail has an empty filter list
2014-07-08 17:59:14 DEBUG o.s.w.s.DispatcherServlet:838 - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/almocaqui/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail]
2014-07-08 17:59:14 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:246 - Looking up handler method for path /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail
2014-07-08 17:59:14 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:251 - Returning handler method [public com.snowmanlabs.almocaqui.domain.external.ExternalBasicRestaurant com.snowmanlabs.almocaqui.rest.controller.RestaurantRestController.getDetail(java.lang.String)]
2014-07-08 17:59:14 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'restaurantRestController'
2014-07-08 17:59:14 DEBUG o.s.w.s.DispatcherServlet:925 - Last-Modified value for [/almocaqui/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail] is: -1
2014-07-08 17:59:14 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:87 - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-08 17:59:14 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'transactionManager'
2014-07-08 17:59:14 DEBUG o.s.o.j.JpaTransactionManager:334 - Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1007b020] for JPA transaction
***2014-07-08 17:59:14 DEBUG o.s.o.j.JpaTransactionManager:367 - Creating new transaction with name [com.snowmanlabs.almocaqui.service.implement.RestaurantServiceImpl.getRestaurant]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2014-07-08 17:59:48 DEBUG o.s.o.j.JpaTransactionManager:403 - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@356f95be]
2014-07-08 17:59:49 DEBUG o.s.o.j.JpaTransactionManager:755 - Initiating transaction commit
2014-07-08 17:59:49 DEBUG o.s.o.j.JpaTransactionManager:510 - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1007b020]
2014-07-08 17:59:49 DEBUG o.s.o.j.JpaTransactionManager:603 - Not closing pre-bound JPA EntityManager after transaction
2014-07-08 17:59:50 DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor:145 - Written [com.snowmanlabs.almocaqui.domain.external.ExternalBasicRestaurant@479caa5b] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@4a37a117]
2014-07-08 17:59:50 DEBUG o.s.w.s.DispatcherServlet:1012 - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2014-07-08 17:59:50 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:112 - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-08 17:59:50 DEBUG o.s.o.j.EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2014-07-08 17:59:50 DEBUG o.s.w.s.DispatcherServlet:991 - Successfully completed request
PS:AWS中的这个问题,使得网站下线(超过了Internal hold的最大时间)
我的文件摘要:
*RestaurantRestController.java
@Controller
@RequestMapping(value = "/rest/restaurant")
public class RestaurantRestController {
private final RestaurantService service;
@Inject
public RestaurantRestController(final RestaurantService service) {
this.service = service;
}
@RequestMapping(value = "/{uuid}/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ExternalBasicRestaurant getDetail(@PathVariable("uuid") String uuid) {
Restaurant restaurant = service.getRestaurant(uuid);
ExternalBasicRestaurant external = createExternalBasicRestaurant(restaurant);
return external;
}
@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public String handleUserAlreadyExistsException(UserAlreadyExistsException e) {
return e.getMessage();
}
}
*RestaurantServiceImpl.java
@Service
@Validated
public class RestaurantServiceImpl implements RestaurantService {
private final RestaurantRepository repository;
@Inject
public RestaurantServiceImpl(final RestaurantRepository repository) {
this.repository = repository;
}
@Override
@Transactional(readOnly = true)
public Restaurant getRestaurant(String uuid){
Restaurant restaurant = repository.findOneByUUID(uuid);
return restaurant;
}
}
*RestaurantRepository.java
@Repository
public interface RestaurantRepository extends JpaRepository<Restaurant, Integer> {
}
*application.properties
# Spring
spring.profiles.active=dev
# Server
server.port=8080
server.sessionTimeout=30
# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
java.runtime.version=1.7
#DataSource
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://x.database.windows.net:1433;database=almocaqui;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
spring.datasource.username=x
spring.datasource.password=x
spring.datasource.validationQuery=SELECT 1
spring.datasource.testOnBorrow=true
spring.datasource.poolPreparedStatements=true
# JPA
spring.jpa.database-platform=org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.autocommit=false
spring.data.jpa.repositories.enabled=true
# HikariCP
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
hibernate.hikari.minimumPoolSize=20
hibernate.hikari.maximumPoolSize=100
hibernate.hikari.idleTimeout=30
# Tomcat
tomcat.accessLogEnabled=false
tomcat.protocolHeader=x-forwarded-proto
tomcat.remoteIpHeader=x-forwarded-for
tomcat.backgroundProcessorDelay=10
server.tomcat.uri-encoding=UTF-8
server.session-timeout=10
*本地决赛:localhost:8080/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail