mirror of https://github.com/apache/openjpa.git
OPENJPA-988 - Refactoring JPA2/MixedLockManager from persistence module to jdbc module. No functional changes.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@756066 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b00ef73b40
commit
ca00078072
|
@ -16,29 +16,29 @@
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.apache.openjpa.persistence;
|
package org.apache.openjpa.jdbc.kernel;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
|
||||||
import org.apache.openjpa.jdbc.kernel.PessimisticLockManager;
|
|
||||||
import org.apache.openjpa.jdbc.meta.ClassMapping;
|
import org.apache.openjpa.jdbc.meta.ClassMapping;
|
||||||
import org.apache.openjpa.jdbc.sql.SQLExceptions;
|
import org.apache.openjpa.jdbc.sql.SQLExceptions;
|
||||||
import org.apache.openjpa.jdbc.sql.Select;
|
import org.apache.openjpa.jdbc.sql.Select;
|
||||||
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
||||||
import org.apache.openjpa.lib.util.Localizer;
|
import org.apache.openjpa.lib.util.Localizer;
|
||||||
|
import org.apache.openjpa.util.OptimisticException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test JPA 2.0 LockTypeMode semantics using JPA 2.0 "mixed"
|
* Mixed lock manager implements both optimistic and pessimistic locking
|
||||||
* lock manager.
|
* semantics in parallel to the JPA 2.0 specification.
|
||||||
*
|
*
|
||||||
* @author Albert Lee
|
* @author Albert Lee
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public class JPA2LockManager extends PessimisticLockManager {
|
public class MixedLockManager extends PessimisticLockManager {
|
||||||
|
|
||||||
private static final Localizer _loc = Localizer
|
private static final Localizer _loc = Localizer
|
||||||
.forPackage(JPA2LockManager.class);
|
.forPackage(MixedLockManager.class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
|
@ -46,7 +46,7 @@ public class JPA2LockManager extends PessimisticLockManager {
|
||||||
* #selectForUpdate(org.apache.openjpa.jdbc.sql.Select,int)
|
* #selectForUpdate(org.apache.openjpa.jdbc.sql.Select,int)
|
||||||
*/
|
*/
|
||||||
public boolean selectForUpdate(Select sel, int lockLevel) {
|
public boolean selectForUpdate(Select sel, int lockLevel) {
|
||||||
return (lockLevel >= JPA2LockLevels.LOCK_PESSIMISTIC_READ)
|
return (lockLevel >= MixedLockLevels.LOCK_PESSIMISTIC_READ)
|
||||||
? super.selectForUpdate(sel, lockLevel) : false;
|
? super.selectForUpdate(sel, lockLevel) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,15 +58,15 @@ public class JPA2LockManager extends PessimisticLockManager {
|
||||||
*/
|
*/
|
||||||
protected void lockInternal(OpenJPAStateManager sm, int level, int timeout,
|
protected void lockInternal(OpenJPAStateManager sm, int level, int timeout,
|
||||||
Object sdata, boolean postLockVersionCheck) {
|
Object sdata, boolean postLockVersionCheck) {
|
||||||
if (level >= JPA2LockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT) {
|
if (level >= MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT) {
|
||||||
setVersionCheckOnReadLock(true);
|
setVersionCheckOnReadLock(true);
|
||||||
setVersionUpdateOnWriteLock(true);
|
setVersionUpdateOnWriteLock(true);
|
||||||
super.lockInternal(sm, level, timeout, sdata, postLockVersionCheck);
|
super.lockInternal(sm, level, timeout, sdata, postLockVersionCheck);
|
||||||
} else if (level >= JPA2LockLevels.LOCK_PESSIMISTIC_READ) {
|
} else if (level >= MixedLockLevels.LOCK_PESSIMISTIC_READ) {
|
||||||
setVersionCheckOnReadLock(true);
|
setVersionCheckOnReadLock(true);
|
||||||
setVersionUpdateOnWriteLock(false);
|
setVersionUpdateOnWriteLock(false);
|
||||||
super.lockInternal(sm, level, timeout, sdata, postLockVersionCheck);
|
super.lockInternal(sm, level, timeout, sdata, postLockVersionCheck);
|
||||||
} else if (level >= JPA2LockLevels.LOCK_OPTIMISTIC) {
|
} else if (level >= MixedLockLevels.LOCK_OPTIMISTIC) {
|
||||||
setVersionCheckOnReadLock(true);
|
setVersionCheckOnReadLock(true);
|
||||||
setVersionUpdateOnWriteLock(true);
|
setVersionUpdateOnWriteLock(true);
|
||||||
optimisticLockInternal(sm, level, timeout, sdata,
|
optimisticLockInternal(sm, level, timeout, sdata,
|
||||||
|
@ -79,19 +79,18 @@ public class JPA2LockManager extends PessimisticLockManager {
|
||||||
super.optimisticLockInternal(sm, level, timeout, sdata,
|
super.optimisticLockInternal(sm, level, timeout, sdata,
|
||||||
postLockVersionCheck);
|
postLockVersionCheck);
|
||||||
if (postLockVersionCheck) {
|
if (postLockVersionCheck) {
|
||||||
if (level >= JPA2LockLevels.LOCK_PESSIMISTIC_READ) {
|
if (level >= MixedLockLevels.LOCK_PESSIMISTIC_READ) {
|
||||||
ClassMapping mapping = (ClassMapping) sm.getMetaData();
|
ClassMapping mapping = (ClassMapping) sm.getMetaData();
|
||||||
try {
|
try {
|
||||||
if (!mapping.getVersion().checkVersion(sm, this.getStore(),
|
if (!mapping.getVersion().checkVersion(sm, this.getStore(),
|
||||||
true)) {
|
true)) {
|
||||||
throw new OptimisticLockException(_loc.get(
|
throw (new OptimisticException(_loc.get(
|
||||||
"optimistic-violation-lock").getMessage(), null, sm
|
"optimistic-violation-lock").getMessage()))
|
||||||
.getManagedInstance(), true);
|
.setFailedObject(sm.getObjectId());
|
||||||
}
|
}
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
throw SQLExceptions.getStore(se,
|
throw SQLExceptions.getStore(se, sm.getObjectId(),
|
||||||
((JDBCConfiguration) getContext().getConfiguration())
|
getStore().getDBDictionary());
|
||||||
.getDBDictionaryInstance());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -140,3 +140,5 @@ finder-cached: Cached finder for "{0}" SQL: "{1}"
|
||||||
finder-not-cachable: Finder for "{0}" is not cachable.
|
finder-not-cachable: Finder for "{0}" is not cachable.
|
||||||
finder-add-pattern: Exclusion pattern "{0}" for finder query has invalidated \
|
finder-add-pattern: Exclusion pattern "{0}" for finder query has invalidated \
|
||||||
{1} existing entries "{2}"
|
{1} existing entries "{2}"
|
||||||
|
optimistic-violation-lock: An optimistic lock violation was detected when \
|
||||||
|
locking object instance.
|
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.kernel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines lock levels used for MixedLockManager.
|
||||||
|
*
|
||||||
|
* @author Albert Lee
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public interface MixedLockLevels extends LockLevels {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic optimistic read lock level. Value of 10.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static final int LOCK_OPTIMISTIC = LOCK_READ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic optimistic write lock level. Value of 20.
|
||||||
|
*/
|
||||||
|
public static final int LOCK_OPTIMISTIC_FORCE_INCREMENT =
|
||||||
|
LockLevels.LOCK_WRITE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic pessimistic read lock level. Value of 30.
|
||||||
|
*/
|
||||||
|
public static final int LOCK_PESSIMISTIC_READ = 30;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic pessimistic write lock level. Value of 40.
|
||||||
|
*/
|
||||||
|
public static final int LOCK_PESSIMISTIC_WRITE = 40;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic pessimistic force increment level. Value of 50.
|
||||||
|
*/
|
||||||
|
public static final int LOCK_PESSIMISTIC_FORCE_INCREMENT = 50;
|
||||||
|
|
||||||
|
}
|
|
@ -39,11 +39,6 @@
|
||||||
<artifactId>openjpa-kernel</artifactId>
|
<artifactId>openjpa-kernel</artifactId>
|
||||||
<version>${pom.version}</version>
|
<version>${pom.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.openjpa</groupId>
|
|
||||||
<artifactId>openjpa-jdbc</artifactId>
|
|
||||||
<version>${pom.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-jpa_2.0_spec</artifactId>
|
<artifactId>geronimo-jpa_2.0_spec</artifactId>
|
||||||
|
|
|
@ -48,13 +48,12 @@ import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||||
import org.apache.openjpa.ee.ManagedRuntime;
|
import org.apache.openjpa.ee.ManagedRuntime;
|
||||||
import org.apache.openjpa.enhance.PCEnhancer;
|
import org.apache.openjpa.enhance.PCEnhancer;
|
||||||
import org.apache.openjpa.enhance.PCRegistry;
|
import org.apache.openjpa.enhance.PCRegistry;
|
||||||
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
|
|
||||||
import org.apache.openjpa.kernel.AbstractBrokerFactory;
|
import org.apache.openjpa.kernel.AbstractBrokerFactory;
|
||||||
import org.apache.openjpa.kernel.Broker;
|
import org.apache.openjpa.kernel.Broker;
|
||||||
import org.apache.openjpa.kernel.DelegatingBroker;
|
import org.apache.openjpa.kernel.DelegatingBroker;
|
||||||
import org.apache.openjpa.kernel.FetchConfiguration;
|
import org.apache.openjpa.kernel.FetchConfiguration;
|
||||||
import org.apache.openjpa.kernel.FindCallbacks;
|
import org.apache.openjpa.kernel.FindCallbacks;
|
||||||
import org.apache.openjpa.kernel.LockLevels;
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
import org.apache.openjpa.kernel.OpCallbacks;
|
import org.apache.openjpa.kernel.OpCallbacks;
|
||||||
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
||||||
import org.apache.openjpa.kernel.PreparedQuery;
|
import org.apache.openjpa.kernel.PreparedQuery;
|
||||||
|
@ -1075,13 +1074,14 @@ public class EntityManagerImpl
|
||||||
|
|
||||||
public LockModeType getLockMode(Object entity) {
|
public LockModeType getLockMode(Object entity) {
|
||||||
assertNotCloseInvoked();
|
assertNotCloseInvoked();
|
||||||
return JPA2LockLevels.fromLockLevel(_broker.getLockLevel(entity));
|
return MixedLockLevelsHelper.fromLockLevel(
|
||||||
|
_broker.getLockLevel(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lock(Object entity, LockModeType mode) {
|
public void lock(Object entity, LockModeType mode) {
|
||||||
assertNotCloseInvoked();
|
assertNotCloseInvoked();
|
||||||
assertValidAttchedEntity(entity);
|
assertValidAttchedEntity(entity);
|
||||||
_broker.lock(entity, JPA2LockLevels.toLockLevel(mode), -1, this);
|
_broker.lock(entity, MixedLockLevelsHelper.toLockLevel(mode), -1, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lock(Object entity) {
|
public void lock(Object entity) {
|
||||||
|
@ -1093,7 +1093,8 @@ public class EntityManagerImpl
|
||||||
public void lock(Object entity, LockModeType mode, int timeout) {
|
public void lock(Object entity, LockModeType mode, int timeout) {
|
||||||
assertNotCloseInvoked();
|
assertNotCloseInvoked();
|
||||||
assertValidAttchedEntity(entity);
|
assertValidAttchedEntity(entity);
|
||||||
_broker.lock(entity, JPA2LockLevels.toLockLevel(mode), timeout, this);
|
_broker.lock(entity, MixedLockLevelsHelper.toLockLevel(mode), timeout,
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lock(Object entity, LockModeType mode,
|
public void lock(Object entity, LockModeType mode,
|
||||||
|
@ -1104,8 +1105,8 @@ public class EntityManagerImpl
|
||||||
|
|
||||||
boolean fcPushed = pushLockProperties(mode, properties);
|
boolean fcPushed = pushLockProperties(mode, properties);
|
||||||
try {
|
try {
|
||||||
_broker.lock(entity, JPA2LockLevels.toLockLevel(mode), _broker
|
_broker.lock(entity, MixedLockLevelsHelper.toLockLevel(mode),
|
||||||
.getFetchConfiguration().getLockTimeout(), this);
|
_broker.getFetchConfiguration().getLockTimeout(), this);
|
||||||
} finally {
|
} finally {
|
||||||
popLockProperties(fcPushed);
|
popLockProperties(fcPushed);
|
||||||
}
|
}
|
||||||
|
@ -1118,7 +1119,8 @@ public class EntityManagerImpl
|
||||||
|
|
||||||
public void lockAll(Collection entities, LockModeType mode, int timeout) {
|
public void lockAll(Collection entities, LockModeType mode, int timeout) {
|
||||||
assertNotCloseInvoked();
|
assertNotCloseInvoked();
|
||||||
_broker.lockAll(entities, JPA2LockLevels.toLockLevel(mode), timeout, this);
|
_broker.lockAll(entities, MixedLockLevelsHelper.toLockLevel(mode),
|
||||||
|
timeout, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lockAll(Object... entities) {
|
public void lockAll(Object... entities) {
|
||||||
|
@ -1571,7 +1573,7 @@ public class EntityManagerImpl
|
||||||
fCfg.setLockTimeout(value);
|
fCfg.setLockTimeout(value);
|
||||||
break;
|
break;
|
||||||
case ReadLockLevel:
|
case ReadLockLevel:
|
||||||
if (value != LockLevels.LOCK_NONE
|
if (value != MixedLockLevels.LOCK_NONE
|
||||||
&& value != fCfg.getReadLockLevel()) {
|
&& value != fCfg.getReadLockLevel()) {
|
||||||
if (!fcPushed) {
|
if (!fcPushed) {
|
||||||
fCfg = _broker.pushFetchConfiguration();
|
fCfg = _broker.pushFetchConfiguration();
|
||||||
|
@ -1581,7 +1583,7 @@ public class EntityManagerImpl
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WriteLockLevel:
|
case WriteLockLevel:
|
||||||
if (value != LockLevels.LOCK_NONE
|
if (value != MixedLockLevels.LOCK_NONE
|
||||||
&& value != fCfg.getWriteLockLevel()) {
|
&& value != fCfg.getWriteLockLevel()) {
|
||||||
if (!fcPushed) {
|
if (!fcPushed) {
|
||||||
fCfg = _broker.pushFetchConfiguration();
|
fCfg = _broker.pushFetchConfiguration();
|
||||||
|
@ -1613,8 +1615,8 @@ public class EntityManagerImpl
|
||||||
FetchConfigProperty.WriteLockLevel }, properties);
|
FetchConfigProperty.WriteLockLevel }, properties);
|
||||||
}
|
}
|
||||||
// override with the specific lockMode, if needed.
|
// override with the specific lockMode, if needed.
|
||||||
int setReadLevel = JPA2LockLevels.toLockLevel(mode);
|
int setReadLevel = MixedLockLevelsHelper.toLockLevel(mode);
|
||||||
if (setReadLevel != JPA2LockLevels.LOCK_NONE) {
|
if (setReadLevel != MixedLockLevels.LOCK_NONE) {
|
||||||
// Set overriden read lock level
|
// Set overriden read lock level
|
||||||
FetchConfiguration fCfg = _broker.getFetchConfiguration();
|
FetchConfiguration fCfg = _broker.getFetchConfiguration();
|
||||||
int curReadLevel = fCfg.getReadLockLevel();
|
int curReadLevel = fCfg.getReadLockLevel();
|
||||||
|
@ -1627,13 +1629,13 @@ public class EntityManagerImpl
|
||||||
}
|
}
|
||||||
// Set overriden isolation level for pessimistic-read/write
|
// Set overriden isolation level for pessimistic-read/write
|
||||||
switch (setReadLevel) {
|
switch (setReadLevel) {
|
||||||
case JPA2LockLevels.LOCK_PESSIMISTIC_READ:
|
case MixedLockLevels.LOCK_PESSIMISTIC_READ:
|
||||||
fcPushed = setIsolationForPessimisticLock(fCfg, fcPushed,
|
fcPushed = setIsolationForPessimisticLock(fCfg, fcPushed,
|
||||||
Connection.TRANSACTION_REPEATABLE_READ);
|
Connection.TRANSACTION_REPEATABLE_READ);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JPA2LockLevels.LOCK_PESSIMISTIC_WRITE:
|
case MixedLockLevels.LOCK_PESSIMISTIC_WRITE:
|
||||||
case JPA2LockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT:
|
case MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT:
|
||||||
fcPushed = setIsolationForPessimisticLock(fCfg, fcPushed,
|
fcPushed = setIsolationForPessimisticLock(fCfg, fcPushed,
|
||||||
Connection.TRANSACTION_SERIALIZABLE);
|
Connection.TRANSACTION_SERIALIZABLE);
|
||||||
break;
|
break;
|
||||||
|
@ -1650,7 +1652,8 @@ public class EntityManagerImpl
|
||||||
fCfg = _broker.pushFetchConfiguration();
|
fCfg = _broker.pushFetchConfiguration();
|
||||||
fcPushed = true;
|
fcPushed = true;
|
||||||
}
|
}
|
||||||
((JDBCFetchConfiguration) fCfg).setIsolation(level);
|
// TODO: refactoring under OPENJPA-957
|
||||||
|
// ((JDBCFetchConfiguration) fCfg).setIsolation(level);
|
||||||
return fcPushed;
|
return fcPushed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,12 +23,12 @@ import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.persistence.LockModeType;
|
import javax.persistence.LockModeType;
|
||||||
|
|
||||||
import org.apache.openjpa.kernel.DelegatingFetchConfiguration;
|
import org.apache.openjpa.kernel.DelegatingFetchConfiguration;
|
||||||
import org.apache.openjpa.kernel.FetchConfiguration;
|
import org.apache.openjpa.kernel.FetchConfiguration;
|
||||||
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements FetchPlan via delegation to FetchConfiguration.
|
* Implements FetchPlan via delegation to FetchConfiguration.
|
||||||
|
@ -245,20 +245,20 @@ public class FetchPlanImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
public LockModeType getReadLockMode() {
|
public LockModeType getReadLockMode() {
|
||||||
return JPA2LockLevels.fromLockLevel(_fetch.getReadLockLevel());
|
return MixedLockLevelsHelper.fromLockLevel(_fetch.getReadLockLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public FetchPlan setReadLockMode(LockModeType mode) {
|
public FetchPlan setReadLockMode(LockModeType mode) {
|
||||||
_fetch.setReadLockLevel(JPA2LockLevels.toLockLevel(mode));
|
_fetch.setReadLockLevel(MixedLockLevelsHelper.toLockLevel(mode));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LockModeType getWriteLockMode() {
|
public LockModeType getWriteLockMode() {
|
||||||
return JPA2LockLevels.fromLockLevel(_fetch.getWriteLockLevel());
|
return MixedLockLevelsHelper.fromLockLevel(_fetch.getWriteLockLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public FetchPlan setWriteLockMode(LockModeType mode) {
|
public FetchPlan setWriteLockMode(LockModeType mode) {
|
||||||
_fetch.setWriteLockLevel(JPA2LockLevels.toLockLevel(mode));
|
_fetch.setWriteLockLevel(MixedLockLevelsHelper.toLockLevel(mode));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,112 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
* or more contributor license agreements. See the NOTICE file
|
|
||||||
* distributed with this work for additional information
|
|
||||||
* regarding copyright ownership. The ASF licenses this file
|
|
||||||
* to you under the Apache License, Version 2.0 (the
|
|
||||||
* "License"); you may not use this file except in compliance
|
|
||||||
* with the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.openjpa.persistence;
|
|
||||||
|
|
||||||
import javax.persistence.LockModeType;
|
|
||||||
|
|
||||||
import org.apache.openjpa.kernel.LockLevels;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines lock levels as per JPA 2.0 specification.
|
|
||||||
*
|
|
||||||
* Translates JPA-defined lock levels and OpenJPA internal lock levels.
|
|
||||||
*
|
|
||||||
* @author Albert Lee
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
public class JPA2LockLevels {
|
|
||||||
/**
|
|
||||||
* Generic optimistic no lock level. Defined since JPA 1.0.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static final int LOCK_NONE = LockLevels.LOCK_NONE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic optimistic read lock level. Defined since JPA 1.0.
|
|
||||||
* Equivalent to <code>LOCK_OPTIMISTIC</code> which is preferred.
|
|
||||||
*/
|
|
||||||
public static final int LOCK_READ = LockLevels.LOCK_READ;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic optimistic read lock level. Defined since JPA 2.0.
|
|
||||||
* Value of 10.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static final int LOCK_OPTIMISTIC = LOCK_READ;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic optimistic write lock level. Value of 20.
|
|
||||||
*/
|
|
||||||
public static final int LOCK_OPTIMISTIC_FORCE_INCREMENT =
|
|
||||||
LockLevels.LOCK_WRITE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic pessimistic read lock level. Value of 30.
|
|
||||||
*/
|
|
||||||
public static final int LOCK_PESSIMISTIC_READ = 30;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic pessimistic write lock level. Value of 40.
|
|
||||||
*/
|
|
||||||
public static final int LOCK_PESSIMISTIC_WRITE = 40;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic pessimistic force increment level. Value of 50.
|
|
||||||
*/
|
|
||||||
public static final int LOCK_PESSIMISTIC_FORCE_INCREMENT = 50;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translates the javax.persistence enum value to our internal lock level.
|
|
||||||
*/
|
|
||||||
public static int toLockLevel(LockModeType mode) {
|
|
||||||
if (mode == null || mode == LockModeType.NONE)
|
|
||||||
return LockLevels.LOCK_NONE;
|
|
||||||
if (mode == LockModeType.READ || mode == LockModeType.OPTIMISTIC)
|
|
||||||
return LockLevels.LOCK_READ;
|
|
||||||
if (mode == LockModeType.WRITE
|
|
||||||
|| mode == LockModeType.OPTIMISTIC_FORCE_INCREMENT)
|
|
||||||
return LockLevels.LOCK_WRITE;
|
|
||||||
// TODO: if (mode == LockModeType.PESSIMISTIC_READ)
|
|
||||||
// TODO: return LockLevels.LOCK_PESSIMISTIC_READ;
|
|
||||||
if (mode == LockModeType.PESSIMISTIC)
|
|
||||||
return LOCK_PESSIMISTIC_WRITE;
|
|
||||||
if (mode == LockModeType.PESSIMISTIC_FORCE_INCREMENT)
|
|
||||||
return LOCK_PESSIMISTIC_FORCE_INCREMENT;
|
|
||||||
throw new ArgumentException(mode.toString(), null, null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate our internal lock level to a javax.persistence enum value.
|
|
||||||
*/
|
|
||||||
public static LockModeType fromLockLevel(int level) {
|
|
||||||
if (level < LOCK_OPTIMISTIC)
|
|
||||||
return null;
|
|
||||||
if (level < LOCK_OPTIMISTIC_FORCE_INCREMENT)
|
|
||||||
return LockModeType.READ;
|
|
||||||
if (level < LOCK_PESSIMISTIC_READ)
|
|
||||||
return LockModeType.WRITE;
|
|
||||||
if (level < LOCK_PESSIMISTIC_WRITE)
|
|
||||||
return LockModeType.PESSIMISTIC;
|
|
||||||
// TODO: return LockModeType.PESSIMISTIC_READ;
|
|
||||||
if (level < LOCK_PESSIMISTIC_FORCE_INCREMENT)
|
|
||||||
return LockModeType.PESSIMISTIC;
|
|
||||||
// TODO: return LockModeType.PESSIMISTIC_WRITE;
|
|
||||||
return LockModeType.PESSIMISTIC_FORCE_INCREMENT;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence;
|
||||||
|
|
||||||
|
import javax.persistence.LockModeType;
|
||||||
|
|
||||||
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper methods translate between JPA-defined lock mode and OpenJPA
|
||||||
|
* internal lock levels.
|
||||||
|
*
|
||||||
|
* @author Albert Lee
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class MixedLockLevelsHelper {
|
||||||
|
/**
|
||||||
|
* Translates javax.persistence LockModeType to internal lock level.
|
||||||
|
*/
|
||||||
|
public static int toLockLevel(LockModeType mode) {
|
||||||
|
if (mode == null || mode == LockModeType.NONE)
|
||||||
|
return MixedLockLevels.LOCK_NONE;
|
||||||
|
if (mode == LockModeType.READ || mode == LockModeType.OPTIMISTIC)
|
||||||
|
return MixedLockLevels.LOCK_READ;
|
||||||
|
if (mode == LockModeType.WRITE
|
||||||
|
|| mode == LockModeType.OPTIMISTIC_FORCE_INCREMENT)
|
||||||
|
return MixedLockLevels.LOCK_WRITE;
|
||||||
|
// TODO: if (mode == LockModeType.PESSIMISTIC_READ)
|
||||||
|
// TODO: return LockLevels.LOCK_PESSIMISTIC_READ;
|
||||||
|
if (mode == LockModeType.PESSIMISTIC)
|
||||||
|
return MixedLockLevels.LOCK_PESSIMISTIC_WRITE;
|
||||||
|
return MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates internal lock level to javax.persistence LockModeType.
|
||||||
|
*/
|
||||||
|
public static LockModeType fromLockLevel(int level) {
|
||||||
|
if (level < MixedLockLevels.LOCK_OPTIMISTIC)
|
||||||
|
return null;
|
||||||
|
if (level < MixedLockLevels.LOCK_OPTIMISTIC_FORCE_INCREMENT)
|
||||||
|
return LockModeType.READ;
|
||||||
|
if (level < MixedLockLevels.LOCK_PESSIMISTIC_READ)
|
||||||
|
return LockModeType.WRITE;
|
||||||
|
if (level < MixedLockLevels.LOCK_PESSIMISTIC_WRITE)
|
||||||
|
return LockModeType.PESSIMISTIC;
|
||||||
|
// TODO: return LockModeType.PESSIMISTIC_READ;
|
||||||
|
if (level < MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT)
|
||||||
|
return LockModeType.PESSIMISTIC;
|
||||||
|
// TODO: return LockModeType.PESSIMISTIC_WRITE;
|
||||||
|
return LockModeType.PESSIMISTIC_FORCE_INCREMENT;
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ package org.apache.openjpa.persistence;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
import org.apache.openjpa.kernel.Broker;
|
import org.apache.openjpa.kernel.Broker;
|
||||||
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
import org.apache.openjpa.util.Exceptions;
|
import org.apache.openjpa.util.Exceptions;
|
||||||
import org.apache.openjpa.util.LockException;
|
import org.apache.openjpa.util.LockException;
|
||||||
import org.apache.openjpa.util.NoTransactionException;
|
import org.apache.openjpa.util.NoTransactionException;
|
||||||
|
@ -165,7 +166,7 @@ public class PersistenceExceptions
|
||||||
LockException lockEx = (LockException)
|
LockException lockEx = (LockException)
|
||||||
(ke instanceof LockException ? ke : cause);
|
(ke instanceof LockException ? ke : cause);
|
||||||
if (lockEx != null && lockEx.getLockLevel() >=
|
if (lockEx != null && lockEx.getLockLevel() >=
|
||||||
JPA2LockLevels.LOCK_PESSIMISTIC_READ) {
|
MixedLockLevels.LOCK_PESSIMISTIC_READ) {
|
||||||
if (!lockEx.isFatal()) {
|
if (!lockEx.isFatal()) {
|
||||||
e = new org.apache.openjpa.persistence
|
e = new org.apache.openjpa.persistence
|
||||||
.LockTimeoutException(
|
.LockTimeoutException(
|
||||||
|
@ -256,7 +257,8 @@ public class PersistenceExceptions
|
||||||
* direct facade-level counterpart
|
* direct facade-level counterpart
|
||||||
* (for example, ObjectNotFoundException can be translated to
|
* (for example, ObjectNotFoundException can be translated to
|
||||||
* EntityNotFoundException).
|
* EntityNotFoundException).
|
||||||
* If the above conditions are not met then return generic ArgumentException.
|
* If the above conditions are not met then return generic
|
||||||
|
* ArgumentException.
|
||||||
*
|
*
|
||||||
* In either case, preserve all the details.
|
* In either case, preserve all the details.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||||
import org.apache.openjpa.conf.OpenJPAProductDerivation;
|
import org.apache.openjpa.conf.OpenJPAProductDerivation;
|
||||||
import org.apache.openjpa.conf.Specification;
|
import org.apache.openjpa.conf.Specification;
|
||||||
|
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||||
import org.apache.openjpa.lib.conf.AbstractProductDerivation;
|
import org.apache.openjpa.lib.conf.AbstractProductDerivation;
|
||||||
import org.apache.openjpa.lib.conf.Configuration;
|
import org.apache.openjpa.lib.conf.Configuration;
|
||||||
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
||||||
|
@ -110,29 +111,29 @@ public class PersistenceProductDerivation
|
||||||
conf.addValue(new EntityManagerFactoryValue());
|
conf.addValue(new EntityManagerFactoryValue());
|
||||||
|
|
||||||
conf.readLockLevel.setAlias("optimistic", String
|
conf.readLockLevel.setAlias("optimistic", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_OPTIMISTIC));
|
.valueOf(MixedLockLevels.LOCK_OPTIMISTIC));
|
||||||
conf.readLockLevel.setAlias("optimistic-force-increment", String
|
conf.readLockLevel.setAlias("optimistic-force-increment", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_OPTIMISTIC_FORCE_INCREMENT));
|
.valueOf(MixedLockLevels.LOCK_OPTIMISTIC_FORCE_INCREMENT));
|
||||||
conf.readLockLevel.setAlias("pessimistic-read", String
|
conf.readLockLevel.setAlias("pessimistic-read", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_READ));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_READ));
|
||||||
conf.readLockLevel.setAlias("pessimistic-write", String
|
conf.readLockLevel.setAlias("pessimistic-write", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_WRITE));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_WRITE));
|
||||||
conf.readLockLevel.setAlias("pessimistic-force-increment", String
|
conf.readLockLevel.setAlias("pessimistic-force-increment", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT));
|
||||||
|
|
||||||
conf.writeLockLevel.setAlias("optimistic", String
|
conf.writeLockLevel.setAlias("optimistic", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_OPTIMISTIC));
|
.valueOf(MixedLockLevels.LOCK_OPTIMISTIC));
|
||||||
conf.writeLockLevel.setAlias("optimistic-force-increment", String
|
conf.writeLockLevel.setAlias("optimistic-force-increment", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_OPTIMISTIC_FORCE_INCREMENT));
|
.valueOf(MixedLockLevels.LOCK_OPTIMISTIC_FORCE_INCREMENT));
|
||||||
conf.writeLockLevel.setAlias("pessimistic-read", String
|
conf.writeLockLevel.setAlias("pessimistic-read", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_READ));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_READ));
|
||||||
conf.writeLockLevel.setAlias("pessimistic-write", String
|
conf.writeLockLevel.setAlias("pessimistic-write", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_WRITE));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_WRITE));
|
||||||
conf.writeLockLevel.setAlias("pessimistic-force-increment", String
|
conf.writeLockLevel.setAlias("pessimistic-force-increment", String
|
||||||
.valueOf(JPA2LockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT));
|
.valueOf(MixedLockLevels.LOCK_PESSIMISTIC_FORCE_INCREMENT));
|
||||||
|
|
||||||
conf.lockManagerPlugin.setAlias("mixed",
|
conf.lockManagerPlugin.setAlias("mixed",
|
||||||
JPA2LockManager.class.getName());
|
"org.apache.openjpa.jdbc.kernel.MixedLockManager");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -633,7 +634,8 @@ public class PersistenceProductDerivation
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (name.charAt(0)) {
|
switch (name.charAt(0)) {
|
||||||
// cases 'name' and 'transaction-type' are handled in startPersistenceUnit()
|
// cases 'name' and 'transaction-type' are handled in
|
||||||
|
// startPersistenceUnit()
|
||||||
// case 'property' for 'properties' is handled in startElement()
|
// case 'property' for 'properties' is handled in startElement()
|
||||||
case 'c': // class
|
case 'c': // class
|
||||||
_info.addManagedClassName(currentText());
|
_info.addManagedClassName(currentText());
|
||||||
|
|
|
@ -167,7 +167,5 @@ no-result: Query "{0}" selected no result, but expected unique result.
|
||||||
non-unique-result: Query "{0}" selected {1} results, but expected unique result.
|
non-unique-result: Query "{0}" selected {1} results, but expected unique result.
|
||||||
unwrap-em-invalid: EntityManager can not be unwrapped to an instance of "{0}".
|
unwrap-em-invalid: EntityManager can not be unwrapped to an instance of "{0}".
|
||||||
unwrap-query-invalid: Query can not be unwrapped to an instance of "{0}".
|
unwrap-query-invalid: Query can not be unwrapped to an instance of "{0}".
|
||||||
optimistic-violation-lock: An optimistic lock violation was detected when \
|
|
||||||
locking object instance.
|
|
||||||
invalid_entity_argument: Object being locked must be an valid and not detached \
|
invalid_entity_argument: Object being locked must be an valid and not detached \
|
||||||
entity.
|
entity.
|
||||||
|
|
|
@ -526,7 +526,10 @@ through the <link linkend="openjpa.ReadLockLevel"><literal>
|
||||||
openjpa.ReadLockLevel</literal></link> and
|
openjpa.ReadLockLevel</literal></link> and
|
||||||
<link linkend="openjpa.WriteLockLevel"><literal>openjpa.WriteLockLevel</literal>
|
<link linkend="openjpa.WriteLockLevel"><literal>openjpa.WriteLockLevel</literal>
|
||||||
</link> configuration properties. Each property accepts a value of <literal>
|
</link> configuration properties. Each property accepts a value of <literal>
|
||||||
none</literal>, <literal>read</literal>, <literal>write</literal>, or a number
|
none</literal>, <literal>read</literal>, <literal>write</literal>,
|
||||||
|
<literal>optimistic</literal>, <literal>optimistic-force-increment</literal>,
|
||||||
|
<literal>pessimistic-read</literal>, <literal>pessimistic-write</literal>,
|
||||||
|
<literal>pessimistic-force-increment</literal>, or a number
|
||||||
corresponding to a lock level defined by the
|
corresponding to a lock level defined by the
|
||||||
<link linkend="ref_guide_locking_lockmgr">lock manager</link> in use. These
|
<link linkend="ref_guide_locking_lockmgr">lock manager</link> in use. These
|
||||||
properties apply only to non-optimistic transactions; during optimistic
|
properties apply only to non-optimistic transactions; during optimistic
|
||||||
|
@ -729,12 +732,12 @@ own lock manager, or use one of the bundled options:
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
<literal>mixed</literal>: This is an alias for the
|
<literal>mixed</literal>: This is an alias for the
|
||||||
<ulink url="../javadoc/org/apache/openjpa/persistence/JPA2LockManager.html">
|
<ulink url="../javadoc/org/apache/openjpa/jdbc/kernel/MixedLockManager.html">
|
||||||
<classname>org.apache.openjpa.persistence.JPA2LockManager</classname>
|
<classname>org.apache.openjpa.jdbc.kernel.MixedLockManager</classname>
|
||||||
</ulink>, which implements the JPA 2.0 specification entity locking behaviors.
|
</ulink>, which implements the JPA 2.0 specification entity locking behaviors.
|
||||||
It combines both the optimistic and pessimistic semantics controlled by
|
It combines both the optimistic and pessimistic semantics controlled by
|
||||||
lock modes argument in methods define in the entity manager
|
lock mode argument in methods define in the EntityManager
|
||||||
and query interfaces or OpenJPA lock level properties.
|
and Query interfaces or OpenJPA lock level properties.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
The <literal>mixed</literal> LockManager inherits all the properties available
|
The <literal>mixed</literal> LockManager inherits all the properties available
|
||||||
|
@ -743,7 +746,7 @@ For example: <literal>VersionCheckOnReadLock</literal> and
|
||||||
<literal>VersionUpdateOnWriteLock</literal> properties.
|
<literal>VersionUpdateOnWriteLock</literal> properties.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
This is the default <literal>openjpa.LockManager</literal> setting in JPA.
|
This is the default <literal>openjpa.LockManager</literal> setting in OpenJPA.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
|
|
Loading…
Reference in New Issue