use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method evictObject.
// ----------------------------- Lifecycle Methods ------------------------------------
/**
* Internal method to evict an object from L1 cache.
* @param obj The object
* @throws NucleusException if an error occurs evicting the object
*/
public void evictObject(Object obj) {
if (obj == null) {
return;
}
try {
clr.setPrimary(obj.getClass().getClassLoader());
assertClassPersistable(obj.getClass());
assertNotDetached(obj);
// we do not directly remove from cache level 1 here. The cache level 1 will be evicted
// automatically on garbage collection, if the object can be evicted. it means not all
// jdo states allows the object to be evicted.
ObjectProvider op = findObjectProvider(obj);
if (op == null) {
throw new NucleusUserException(Localiser.msg("010048", StringUtils.toJVMIDString(obj), getApiAdapter().getIdForObject(obj), "evict"));
}
op.evict();
} finally {
clr.unsetPrimary();
}
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method findObject.
/**
* Accessor for an object given the object id and a set of field values to apply to it.
* This is intended for use where we have done a query and have the id from the results, and we want to
* create the object, preferably using the cache, and then apply any field values to it.
* @param id Id of the object.
* @param fv Field values for the object (to copy in)
* @param cls the type which the object is (optional). Used to instantiate the object
* @param ignoreCache true if it must ignore the cache
* @param checkInheritance Whether to check the inheritance on the id of the object
* @return The Object
*/
public Object findObject(Object id, FieldValues fv, Class cls, boolean ignoreCache, boolean checkInheritance) {
assertIsOpen();
Object pc = null;
ObjectProvider op = null;
if (!ignoreCache) {
// Check if an object exists in the L1/L2 caches for this id
pc = getObjectFromCache(id);
}
if (pc == null) {
// Find direct from the datastore if supported
pc = getStoreManager().getPersistenceHandler().findObject(this, id);
}
boolean createdHollow = false;
if (pc == null) {
// Determine the class details for this "id" if not provided, including checking of inheritance level
String className = cls != null ? cls.getName() : null;
if (!(id instanceof SCOID)) {
ClassDetailsForId details = getClassDetailsForId(id, className, checkInheritance);
if (details.className != null && cls != null && !cls.getName().equals(details.className)) {
cls = clr.classForName(details.className);
}
className = details.className;
id = details.id;
if (details.pc != null) {
// Found following inheritance check via the cache
pc = details.pc;
op = findObjectProvider(pc);
}
}
if (pc == null) {
// Still not found so create a Hollow instance with the supplied field values
if (cls == null) {
try {
cls = clr.classForName(className, id.getClass().getClassLoader());
} catch (ClassNotResolvedException e) {
String msg = Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id));
NucleusLogger.PERSISTENCE.warn(msg);
throw new NucleusUserException(msg, e);
}
}
createdHollow = true;
// Will put object in L1 cache
op = nucCtx.getObjectProviderFactory().newForHollow(this, cls, id, fv);
pc = op.getObject();
putObjectIntoLevel2Cache(op, false);
}
}
if (pc != null && fv != null && !createdHollow) {
// Object found in the cache so load the requested fields
if (op == null) {
op = findObjectProvider(pc);
}
if (op != null) {
// Load the requested fields
fv.fetchNonLoadedFields(op);
}
}
return pc;
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-core by datanucleus.
the class TransactionImpl method rollback.
/**
* Method to rollback the transaction.
*/
public void rollback() {
if (!isActive()) {
throw new TransactionNotActiveException();
}
long startTime = System.currentTimeMillis();
try {
// whether the transaction can be completed
boolean canComplete = true;
committing = true;
try {
// TODO Is this really needed? om.preRollback does all necessary
flush();
} finally {
// even if flush fails, we ignore and go ahead cleaning up and rolling back everything ahead...
try {
internalPreRollback();
} catch (NucleusUserException e) {
// catch only NucleusUserException; they must be cascade up to user code and transaction is still alive
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
canComplete = false;
throw e;
} finally {
if (canComplete) {
try {
internalRollback();
} finally {
try {
active = false;
if (ec.getStatistics() != null) {
ec.getStatistics().transactionRolledBack(System.currentTimeMillis() - beginTime);
}
} finally {
listenersPerTransaction.clear();
// Reset rollbackOnly flag
rollbackOnly = false;
if (sync != null) {
sync.afterCompletion(Status.STATUS_ROLLEDBACK);
}
}
}
}
}
}
} catch (NucleusUserException e) {
throw e;
} catch (NucleusException e) {
throw new NucleusDataStoreException(Localiser.msg("015009"), e);
} finally {
committing = false;
}
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(Localiser.msg("015023", System.currentTimeMillis() - startTime));
}
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-core by datanucleus.
the class TransactionImpl method commit.
/**
* Method to commit the transaction.
*/
public void commit() {
if (!isActive()) {
throw new TransactionNotActiveException();
}
// the exception and call rollback themselves. i.e we don't need to close the DB connection or set "active" to false.
if (rollbackOnly) {
// Throw an exception since can only exit via rollback
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(Localiser.msg("015020"));
}
throw new NucleusDataStoreException(Localiser.msg("015020")).setFatal();
}
long startTime = System.currentTimeMillis();
boolean success = false;
// whether the transaction can be completed
boolean canComplete = true;
List<Throwable> errors = new ArrayList();
try {
// TODO Is this needed? om.preCommit will handle flush calls
flush();
internalPreCommit();
internalCommit();
success = true;
} catch (RollbackException e) {
// in Transaction.Synchronization and they should cascade up to user code
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
errors.add(e);
} catch (HeuristicRollbackException e) {
// in Transaction.Synchronization and they should cascade up to user code
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
errors.add(e);
} catch (HeuristicMixedException e) {
// in Transaction.Synchronization and they should cascade up to user code
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
errors.add(e);
} catch (NucleusUserException e) {
// they must be cascade up to user code and transaction is still alive
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
canComplete = false;
throw e;
} catch (NucleusException e) {
// in Transaction.Synchronization and they should cascade up to user code
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
}
errors.add(e);
} finally {
if (canComplete) {
try {
if (!success) {
rollback();
} else {
internalPostCommit();
}
} catch (Throwable e) {
errors.add(e);
}
}
}
if (!errors.isEmpty()) {
throw new NucleusTransactionException(Localiser.msg("015007"), errors.toArray(new Throwable[errors.size()]));
}
if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
NucleusLogger.TRANSACTION.debug(Localiser.msg("015022", System.currentTimeMillis() - startTime));
}
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-core by datanucleus.
the class ApiAdapterFactory method getApiAdapter.
/**
* Accessor for an adapter, given the api name.
* If the API adapter doesn't yet exist will try to initialise it from the plugin information
* defined under extension-point "org.datanucleus.api_adapter".
* @param name the adapter name
* @param pluginMgr Plugin Manager
* @return The ApiAdapter
* @throws NucleusUserException when requested API not found in CLASSPATH
*/
public ApiAdapter getApiAdapter(String name, PluginManager pluginMgr) {
ApiAdapter api = adapters.get(name);
if (api == null) {
try {
api = (ApiAdapter) pluginMgr.createExecutableExtension("org.datanucleus.api_adapter", "name", name, "class-name", null, null);
if (api == null) {
String msg = Localiser.msg("022001", name);
NucleusLogger.PERSISTENCE.error(msg);
throw new NucleusUserException(msg);
}
adapterFactory.addAdapter(name, api);
} catch (// NoClassDefFoundError for some dependent class?
Error err) {
String className = pluginMgr.getAttributeValueForExtension("org.datanucleus.api_adapter", "name", name, "class-name");
String msg = Localiser.msg("022000", className, err.getMessage());
NucleusLogger.PERSISTENCE.error(msg, err);
throw new NucleusUserException(msg);
} catch (InvocationTargetException e) {
String className = pluginMgr.getAttributeValueForExtension("org.datanucleus.api_adapter", "name", name, "class-name");
String msg = Localiser.msg("022000", className, e.getTargetException());
NucleusLogger.PERSISTENCE.error(msg, e);
throw new NucleusUserException(msg);
} catch (NucleusUserException nue) {
throw nue;
} catch (Exception e) {
String className = pluginMgr.getAttributeValueForExtension("org.datanucleus.api_adapter", "name", name, "class-name");
String msg = Localiser.msg("022000", className, e.getMessage());
NucleusLogger.PERSISTENCE.error(msg, e);
throw new NucleusUserException(msg);
}
}
return api;
}
Aggregations