我不会说英语。 (我是巴西人)。
我正在尝试使用 Spring4 + JPA2 + Hibernate4 运行 Java Web 应用程序。在我的本地服务器目前运行良好,放在AWS上工作一段时间后发生以下错误:
Proxy Error
The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /rest/restaurant/C1F15300-8BFC-496B- B90C-CF57596C8319/detail.
Reason: Error reading from remote server
我的文件:(不要使用任何XML文件,所有设置都带有注释和Java类。)
应用程序属性
# 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);
}
}
编辑(包括日志)
过了一段时间(可能 session 过期)我意识到,本地需要近一分钟才能“重新打开”。遵循日志:
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 分钟后出错(关闭 session )
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jul 08 15:01:52 BRT 2014 There was an unexpected error (type=Internal Server Error, status=500). Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:
最佳答案
您的应用程序服务器可能位于reverse proxy后面。通常反向代理有超时设置。如果从后端获取答案所需的时间较长,则会出现该错误。
类似的问题发生在我身上一次,这是因为我使用了localhost
主机引用。结果 localhost
可能不明确,它可以被解释为 ipv4 或 ipv6。在某些配置中,ipv4 和 v6 之间的切换花费了很长时间,并且发生了代理错误。
从那时起,我改用 127.0.0.1
,因此它不会切换到 ipv6。
关于java - AWS (Elastic Beanstalk) + Java (Spring) = 原因 : Error reading from remote server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601868/