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.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -26,6 +27,7 @@ public class LogClientInfo implements Filter {
|
|||||||
"/accesslog");
|
"/accesslog");
|
||||||
|
|
||||||
@Autowired AccessLogRepository AccessLogRepository;
|
@Autowired AccessLogRepository AccessLogRepository;
|
||||||
|
@Autowired Lock SQLiteWriteLock;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
@@ -54,12 +56,16 @@ public class LogClientInfo implements Filter {
|
|||||||
Instant.now(),
|
Instant.now(),
|
||||||
null,
|
null,
|
||||||
null );
|
null );
|
||||||
|
SQLiteWriteLock.lock();
|
||||||
Log = AccessLogRepository.save(Log);
|
Log = AccessLogRepository.save(Log);
|
||||||
|
SQLiteWriteLock.unlock();
|
||||||
chain.doFilter(request, Response);
|
chain.doFilter(request, Response);
|
||||||
var ResponseBody = IgnorePaths.stream()
|
var ResponseBody = IgnorePaths.stream()
|
||||||
.anyMatch( path -> HttpServletRequest.getRequestURI().startsWith(path) )
|
.anyMatch( path -> HttpServletRequest.getRequestURI().startsWith(path) )
|
||||||
? null : new String( Response.getContentAsByteArray(), Response.getCharacterEncoding() );
|
? null : new String( Response.getContentAsByteArray(), Response.getCharacterEncoding() );
|
||||||
Log.setResponseStatusCode(HttpServletResponse.getStatus());
|
Log.setResponseStatusCode(HttpServletResponse.getStatus());
|
||||||
Log.setResponseBody(ResponseBody);
|
Log.setResponseBody(ResponseBody);
|
||||||
|
SQLiteWriteLock.lock();
|
||||||
AccessLogRepository.save(Log);
|
AccessLogRepository.save(Log);
|
||||||
|
SQLiteWriteLock.unlock();
|
||||||
Response.copyBodyToResponse(); } }
|
Response.copyBodyToResponse(); } }
|
||||||
@@ -13,6 +13,8 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -50,4 +52,8 @@ class GlobalSharedBean {
|
|||||||
@Bean
|
@Bean
|
||||||
ApiKeyAuthenticationFilter ApiKeyAuthenticationFilter(){
|
ApiKeyAuthenticationFilter ApiKeyAuthenticationFilter(){
|
||||||
return new ApiKeyAuthenticationFilter(); }
|
return new ApiKeyAuthenticationFilter(); }
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Lock SQLiteWriteLock(){
|
||||||
|
return new ReentrantLock(); }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user