1

I am looking for days to understand the cache with postgressql Here is the POST class

public class Post implements Serializable {
private static final long serialVersionUID = 0L;
private String id;
private String title;
private String description;
private LocalDate creationDate;
private String author;
// rest of the setter and getter are omitted
}

Here is the code that I could not understand at all

@Override
public Post load(String key) throws CacheLoaderException {
    Map<String, Object> inputParam = new HashMap<>();
    inputParam.put("id", key);
    return jdbcTemplate.queryForObject("SELECT * FROM POSTS WHERE id=?", inputParam, new RowMapper<Post>() {
        @Override
        //WHAT THE mapRow method does? Is there another way to do some thing?
        public Post mapRow(ResultSet rs, int i) throws SQLException {
            return new Post(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5));
        }
    });

}

Here is the full code : link

These codes are taken from High Performance in-memory computing with Apache Ignite book. thank you...

monstereo
  • 830
  • 11
  • 31

1 Answers1

0

Here you try to process next SQL request:

"SELECT * FROM POSTS WHERE id=?"

For example table "POSTS" have 5 entries. Every POST entry contains 5 fields. When you execute this SQL command in the database you will get 5 rows. Every row will contain this 5 fields in some order (depends on how the table was created).

In your example you are going to use jdbcTemplate.queryForObject method. It required to implement the RowMapper object that will proceed the rows from "SELECT * FROM POSTS WHERE id=?".

You can read about it here:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/RowMapper.html#mapRow-java.sql.ResultSet-int-

How it works:

For example "SELECT * FROM POSTS WHERE id=?" should return 1 or more rows for some id. Every row will be presented as ResultSet object (you can look at it as on row with 5 fields). Every ResultSet (row) should be transformed into POST object before it will be returned to the user.

public Post mapRow(ResultSet rs, int i) throws SQLException {
    return new Post(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5));
}

This code just says that you get the row with 5 fields (string, string, string, date, string) and build the POST object using this 5 fields.

  • thank you for your comment, I understood what it is going on there. However, there is a another question that should be answered. How I will be sure that such a this condition will not create a contradiction -> Let's say the cache1 will delete Person in cache memory and also from database, and at the same time another cache2 wants to load the Person that cache1 attempted the delete. My solution should be like that -> cache2 should not see the that Person. Ignite is providing this solution or I have to think about it? Note that cache mode is REPLICATED – monstereo Jun 30 '18 at 20:57
  • First of all, you should read about SQL transactions and atomicity modes in Ignite - https://apacheignite.readme.io/docs/transactions. In case if you will remove/read/write/update the data from the cache in some transaction then any other clients will not be able to get this data until this transaction isn't complete or rollback. Otherwise, it will be possible that some data will be in some inconsistence mode. You can read about it here - https://en.wikipedia.org/wiki/Isolation_(database_systems) – Andrei Aleksandrov Jul 02 '18 at 10:12
  • thanks again, unfortunately, I have many questions. I believe that I have solved the my problem. I have posted my plan to solve that situation in another post via some codes. Could you look at it? Here is the link https://stackoverflow.com/questions/51142575/apache-ignite-cache-gets-all-data-from-database – monstereo Jul 02 '18 at 19:27