mirror of
https://github.com/TheWhiteDog9487/WebAPI.git
synced 2026-08-11 23:01:30 +08:00
在SQLite写入时加锁,避免写入失败导致未返回响应数据
This commit is contained in:
@@ -15,6 +15,7 @@ import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -26,6 +27,7 @@ public class LogClientInfo implements Filter {
|
||||
"/accesslog");
|
||||
|
||||
@Autowired AccessLogRepository AccessLogRepository;
|
||||
@Autowired Lock SQLiteWriteLock;
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
@@ -54,12 +56,16 @@ public class LogClientInfo implements Filter {
|
||||
Instant.now(),
|
||||
null,
|
||||
null );
|
||||
SQLiteWriteLock.lock();
|
||||
Log = AccessLogRepository.save(Log);
|
||||
SQLiteWriteLock.unlock();
|
||||
chain.doFilter(request, Response);
|
||||
var ResponseBody = IgnorePaths.stream()
|
||||
.anyMatch( path -> HttpServletRequest.getRequestURI().startsWith(path) )
|
||||
? null : new String( Response.getContentAsByteArray(), Response.getCharacterEncoding() );
|
||||
Log.setResponseStatusCode(HttpServletResponse.getStatus());
|
||||
Log.setResponseBody(ResponseBody);
|
||||
SQLiteWriteLock.lock();
|
||||
AccessLogRepository.save(Log);
|
||||
SQLiteWriteLock.unlock();
|
||||
Response.copyBodyToResponse(); } }
|
||||
@@ -13,6 +13,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -50,4 +52,8 @@ class GlobalSharedBean {
|
||||
@Bean
|
||||
ApiKeyAuthenticationFilter ApiKeyAuthenticationFilter(){
|
||||
return new ApiKeyAuthenticationFilter(); }
|
||||
|
||||
@Bean
|
||||
Lock SQLiteWriteLock(){
|
||||
return new ReentrantLock(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user