9.10.3 Notes on using the optimistic lock
This section describes the notes on using the optimistic lock.
- Organization of this subsection
(1) Notes on the Version field settings
The notes on the Version field settings are as follows:
-
If the Version field is not set, the version of that entity is not checked. In this case, the user shall create an application that maintains the consistency between the entity and the database.
-
If the transaction contains entities for which the Version field is set and entities for which the Version field is not set, the version is only checked for the entities where the Version field is set. Note that the non-inclusion of a version in the entity does not affect the transaction conclusion processing.
-
The user can reference the Version field value, but must not update the value. However, the user can update the Version field value during bulk update processing.
(2) Notes on using the lock method
The notes on using the lock method are as follows:
-
The lock method of EntityManager is not supported for entities that do not have the Version field (property). If an entity that does not have a Version field (property) is specified and the lock(entity, LockModeType.READ) or lock(entity, LockModeType.WRITE) is invoked, PersistenceException occurs.
-
When the state of an entity that has a Version field (property) is updated, regardless of whether the lock method is invoked, the dirty read and un-repeatable read events do not occur.
-
When you specify LockModeType.READ with Cosminexus JPA Provider, the UPDATE statement is issued to check whether the database value corresponding to the entity is changed. Therefore, the UPDATE statement sets a lock for the database. The UPDATE statement is issued during the execution of flush processing and when the transaction is committed. The UPDATE statement is also issued when there is no change in the state of the entity object. However, the statement is not issued if the entity is deleted.
(3) Exclusive control of clients in HiRDB
The optimistic lock of Cosminexus JPA Provider is a locking method that assumes that the database Isolation level is accessed with Read Committed. If the database is HiRDB, the Isolation level is Repeatable Read by default; therefore, you must change the level to Read Committed.
Set the Isolation level for each client in the PDISLLVL parameter of the data guarantee level of the client environment variable. The default value is Repeatable Read (2). Therefore, change the value to Read Committed (1). An example of a change in setting is as follows:
Example of change: PDISLLVL=1
Specify the client environment variable in the value of the environmentVariables property with the <config-property> tag of the HITACHI Connector Property file or add the client environment variable in the configuration file for the client environment variable group of HiRDB.
If the data guarantee level of the client environment variable is operated with the default Repeatable Read, a lock is set in the shared mode. Therefore, note that if you combine the issue of reference series SQL such as the find method and the issue of update series SQL such as the flush method, a deadlock occurs easily.