Search code examples
jpacaching

Could I use stored persistence context if DB temporarily shut down?


I have to implement some kind of cache, or temporal storage, that matches the following condition :

  • This cache stores 4 tables from a specific (maria) DB. len(column) < 50, len(row) < 1000
  • This cache runs innate when a spring web server turned on. When server turned on, it immediately crawls data from DB, stores them into cache, to minimize direct DB querying.
  • The spring web server fetches data from cache when received HTTP.get resquest.
  • The spring web server updates DB column when received HTTP.post, HTTP.delete, HTTP.put, by updating the data in cache, and paste them into tables consequently.
  • The spring web server must not invoke exception when DB suddenly shuts down, and lose connection. It must handle HTTP requests by cached data, delaying any direct connect logic to DB, and synchronize DB data when DB restores.

I'm not familiar to JPA, but it seems that Spring JPA itself supports the former 4 conditions, by using EntityManager and Persistence context.

However, I cannot find any information that makes this context tolerant. I cannot find any option that makes whole JPA structure Check DB connection alive, and only update after checking returns true.

Since I'm requested to use JPA as far as I could, I want to find out whether using JPA to match the conditions above is possible or not.

Thanks for any Information provided.


Solution

  • Could I use stored persistence context if DB temporarily shut down?

    No, not really.

    The persistence context gets flushed on every commit, which you don't want, because you want to serve queries from the cache.

    Also it doesn't serve the result of any kind of query, it just serves entities.

    And most importantly: when a flush event happens and the database is not available you will get an exception.