Data doesn't look synchronised

There are two processes one the client pay something (java websocket). And the other callback server marks two rows of two tables as payed (tomcat). However I don’t catch the modification since I disconnect and reconnect the client, like that a new entitymanager is used.

I am using hbm2ddl.auto.

Your description isn’t really helpful. What kind of modification is missing? What is the exact interact and outcome versus what do you expect?

What database are you using? Which isolation level on the JDBC driver?

I would have expected that the two properties, one in a table and one in an other table, would have been actualized during the session of the client application. However I have to disconnect and reconnect the client application to see the data actualized. That behavior makes a little inconsistencies in the data base, so if the client saves the data during that session it happens.

I am using postgresql. I don’t know the isolation level. I think it is the version 4.1. (that type of jdbc).

Thank you for answering. Best regards,

Josep

pd: this is the log of the server booting on a linux lite terminal:

-I have to say that find a bit suspicious when I tell it to not autocommit , but I think it says it autocommit.

de nov. 17, 2020 3:47:39 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: TerratsPU]
de nov. 17, 2020 3:47:40 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.3.Final}
de nov. 17, 2020 3:47:40 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
de nov. 17, 2020 3:47:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
de nov. 17, 2020 3:47:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://127.0.0.1:5432/terrats]
de nov. 17, 2020 3:47:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=terrats, password=****, autocommit=false}
de nov. 17, 2020 3:47:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
de nov. 17, 2020 3:47:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
de nov. 17, 2020 3:47:42 PM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect
de nov. 17, 2020 3:47:45 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1a28aef1] for (non-JTA) DDL execution was not in auto-commit mode; the Connection ‘local transaction’ will be committed and the Connection will be set into auto-commit mode.
de nov. 17, 2020 3:47:46 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
de nov. 17, 2020 3:47:46 PM cat.tactictic.servidorTerrats.servidor.ServidorTerrats missatgeLog

I have tested it without hbm2ddl (only not mentioning it into persistence.xml). And the results are the same.

This has nothing to do with hbm2ddl. Your description is very generic. I asked you what the specific interactions are that are happening. You need to read into database isolation levels if you don’t know what this is then and tell me what is configured. If you use repeatable read instead of read committed, you might have to restart transactions to see data from new commits.

Thank you. I think the problem are the lazy entities or collections. I was reading from the root entity. I have tried using the get property for those, the hibernate.initialize, and now I am trying to make different queries to fill the collections.

I am using jpa with a factoryentitymanager. When I have to read I use begintransaction. And when I finish I do a commit. During the connection the entitymanager is the same.

Excuse me for my few knowledge .

Best regards,

Ok, I think I have solved the lazy loading of the entities. The issue happened because there were different places where the root entity was loaded and in different ways.

I have the same problem with the payment again.
Which is the isolation of a transaction of an entitymanager while it is opened?
Because in some way seems like while the entitymanager A is opened, if there is another process B that make changes to the data, A is not capable of seeing them. But obtaining another entitymanager A’ solves the problem. However I am afraid that it has to be more inefitient to open an entitymanager each time a transaction is needed.

So what do you think about it.

Thank you,

Josep

Creating an entity manager is a cheap operation and you should use a dedicated entity manager for every transaction. Like I wrote before, the problem you have might be due to the use of the REPEATABLE_READ isolation level. If process B committed his change after process A started it’s transaction, A will only see changes of B when using the READ_COMMITTED isolation level. There re plenty of learning resources online about this topic.

Finally I solved it.

I have applied the opening and closing of the entitymanager inside the opening and closing of the transaction. What I needed was to implement the proxies and here is what I found to do that:

public static <T> T initializeAndUnproxy(T entity) {
    if (entity == null) {
        return null;
    }

    Hibernate.initialize(entity);
    if (entity instanceof HibernateProxy) {
        entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
                .getImplementation();
    }
    return entity;
}

The use of this is like that:
user = initializeAndUnproxy(user);

If not, it seems it works fine, but it doesn’t. For example without assigning:
initializeAndUnproxy(user);

At the beginning I set the READ_COMMITED isolation, however I don’t use jta so I tried to not use it, and works fine without it.

Thank you beikov it has been a good journey.

Best regards,

Josep