Skip to content

Commit

Permalink
Issue #2343 - Code refactoring.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Jan 22, 2025
1 parent 3aee10d commit a3a527d
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
package org.eclipse.persistence.descriptors;

import java.sql.Timestamp;
import java.time.Instant;

import org.eclipse.persistence.exceptions.OptimisticLockException;
import org.eclipse.persistence.internal.databaseaccess.Platform;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
Expand All @@ -24,7 +24,7 @@
/**
* Version policy used for optimistic locking with {@link Instant} field.
*/
public class InstantLockingPolicy extends AbstractTsLockingPolicy<Instant> {
public class InstantLockingPolicy extends JavaTimeLockingPolicy<Instant> {

/**
* Create a new instance of version policy used for optimistic locking
Expand All @@ -35,70 +35,59 @@ public InstantLockingPolicy() {
super();
}

/**
* Create a new instance of version policy used for optimistic locking
* with {@link Instant} field.
* Defaults to using the time retrieved from the server.
*
* @param fieldName the field where the write lock value will be stored
*/
public InstantLockingPolicy(String fieldName) {
super(fieldName);
}

/**
* Create a new instance of version policy used for optimistic locking
* with {@link Instant} field.
* Defaults to using the time retrieved from the server.
*
* @param field the field where the write lock value will be stored
*/
InstantLockingPolicy(DatabaseField field) {
public InstantLockingPolicy(DatabaseField field) {
super(field);
}

@Override
int compareTsLockValues(Instant value1, Instant value2) {
int compareJavaTimeLockValues(Instant value1, Instant value2) {
return value1.compareTo(value2);
}

@Override
Class<Instant> getDefaultTsLockFieldType() {
Class<Instant> getDefaultJavaTimeLockFieldType() {
return ClassConstants.TIME_INSTANT;
}

@Override
Instant getBaseTsValue() {
Instant getBaseJavaTimeValue() {
// LocalDateTime is immutable so constant is safe
return Instant.MIN;
}

@Override
Instant getInitialTsWriteValue(AbstractSession session) {
if (usesLocalTime()) {
return Instant.now();
}
if (usesServerTime()) {
AbstractSession readSession = session.getSessionForClass(getDescriptor().getJavaClass());
while (readSession.isUnitOfWork()) {
readSession = readSession.getParent()
.getSessionForClass(getDescriptor().getJavaClass());
}
Timestamp ts = readSession.getDatasourceLogin()
.getDatasourcePlatform()
.getTimestampFromServer(session, readSession.getName());
return ts.toInstant();
Instant getInitialJavaTimeWriteValue(AbstractSession session) {
switch (getTimeSource()) {
case Local:
return Instant.now();
case Server:
AbstractSession readSession = session.getSessionForClass(getDescriptor().getJavaClass());
Platform platform = session.getDatasourcePlatform();
while (readSession.isUnitOfWork()) {
readSession = readSession.getParent()
.getSessionForClass(getDescriptor().getJavaClass());
}
return platform.convertObject(
session.executeQuery(platform.getTimestampQuery()), ClassConstants.TIME_INSTANT);
default:
return null;
}
return null;
}

@Override
Instant getNewTsLockValue(ModifyQuery query) {
return getInitialTsWriteValue(query.getSession());
Instant getNewJavaTimeLockValue(ModifyQuery query) {
return getInitialJavaTimeWriteValue(query.getSession());
}

@Override
Instant getTsValueToPutInCache(AbstractRecord row, AbstractSession session) {
Instant getJavaTimeValueToPutInCache(AbstractRecord row, AbstractSession session) {
if (isStoredInCache()) {
return session.getDatasourcePlatform()
.convertObject(row.get(getWriteLockField()), ClassConstants.TIME_INSTANT);
Expand All @@ -108,7 +97,7 @@ Instant getTsValueToPutInCache(AbstractRecord row, AbstractSession session) {
}

@Override
Instant getWriteTsLockValue(Object domainObject, Object primaryKey, AbstractSession session) {
Instant getWriteJavaTimeLockValue(Object domainObject, Object primaryKey, AbstractSession session) {
Instant writeLockFieldValue = null;
if (isStoredInCache()) {
writeLockFieldValue = (Instant) session.getIdentityMapAccessorInstance()
Expand All @@ -128,7 +117,7 @@ Instant getWriteTsLockValue(Object domainObject, Object primaryKey, AbstractSess
}

@Override
boolean isNewerTsVersion(Instant current, Object domainObject, Object primaryKey, AbstractSession session) {
boolean isNewerJavaTimeVersion(Instant current, Object domainObject, Object primaryKey, AbstractSession session) {
Instant writeLockFieldValue;
if (isStoredInCache()) {
writeLockFieldValue = (Instant) session.getIdentityMapAccessorInstance()
Expand All @@ -137,12 +126,12 @@ boolean isNewerTsVersion(Instant current, Object domainObject, Object primaryKey
writeLockFieldValue = (Instant)lockValueFromObject(domainObject);
}

return isNewerTsVersion(current, writeLockFieldValue);
return isNewerJavaTimeVersion(current, writeLockFieldValue);

}

@Override
boolean isNewerTsVersion(AbstractRecord row, Object domainObject, Object primaryKey, AbstractSession session) {
boolean isNewerJavaTimeVersion(AbstractRecord row, Object domainObject, Object primaryKey, AbstractSession session) {
Instant writeLockFieldValue;
Instant newWriteLockFieldValue = session.getDatasourcePlatform()
.convertObject(row.get(getWriteLockField()), ClassConstants.TIME_INSTANT);
Expand All @@ -152,11 +141,11 @@ boolean isNewerTsVersion(AbstractRecord row, Object domainObject, Object primary
} else {
writeLockFieldValue = (Instant) lockValueFromObject(domainObject);
}
return isNewerTsVersion(newWriteLockFieldValue, writeLockFieldValue);
return isNewerJavaTimeVersion(newWriteLockFieldValue, writeLockFieldValue);
}

@Override
boolean isNewerTsVersion(Instant first, Instant second) {
boolean isNewerJavaTimeVersion(Instant first, Instant second) {
// 2.5.1.6 if the write lock value is null, then what ever we have is treated as newer.
if (first == null) {
return false;
Expand Down
Loading

0 comments on commit a3a527d

Please sign in to comment.