OPENJPA-1142 - Propagate lock level into LockException so that the proper optimistic/pessimistic exception is translated.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@835190 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Albert Lee 2009-11-12 02:04:12 +00:00
parent 983b34b68e
commit 4991e6bdc2
8 changed files with 263 additions and 47 deletions

View File

@ -49,7 +49,7 @@ public class GenericResultObjectProvider
* @param fetch the fetch configuration, or null for default
* @param res the result containing the data
*/
public GenericResultObjectProvider(Class pcClass,
public GenericResultObjectProvider(Class<?> pcClass,
JDBCStore store, JDBCFetchConfiguration fetch, Result res) {
this(store.getConfiguration().getMappingRepositoryInstance().getMapping
(pcClass, store.getContext().getClassLoader(), true),
@ -122,8 +122,7 @@ public class GenericResultObjectProvider
public void handleCheckedException(Exception e) {
if (e instanceof SQLException)
throw SQLExceptions.getStore((SQLException) e,
_store.getDBDictionary());
throw SQLExceptions.getStore((SQLException) e, _store.getDBDictionary(), _fetch.getReadLockLevel());
throw new StoreException(e);
}
}

View File

@ -106,7 +106,7 @@ public class JDBCStoreManager
private boolean _active = false;
// track the pending statements so we can cancel them
private Set _stmnts = Collections.synchronizedSet(new HashSet());
private Set<Statement> _stmnts = Collections.synchronizedSet(new HashSet<Statement>());
private static final Constructor<ClientConnection> clientConnectionImpl;
private static final Constructor<RefCountConnection> refCountConnectionImpl;
@ -293,8 +293,7 @@ public class JDBCStoreManager
} catch (ClassNotFoundException cnfe) {
throw new UserException(cnfe);
} catch (SQLException se) {
throw SQLExceptions.getStoreSQLException(sm, se, _dict,
fetch.getReadLockLevel());
throw SQLExceptions.getStore(se, _dict, fetch.getReadLockLevel());
}
}
@ -346,7 +345,7 @@ public class JDBCStoreManager
// figure out what type of object this is; the state manager
// only guarantees to provide a base class
Class type;
Class<?> type;
if ((type = getType(res, mapping)) == null) {
if (res.getBaseMapping() != null)
mapping = res.getBaseMapping();
@ -413,15 +412,15 @@ public class JDBCStoreManager
Object coll = owner.fetchObject(fms[i].getIndex());
if (coll instanceof Map)
coll = ((Map)coll).values();
if (coll instanceof Collection &&
((Collection) coll).size() > 0) {
if (coll instanceof Collection<?> &&
((Collection<?>) coll).size() > 0) {
// Found eagerly loaded collection.
// Publisher (1) <==> (M) Magazine
// publisher has a EAGER OneToMany relation
// magazine has a EAGER or LAZY ManyToOne publisher
// For each member (Magazine) in the collection,
// set its inverse relation (Publisher).
for (Iterator itr = ((Collection) coll).iterator();
for (Iterator<?> itr = ((Collection<?>) coll).iterator();
itr.hasNext();) {
PersistenceCapable pc = (PersistenceCapable) itr.next();
@ -485,7 +484,7 @@ public class JDBCStoreManager
* This method is to provide override for non-JDBC or JDBC-like
* implementation of getting type from the result set.
*/
protected Class getType(Result res, ClassMapping mapping){
protected Class<?> getType(Result res, ClassMapping mapping){
if (res == null)
return mapping.getDescribedType();
return null;
@ -646,7 +645,7 @@ public class JDBCStoreManager
} catch (ClassNotFoundException cnfe) {
throw new StoreException(cnfe);
} catch (SQLException se) {
throw SQLExceptions.getStore(se, _dict);
throw SQLExceptions.getStore(se, _dict, lockLevel);
}
}
@ -682,15 +681,15 @@ public class JDBCStoreManager
// we want to allow a different thread to be able to cancel the
// outstanding statement on a different context
Collection stmnts;
Collection<Statement> stmnts;
synchronized (_stmnts) {
if (_stmnts.isEmpty())
return false;
stmnts = new ArrayList(_stmnts);
stmnts = new ArrayList<Statement>(_stmnts);
}
try {
for (Iterator itr = stmnts.iterator(); itr.hasNext();)
for (Iterator<Statement> itr = stmnts.iterator(); itr.hasNext();)
((Statement) itr.next()).cancel();
return true;
} catch (SQLException se) {
@ -724,13 +723,13 @@ public class JDBCStoreManager
return true;
}
public Class getManagedType(Object oid) {
public Class<?> getManagedType(Object oid) {
if (oid instanceof Id)
return ((Id) oid).getType();
return null;
}
public Class getDataStoreIdType(ClassMetaData meta) {
public Class<?> getDataStoreIdType(ClassMetaData meta) {
return Id.class;
}

View File

@ -137,8 +137,7 @@ public class PessimisticLockManager
checkLock(rs, sm, timeout);
}
} catch (SQLException se) {
throw SQLExceptions.getStoreSQLException(sm, se, dict,
level);
throw SQLExceptions.getStore(se, dict, level);
} finally {
if (stmnt != null)
try { stmnt.close(); } catch (SQLException se) {}

View File

@ -152,8 +152,7 @@ public abstract class SelectResultObjectProvider
public void handleCheckedException(Exception e) {
if (e instanceof SQLException)
throw SQLExceptions.getStore((SQLException) e,
_store.getDBDictionary());
throw SQLExceptions.getStore((SQLException) e, _store.getDBDictionary(), _fetch.getReadLockLevel());
throw new StoreException(e);
}
}

View File

@ -62,12 +62,28 @@ public class SQLExceptions {
return getStore(se.getMessage(), se, dict);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(SQLException se,
DBDictionary dict, int level) {
return getStore(se.getMessage(), se, dict, level);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(SQLException se, Object failed,
DBDictionary dict) {
return getStore(se.getMessage(), se, failed, dict);
return getStore(se.getMessage(), se, failed, dict, -1);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(SQLException se, Object failed,
DBDictionary dict, int level) {
return getStore(se.getMessage(), se, failed, dict, level);
}
/**
@ -75,7 +91,15 @@ public class SQLExceptions {
*/
public static OpenJPAException getStore(Message msg, SQLException se,
DBDictionary dict) {
return getStore(msg.getMessage(), se, null, dict);
return getStore(msg.getMessage(), se, null, dict, -1);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(Message msg, SQLException se,
DBDictionary dict, int level) {
return getStore(msg.getMessage(), se, null, dict, level);
}
/**
@ -83,7 +107,15 @@ public class SQLExceptions {
*/
public static OpenJPAException getStore(String msg, SQLException se,
DBDictionary dict) {
return getStore(msg, se, null, dict);
return getStore(msg, se, null, dict, -1);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(String msg, SQLException se,
DBDictionary dict, int level) {
return getStore(msg, se, null, dict, level);
}
/**
@ -91,18 +123,29 @@ public class SQLExceptions {
*/
public static OpenJPAException getStore(String msg, SQLException se,
Object failed, DBDictionary dict) {
return getStore(msg, se, failed, dict, -1);
}
/**
* Convert the specified exception into a {@link StoreException}.
*/
public static OpenJPAException getStore(String msg, SQLException se,
Object failed, DBDictionary dict, int level) {
if (msg == null)
msg = se.getClass().getName();
SQLException[] ses = getSQLExceptions(se);
if (dict == null)
return new StoreException(msg).setFailedObject(failed).
setNestedThrowables(ses);
return dict.newStoreException(msg, ses, failed);
OpenJPAException storeEx = (dict == null) ? new StoreException(msg).setFailedObject(failed)
.setNestedThrowables(ses) : dict.newStoreException(msg, ses, failed);
if (level != -1 && storeEx.getSubtype() == StoreException.LOCK) {
LockException lockEx = (LockException) storeEx;
lockEx.setLockLevel(level);
}
return storeEx;
}
/**
* Returns an array of {@link SQLException} instances for the
* specified exception.
* Returns an array of {@link SQLException} instances for the specified
* exception.
*/
private static SQLException[] getSQLExceptions(SQLException se) {
if (se == null)
@ -115,21 +158,4 @@ public class SQLExceptions {
}
return (SQLException[]) errs.toArray(new SQLException[errs.size()]);
}
public static OpenJPAException getStoreSQLException(OpenJPAStateManager sm,
SQLException se, DBDictionary dict, int level) {
return getStoreSQLException(sm.getContext().getConfiguration(), se,
dict, level);
}
public static OpenJPAException getStoreSQLException(
OpenJPAConfiguration config, SQLException se, DBDictionary dict,
int level) {
OpenJPAException storeEx = SQLExceptions.getStore(se, dict);
if (storeEx.getSubtype() == StoreException.LOCK) {
LockException lockEx = (LockException) storeEx;
lockEx.setLockLevel(level);
}
return storeEx;
}
}

View File

@ -25,8 +25,14 @@ import java.io.ObjectOutput;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Version;
@NamedQuery(
name="findEmployeeById"
, query="SELECT c FROM LockEmployee c WHERE c.id = :id"
)
@Entity
public class LockEmployee implements Externalizable {

View File

@ -33,7 +33,10 @@ import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
@ -193,6 +196,8 @@ public abstract class SequencedActionsTest extends SQLListenerTestCase {
FindWithLock, // (int id, LockModeType lockType,
// [String properties, Object value]*)
FindObject, // (Object obj, LockModeType lockType)
NamedQueryWithLock,// (String namedQuery, int id, LockModeType lockType,
// [String properties, Object value]*)
Refresh, // ([int id])
RefreshWithLock, // (int id, LockModeType lockType,
// [String properties, Object value]*)
@ -384,6 +389,39 @@ public abstract class SequencedActionsTest extends SQLListenerTestCase {
(LockModeType) args[3]);
// log.trace("Employee=" + employee);
break;
case NamedQueryWithLock:
String namedQuery = "????";
if (args.length > 1) {
namedQuery = (String)args[1];
}
id = 1;
if (args.length > 2) {
id = (Integer)args[2];
}
lockMode = null;
if (args.length > 3) {
lockMode = (LockModeType)args[3];
}
Map<String, Object> queryProps = buildPropsMap(args, 4);
//TypedQuery<LockEmployee> q = em.createNamedQuery(namedQuery, LockEmployee.class);
Query q = em.createNamedQuery(namedQuery);
if( lockMode != null) {
q.setLockMode(lockMode);
}
if( queryProps != null) {
for( String name : queryProps.keySet()) {
q.setHint(name, queryProps.get(name));
}
}
q.setParameter("id", id);
employee = (LockEmployee)q.getSingleResult();
log.trace("Employee=" + employee);
if( employee != null ) {
employees.put(id, employee);
} else {
employees.remove(id);
}
break;
case Persist:
id = 1;
if (args[1] != null) {
@ -931,4 +969,21 @@ public abstract class SequencedActionsTest extends SQLListenerTestCase {
}
}
}
protected enum DBType {
access, db2, derby, empress, foxpro, h2, hsql, informix, ingres, jdatastore, mysql, oracle, pointbase, postgres,
sqlserver, sybase
};
protected DBType getDBType(EntityManager em) {
JDBCConfigurationImpl conf = (JDBCConfigurationImpl) getConfiguration(em);
String dictClassName = getConfiguration(em).getDBDictionaryInstance().getClass().getName();
String db = conf.dbdictionaryPlugin.alias(dictClassName);
return DBType.valueOf(db);
}
@SuppressWarnings( { "unused", "deprecation" })
protected JDBCConfiguration getConfiguration(EntityManager em) {
return ((JDBCConfiguration) ((OpenJPAEntityManager) em).getConfiguration());
}
}

View File

@ -0,0 +1,133 @@
/*
* 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.lockmgr;
import java.util.Arrays;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
/**
* Test EntityManager find/namedQuery deadlock exceptions.
*/
public class TestMixedLockManagerDeadlock extends SequencedActionsTest {
private DBType dbType;
public void setUp() {
setSupportedDatabases(
org.apache.openjpa.jdbc.sql.DerbyDictionary.class,
org.apache.openjpa.jdbc.sql.OracleDictionary.class,
org.apache.openjpa.jdbc.sql.DB2Dictionary.class);
if (isTestsDisabled()) {
return;
}
setUp(LockEmployee.class
, "openjpa.LockManager", "mixed"
);
commonSetUp();
EntityManager em = emf.createEntityManager();
dbType = getDBType(em);
}
/* ======== Find dead lock exception test ============*/
public void testFindDeadLockException() {
commonFindTest("testFindDeadLockException", LockModeType.WRITE, dbType == DBType.oracle ? null
: ExpectingOptimisticLockExClass);
commonFindTest("testFindDeadLockException", LockModeType.PESSIMISTIC_WRITE, ExpectingAnyLockExClass);
}
private void commonFindTest( String testName,
LockModeType t1Lock, Class<?>[] t1Exceptions ) {
String[] parameters = new String[] { "Thread 1: lock= " + t1Lock + ", expectedEx= "
+ Arrays.toString(t1Exceptions) };
Object[][] threadMain = {
{Act.CreateEm},
{Act.StartTx},
{Act.FindWithLock, 1, t1Lock},
{Act.Flush},
{Act.NewThread, 1 },
{Act.StartThread, 1 },
{Act.Wait, 0},
{Act.FindWithLock, 2, t1Lock},
{Act.WaitAllChildren},
{Act.TestException, 1, t1Exceptions},
};
Object[][] thread1 = {
{Act.CreateEm},
{Act.StartTx},
{Act.FindWithLock, 2, t1Lock},
{Act.Flush},
{Act.Notify, 0},
{Act.FindWithLock, 1, t1Lock},
{Act.RollbackTx},
};
launchActionSequence(testName, parameters, threadMain, thread1);
}
/* ======== named query dead lock exception test ============*/
public void testNamedQueryDeadLockException() {
commonNamedQueryTest("testNamedQueryDeadLockException", LockModeType.WRITE, dbType == DBType.oracle ? null
: ExpectingOptimisticLockExClass);
commonNamedQueryTest("testNamedQueryDeadLockException", LockModeType.PESSIMISTIC_FORCE_INCREMENT,
ExpectingAnyLockExClass);
}
private void commonNamedQueryTest( String testName,
LockModeType t1Lock, Class<?>[] t1Exceptions ) {
String[] parameters = new String[] { "Thread 1: lock= " + t1Lock + ", expectedEx= "
+ Arrays.toString(t1Exceptions) };
Object[][] threadMain = {
{Act.CreateEm},
{Act.StartTx},
{Act.NamedQueryWithLock, "findEmployeeById", 1, t1Lock, "openjpa.hint.IgnorePreparedQuery", true},
{Act.Flush},
{Act.NewThread, 1 },
{Act.StartThread, 1 },
{Act.Wait, 0},
{Act.NamedQueryWithLock, "findEmployeeById", 2, t1Lock, "openjpa.hint.IgnorePreparedQuery", true},
{Act.WaitAllChildren},
{Act.TestException, 1, t1Exceptions},
};
Object[][] thread1 = {
{Act.CreateEm},
{Act.StartTx},
{Act.NamedQueryWithLock, "findEmployeeById", 2, t1Lock, "openjpa.hint.IgnorePreparedQuery", true},
{Act.Flush},
{Act.Notify, 0},
{Act.NamedQueryWithLock, "findEmployeeById", 1, t1Lock, "openjpa.hint.IgnorePreparedQuery", true},
{Act.RollbackTx},
};
launchActionSequence(testName, parameters, threadMain, thread1);
}
}