0

I am using hibernate in dropwizard framework. Configuration of application: having a loadbalancer for the backend for multiple servers. The issue is when i am saving a new record, the records get saved in the database. But when i am trying to fetch the record from different server pointing to same database, i am not able to get the newly created row. It only exist in that server on which it was created. Hibernate cache are marked as false in the properties. Can anyone pls suggest what i am missing? or any tips.

Code :

Save api:

@Transactional
public ID save(T entity) {
    EntityManager em = getEntityManager();
    if (em.contains(entity) ) {
        em.merge(entity);
    } else {
        em.persist(entity);
    }
    em.flush();
    return (ID) entity.getId();
}

GET api:

  public T findOne(final ID id) {
    EntityManager em = getEntityManager();

    T entity = em.find(getEntityClass(), id);
    em.flush();
    return entity;
}
Ankush
  • 1
  • 5

2 Answers2

1

This is a problem that JPA has.

JPA does some magical operations in the background that speed up transactions with the database. (thats a basic overview)

Some of what the entity manager does to speed things up is caching, and batch queries.
The caching is a problem because:

Server 1 commits a new object to the database, and the data is flushed
Server 2 fetch request to get objects
    Server 2 has the data its fetching in cache so it returns the data without the new object

There are different ways to make sure this doesnt happen:

1.) have one entity manager set up a service on a server that handles all the DB interactions.

2.) Set up a master Entity manager that sync's (cache) data between other server's entity managers.

There are other ways to work around this but I believe these are the best two approaches .

Better Answer ---- JPA with Multiple Servers Based off of eclipse link.

Community
  • 1
  • 1
  • What are the standard practice used ? – Arjit Sep 11 '17 at 05:47
  • @Arjit As far as a standard I am not 100% sure. I have only used the ways I have listed. Besides that our shop was mostly db2 / SQL so we were by no means JPA experts. You are better off asking someone else. (Id rather not steer you wrong) – Andrew Riznyk Sep 11 '17 at 20:21
0

Although we can't be sure without seeing the relevant code, this sure sounds like the data either doesn't get written to the database at all, or doesn't get commmitted and thus isn't visible to other transactions.

In order to verify, this is the cause of the problem, active logging for sql statements and transactions and verify

  1. that your data gets written to the database. You should see the sql-statements for this in the log.

  2. that the transaction gets commited afterwards.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Save entity code: @Transactional public ID save(T entity) { EntityManager em = getEntityManager(); if (em.contains(entity) ) { em.merge(entity); } else { em.persist(entity); } em.flush(); return (ID) entity.getId(); } ...... Get entity code : public T findOne(final ID id) { EntityManager em = getEntityManager(); T entity = em.find(getEntityClass(), id); em.flush(); return entity; } – Ankush Jun 28 '15 at 07:11
  • .. After save, i can see data in database, and even get calls on the server on which it was created works, but when the get call is made on the diff server, i cannot find that record. – Ankush Jun 28 '15 at 07:14
  • @Ankush You don't expect anybody to actually read code, that you squished in a comment, do you? – Jens Schauder Jun 28 '15 at 08:28
  • thanks..i have updated the code part in the question itself...now its in a readable format... – Ankush Jun 28 '15 at 14:30
  • What do you mean with "I can see data in the database"? Does the logging actually show that a commit happens? – Jens Schauder Jun 29 '15 at 06:06
  • It means the data is committed , and that is why i can view that record in database. – Ankush Jun 30 '15 at 13:25