Spring JpaRepository的saveAndFlush()方法块

时间:2018-12-25 14:05:34

标签: java spring-data-jpa

我有一个服务器和一个客户端应用程序。在服务器端,当我使用saveAndFlush()将实体保存到JpaRepository时,调用此方法的线程的当前执行将永远阻塞。但是,当服务器关闭并且spring上下文关闭时,该实体将毫无问题地保存到我的Derby DB中。因此,如果要在客户端使用更新的DB进行工作,则需要重新启动服务器。这是为什么?我认为此方法永远不会阻塞,它会持续存在并立即提交更改。

这是服务器代码:

@SpringBootApplication
public class Main {

private TermsRepository termsRepository;

private static final int PORT = 16105;

public static void main(String[] args) {

    SpringApplication.run(Main.class, args);

}

public Main(TermsRepository termsRepository) {

    this.termsRepository = termsRepository;

    try (ServerSocket serverSocket = new ServerSocket(PORT);
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(new Server(serverSocket));

        executorService.shutdown();

        System.out.printf("Local server is launched on port: %d\n", PORT);

        System.out.println("To shutdown type \"exit\"");

        String line;

        while ((line = reader.readLine()) != null)
            if (line.equals("exit")) {
                serverSocket.close();
                break;
            }

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

private class Server implements Runnable {

    private ServerSocket serverSocket;

    Server(ServerSocket serverSocket) {

        this.serverSocket = serverSocket;

    }

    @Override
    public void run() {

        ExecutorService es = Executors.newCachedThreadPool();

        try {
            while (true) {
                Socket clientSocket = serverSocket.accept();
                es.submit(new ClientWorker(clientSocket));
            }
        } catch (IOException e) {
        } finally {
            try {
                es.shutdown();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

} 

private class ClientWorker implements Runnable {

    private Socket clientSocket;

    ClientWorker(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {

        try (ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
             ObjectOutputStream ous = new ObjectOutputStream(clientSocket.getOutputStream())) {

        String term = (String) ois.readObject();

        String definition = (String) ois.readObject();

        Optional<Term> found = termsRepository.findByTerm(term);

        if (found.isPresent()) {

            Term foundTerm = found.get();

            foundTerm.addDefinition(new Definition(definition));

            termsRepository.saveAndFlush(foundTerm);

            // only goes here when "exit" is typed to console
            System.out.println("Saved");

            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    }
}

termsRepository在这里扩展了JpaRepository

0 个答案:

没有答案