Java Reference
In-Depth Information
Caution
Using pessimistic locks on entities that are not subject to frequent modi-
fication may result in decreased application performance.
Using Optimistic Locking
The javax.persistence.Version annotation is used to mark a persistent field or
property as a version attribute of an entity. By adding a version attribute, the entity is en-
abled for optimistic concurrency control. The version attribute is read and updated by the
persistence provider when an entity instance is modified during a transaction. The applic-
ation may read the version attribute, but must not modify the value.
Note
Although some persistence providers may support optimistic locking
for entities that do not have version attributes, portable applications
should always use entities with version attributes when using optimistic
locking. If the application attempts to lock an entity without a version
attribute, and the persistence provider doesn't support optimistic lock-
ing for non-versioned entities, a PersistenceException will be
thrown.
The @Version annotation has the following requirements:
• Only a single @Version attribute may be defined per entity.
• The @Version attribute must be in the primary table for an entity mapped to mul-
tiple tables.
• The type of the @Version attribute must be one of the following: int , In-
teger , long , Long , short , Short , or java.sql.Timestamp .
The following code snippet shows how to define a version attribute in an entity with per-
sistent fields:
@Version
protected int version;
The following code snippet shows how to define a version attribute in an entity with per-
sistent properties:
Search WWH ::




Custom Search