static void runStaleReads()

in google-cloud-spanner-hibernate-samples/basic-spanner-features-sample/src/main/java/com/example/StaleReadsDemo.java [47:81]


  static void runStaleReads(SessionHelper sessionHelper) {
    System.out.println("======== Stale Reads Demo ========");

    Book book;

    try (Session session = sessionHelper.createReadWriteSession()) {
      // First save a book record in the database.
      session.beginTransaction();
      book = new Book("Super Book", "Bob Blob");
      session.save(book);
      session.getTransaction().commit();
      System.out.println("Saved book to database: " + book);

      // Perform a strong read. One book is returned.
      List<Book> booksInTable =
          session
              .createQuery("from Book b where b.id = :id", Book.class)
              .setParameter("id", book.getId())
              .list();
      System.out.println("Executing a strong read: " + booksInTable);
    }

    try (Session session = sessionHelper.createExactStaleReadSession(600)) {
      List<Book> booksInTable =
          session
              .createQuery("from Book b where b.id = :id", Book.class)
              .setParameter("id", book.getId())
              .list();
      System.out.println(
          "Executing a exact stale read 10 minutes in the past (no books should be found): "
              + booksInTable);
    }

    System.out.println("==========================");
  }