【发布时间】:2014-07-09 02:06:49
【问题描述】:
我不会说英语。 (我是巴西人)。
我正在尝试使用 Spring4 + JPA2 + Hibernate4 运行 Java webapp。在我的本地服务器上目前运行很酷,工作一段时间后放在 AWS 上会发生以下错误:
代理错误
代理服务器收到来自上游服务器的无效响应。 代理服务器无法处理请求 GET /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail。
原因:从远程服务器读取错误
我的文件:(不要使用任何 XML 文件,所有设置都带有注释和 Java 类。)
application.properties
# 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://xx.database.windows.net:1433;database=xxx;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
spring.datasource.username=xxxx
spring.datasource.password=xxxx
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=true
spring.data.jpa.repositories.enabled=true
# Tomcat
tomcat.accessLogEnabled=false
tomcat.protocolHeader=x-forwarded-proto
tomcat.remoteIpHeader=x-forwarded-for
tomcat.backgroundProcessorDelay=30
server.tomcat.uri-encoding=UTF-8
server.session-timeout=40
RestaurantRestController.java
@RestController
@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 Restaurant getDetail(@PathVariable("uuid") String uuid) {
Restaurant restaurant = service.getRestaurant(uuid);
return restaurant;
}
}
RestaurantServiceImpl
@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){
return repository.findOneByUUID(uuid);
}
}
编辑(包括日志)
我意识到一段时间后(可能会话过期),本地需要将近一分钟才能“重新打开”。按照日志:
2014-07-07 10:32:29 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-07 10:32:29 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-07 10:32:29 DEBUG o.s.s.w.FilterChainProxy:180 - /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail has an empty filter list
2014-07-07 10:32:29 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-07 10:32:29 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-07 10:32:29 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:251 - Returning handler method [public com.snowmanlabs.almocaqui.domain.Restaurant com.snowmanlabs.almocaqui.rest.controller.RestaurantRestController.getDetail(java.lang.String)]
2014-07-07 10:32:29 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'restaurantRestController'
2014-07-07 10:32:29 DEBUG o.s.w.s.DispatcherServlet:925 - Last-Modified value for [/almocaqui/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail] is: -1
2014-07-07 10:32:29 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:87 - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-07 10:32:29 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'transactionManager'
2014-07-07 10:32:29 DEBUG o.s.o.j.JpaTransactionManager:334 - Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f85dbd] for JPA transaction
****2014-07-07 10:32:29 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-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:403 - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565da00b]
2014-07-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:755 - Initiating transaction commit
2014-07-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:510 - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f85dbd]
2014-07-07 10:33:25 DEBUG o.s.o.j.JpaTransactionManager:603 - Not closing pre-bound JPA EntityManager after transaction
2014-07-07 10:33:25 DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor:145 - Written [com.snowmanlabs.almocaqui.domain.Restaurant@48e83911] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@5bfa9eb7]
2014-07-07 10:33:25 DEBUG o.s.w.s.DispatcherServlet:1012 - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2014-07-07 10:33:25 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:112 - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-07 10:33:25 DEBUG o.s.o.j.EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2014-07-07 10:33:25 DEBUG o.s.w.s.DispatcherServlet:991 - Successfully completed request
服务器上的这种延迟,小时工作并且小时返回错误“从远程服务器读取错误”
编辑 2
我认为是连接数据库的时间问题,我尝试配置一个连接池。
dbConfig.java 文件
@PropertySource(value = "classpath:db.properties")
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories("com.snowmanlabs.almocaqui.repository")
@Configuration
public class dbConfig {
@Autowired
Environment env;
@Bean
public BoneCPDataSource boneCPDataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(env.getProperty("jdbc.driverClass"));
boneCPDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
boneCPDataSource.setUsername(env.getProperty("jdbc.username"));
boneCPDataSource.setPassword(env.getProperty("jdbc.password"));
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
boneCPDataSource.setIdleMaxAgeInMinutes(420);
boneCPDataSource.setMaxConnectionsPerPartition(30);
boneCPDataSource.setMinConnectionsPerPartition(10);
boneCPDataSource.setPartitionCount(3);
boneCPDataSource.setAcquireIncrement(5);
boneCPDataSource.setStatementsCacheSize(100);
return boneCPDataSource;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean
@Autowired
public EntityManagerFactory entityManagerFactory(BoneCPDataSource dataSource) {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
vendorAdapter.setDatabasePlatform(env.getProperty("jdbc.database-platform"));
vendorAdapter.setDatabase(Database.SQL_SERVER);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.snowmanlabs.almocaqui.domain");
factory.setDataSource(dataSource);
Properties properties = new Properties();
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
properties.setProperty("hibernate.cache.use_query_cache", "true");
properties.setProperty("hibernate.generate_statistics", "true");
factory.setJpaProperties(properties);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
@Autowired
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
JpaDialect jpaDialect = new HibernateJpaDialect();
txManager.setEntityManagerFactory(entityManagerFactory);
txManager.setJpaDialect(jpaDialect);
return txManager;
}
}
10 分钟后出错(关闭会话)
白标错误页面
这个应用程序没有明确的 /error 映射,所以你看到 这是一个后备。
Tue Jul 08 15:01:52 BRT 2014 出现意外错误 (类型=内部服务器错误,状态=500)。无法打开 JPA EntityManager 用于事务;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.TransactionException:JDBC 开始事务失败:
【问题讨论】:
-
嗨,我遇到了同样的问题,在本地测试时,我可以访问控制器,但是当我将战争上传到 AWS 时,我找不到资源。当我将spring boot与jpa集成时产生了这个错误,之前没有问题。任何帮助将不胜感激。谢谢
标签: java spring jpa amazon-web-services tomcat7