use of org.apache.derby.iapi.services.context.ContextService in project derby by apache.
the class T_CreateConglomRet method t_016.
/**
* Test deadlocks during critical times of row level locking.
* <p>
* Use trace points to force errors in split at critical points:
* leaf_split_abort{1,2,3,4}
*
* @exception StandardException Standard exception policy.
* @exception T_Fail Throws T_Fail on any test failure.
*/
protected boolean t_016(TransactionController tc) throws StandardException, T_Fail {
ScanController scan = null;
// SanityManager.DEBUG_SET("LockTrace");
REPORT("Starting t_016");
T_CreateConglomRet create_ret = new T_CreateConglomRet();
// Create the btree so that it only allows 2 rows per page.
createCongloms(tc, 2, false, false, 2, create_ret);
// Open the base table
ConglomerateController base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
// Open the secondary index
ConglomerateController index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
if (!(index_cc instanceof B2IController)) {
throw T_Fail.testFailMsg("openConglomerate returned wrong type");
}
index_cc.checkConsistency();
// Create a row and insert into base table, remembering it's location.
DataValueDescriptor[] r1 = TemplateRow.newU8Row(2);
T_SecondaryIndexRow index_row1 = new T_SecondaryIndexRow();
RowLocation base_rowloc1 = base_cc.newRowLocationTemplate();
index_row1.init(r1, base_rowloc1, 3);
// Commit the create of the tables so that the following aborts don't
// undo that work.
tc.commit();
// Open the base table
base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
// Open the secondary index
index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
// now insert enough rows to cause failure
for (int i = 100; i > 0; i -= 2) {
((SQLLongint) r1[0]).setValue(2);
((SQLLongint) r1[1]).setValue(i);
// Insert the row into the base table;remember its location.
base_cc.insertAndFetchLocation(r1, base_rowloc1);
// Insert the row into the secondary index.
if (index_cc.insert(index_row1.getRow()) != 0) {
throw T_Fail.testFailMsg("insert failed");
}
}
tc.abort();
// Now try simulated deadlocks
// RESOLVE (Mikem) - test out aborts and errors during inserts.
String[] deadlock_debug_strings = { "B2iRowLocking3_1_lockScanRow2", "B2iRowLocking3_2_lockScanRow2", // "BTreeController_doIns2",
"BTreeScan_positionAtStartPosition2", // "BTreeScan_reposition2",
"BTreeScan_fetchNextGroup2" };
for (int errs = 0; errs < deadlock_debug_strings.length; errs++) {
try {
REPORT("Doing deadlock tests: " + deadlock_debug_strings[errs]);
// latch release path through the code.
if (SanityManager.DEBUG)
SanityManager.DEBUG_SET(deadlock_debug_strings[errs]);
// Just scan the rows and make sure you see them all, mostly just
// a test to make sure no errors are thrown by the latch release
// code paths.
scan = tc.openScan(create_ret.index_conglomid, false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, null, ScanController.NA, null, null, ScanController.NA);
int row_count = 0;
while (scan.next()) {
row_count++;
}
scan.close();
throw T_Fail.testFailMsg("expected deadlock");
} catch (StandardException e) {
if (!e.getMessageId().equals(SQLState.DEADLOCK))
throw e;
ContextService contextFactory = getContextService();
// Get the context manager.
ContextManager cm = contextFactory.getCurrentContextManager();
if (SanityManager.DEBUG)
SanityManager.ASSERT(cm != null);
cm.cleanupOnError(e, isdbActive());
}
}
tc.commit();
REPORT("Ending t_016");
return true;
}
use of org.apache.derby.iapi.services.context.ContextService in project derby by apache.
the class SequenceUpdater method updateCurrentValueOnDisk.
// /////////////////////////////////////////////////////////////////////////////////
//
// DISK WRITING MINIONS
//
// /////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Update the value on disk. Does its work in a subtransaction of the user's
* execution transaction. If that fails, raises a TOO MUCH CONTENTION exception.
* </p>
*
* @return Returns true if the value was successfully updated, false if we lost a race with another session.
*/
public synchronized boolean updateCurrentValueOnDisk(Long oldValue, Long newValue) throws StandardException {
LanguageConnectionContext lcc = getLCC();
//
if (lcc == null) {
if (SanityManager.DEBUG) {
SanityManager.ASSERT(oldValue == null, "We should be flushing unused sequence values here.");
}
ContextService csf = getContextService();
ContextManager cm = csf.getCurrentContextManager();
AccessFactory af = _dd.af;
TransactionController dummyTransaction = af.getTransaction(cm);
boolean retval = updateCurrentValueOnDisk(dummyTransaction, oldValue, newValue, false);
dummyTransaction.commit();
dummyTransaction.destroy();
return retval;
}
TransactionController executionTransaction = lcc.getTransactionExecute();
TransactionController nestedTransaction = executionTransaction.startNestedUserTransaction(false, true);
if (nestedTransaction != null) {
boolean retval = false;
boolean escalateToParentTransaction = false;
try {
retval = updateCurrentValueOnDisk(nestedTransaction, oldValue, newValue, false);
} catch (StandardException se) {
if (!se.isLockTimeout()) {
if (se.isSelfDeadlock()) {
// We're blocked by a lock held by our parent transaction.
// Escalate into the parent transaction now. See DERBY-6554.
escalateToParentTransaction = true;
} else {
Monitor.logThrowable(se);
throw se;
}
}
} finally {
// DERBY-5494, if this commit does not flush log then an
// unorderly shutdown could lose the update. Do not use
// commitNoSync(), and store needs to flush user nested update
// transaction commits by default.
nestedTransaction.commit();
nestedTransaction.destroy();
if (escalateToParentTransaction) {
retval = updateCurrentValueOnDisk(executionTransaction, oldValue, newValue, false);
}
return retval;
}
}
throw tooMuchContentionException();
}
use of org.apache.derby.iapi.services.context.ContextService in project derby by apache.
the class BaseMonitor method runWithState.
protected final void runWithState(Properties properties, PrintWriter log) {
bootProperties = properties;
logging = log;
// false indicates the full monitor is required, not the lite.
if (!initialize(false)) {
dumpTempWriter(true);
return;
}
// booted or in the process of booting or shutting down.
if (setMonitor(this)) {
return;
}
MessageService.setFinder(this);
if (SanityManager.DEBUG) {
reportOn = Boolean.valueOf(PropertyUtil.getSystemProperty("derby.monitor.verbose")).booleanValue();
}
// Set up the application properties
applicationProperties = readApplicationProperties();
// The security manager may not let us get the System properties
// object itself, although it may let us look at the properties in it.
Properties systemProperties = null;
if (SanityManager.DEBUG) {
// would only work with a sane codeline.
try {
systemProperties = System.getProperties();
} catch (SecurityException se) {
}
}
Vector<Class<?>> bootImplementations = getImplementations(bootProperties, false);
Vector<Class<?>> systemImplementations = null;
Vector<Class<?>> applicationImplementations = null;
// I will fix soon, djd.
if (true || SanityManager.DEBUG) {
// Don't allow external code to override our implementations.
systemImplementations = getImplementations(systemProperties, false);
applicationImplementations = getImplementations(applicationProperties, false);
}
Vector<Class<?>> defaultImplementations = getDefaultImplementations();
int implementationCount = 0;
if (bootImplementations != null)
implementationCount++;
// TEMP - making this sanity only breaks the unit test code
if (true || SanityManager.DEBUG) {
// Don't allow external code to override our implementations.
if (systemImplementations != null)
implementationCount++;
if (applicationImplementations != null)
implementationCount++;
}
if (defaultImplementations != null)
implementationCount++;
implementationSets = new ArrayList<List<Class<?>>>(implementationCount);
if (bootImplementations != null)
implementationSets.add(bootImplementations);
if (true || SanityManager.DEBUG) {
// Don't allow external code to override our implementations.
if (systemImplementations != null)
implementationSets.add(systemImplementations);
if (applicationImplementations != null)
implementationSets.add(applicationImplementations);
}
if (defaultImplementations != null)
implementationSets.add(defaultImplementations);
if (SanityManager.DEBUG) {
// Look for the derby.debug.* properties.
if (applicationProperties != null) {
addDebugFlags(applicationProperties.getProperty(Monitor.DEBUG_FALSE), false);
addDebugFlags(applicationProperties.getProperty(Monitor.DEBUG_TRUE), true);
}
addDebugFlags(PropertyUtil.getSystemProperty(Monitor.DEBUG_FALSE), false);
addDebugFlags(PropertyUtil.getSystemProperty(Monitor.DEBUG_TRUE), true);
}
try {
systemStreams = (InfoStreams) Monitor.startSystemModule("org.apache.derby.shared.common.stream.InfoStreams");
if (SanityManager.DEBUG) {
SanityManager.SET_DEBUG_STREAM(systemStreams.stream().getPrintWriter());
}
contextService = new ContextService();
uuidFactory = (UUIDFactory) Monitor.startSystemModule("org.apache.derby.iapi.services.uuid.UUIDFactory");
timerFactory = (TimerFactory) Monitor.startSystemModule("org.apache.derby.iapi.services.timer.TimerFactory");
Monitor.startSystemModule(Module.JMX);
} catch (StandardException se) {
// if we can't create an error log or a context then there's no point going on
reportException(se);
// dump any messages we have been saving ...
dumpTempWriter(true);
return;
} catch (AccessControlException e) {
dumpTempWriter(true);
throw e;
}
// switch cover to the real error stream and
// dump any messages we have been saving ...
dumpTempWriter(false);
if (SanityManager.DEBUG && reportOn) {
dumpProperties("-- Boot Properties --", bootProperties);
dumpProperties("-- System Properties --", systemProperties);
dumpProperties("-- Application Properties --", applicationProperties);
}
// bootup all the service providers
determineSupportedServiceProviders();
// See if automatic booting of persistent services is required
boolean bootAll = Boolean.valueOf(PropertyUtil.getSystemProperty(Property.BOOT_ALL)).booleanValue();
startServices(bootProperties, bootAll);
startServices(systemProperties, bootAll);
startServices(applicationProperties, bootAll);
if (// only if automatic booting is required
bootAll)
bootPersistentServices();
}
use of org.apache.derby.iapi.services.context.ContextService in project derby by apache.
the class IndexStatisticsDaemonImpl method run.
/**
* Drives the statistics generation.
* <p>
* This method will be run in a separate thread, and it will keep working
* as long as there is work to do. When the queue is exhausted, the method
* will exit (the thread dies).
*/
public void run() {
final long runStart = System.currentTimeMillis();
ContextService ctxService = null;
// Implement the outer-level exception handling here.
try {
// DERBY-5088: Factory-call may fail.
ctxService = getContextService();
ctxService.setCurrentContextManager(ctxMgr);
processingLoop();
} catch (ShutdownException se) {
// The database is/has been shut down.
// Log processing statistics and exit.
trace(1, "swallowed shutdown exception: " + extractIstatInfo(se));
stop();
ctxMgr.cleanupOnError(se, db.isActive());
} catch (RuntimeException re) {
// on a lower level
if (!isShuttingDown()) {
log(AS_BACKGROUND_TASK, null, re, "runtime exception during normal operation");
throw re;
}
trace(1, "swallowed runtime exception during shutdown: " + extractIstatInfo(re));
} finally {
if (ctxService != null) {
ctxService.resetCurrentContextManager(ctxMgr);
}
runTime += (System.currentTimeMillis() - runStart);
trace(0, "worker thread exit");
}
}
use of org.apache.derby.iapi.services.context.ContextService in project derby by apache.
the class EmbedXAResource method commit.
/**
* Commit the global transaction specified by xid.
* @param xid A global transaction identifier
* @param onePhase If true, the resource manager should use a one-phase
* commit protocol to commit the work done on behalf of xid.
*
* @exception XAException An error has occurred. Possible XAExceptions are
* XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
* XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
* <P>If the resource manager did not commit the transaction and
* the paramether onePhase is set to true, the resource manager
* may throw one of the XA_RB* exceptions. Upon return, the
* resource manager has rolled back the branch's work and has
* released all held resources.
*/
public final synchronized void commit(Xid xid, boolean onePhase) throws XAException {
checkXAActive();
// ensure immtable and correct equals method.
XAXactId xid_im = new XAXactId(xid);
XATransactionState tranState = getTransactionState(xid_im);
if (tranState == null) {
XAResourceManager rm = ra.getXAResourceManager();
ContextManager inDoubtCM = rm.find(xid);
// RM also does not know about this xid.
if (inDoubtCM == null)
throw new XAException(XAException.XAER_NOTA);
ContextService csf = getContextService();
csf.setCurrentContextManager(inDoubtCM);
try {
rm.commit(inDoubtCM, xid_im, onePhase);
// close the connection/transaction since it can never
// be used again. DERBY-4856 No extended diagnostic information needed.
inDoubtCM.cleanupOnError(StandardException.closeException(), false);
return;
} catch (StandardException se) {
// The rm threw an exception, clean it up in the approprate
// context. There is no transactionResource to handle the
// exception for us.
inDoubtCM.cleanupOnError(se, con.isActive());
throw wrapInXAException(se);
} finally {
csf.resetCurrentContextManager(inDoubtCM);
}
}
synchronized (tranState) {
checkUserCredentials(tranState.creatingResource);
// any XAResource.
switch(tranState.associationState) {
case XATransactionState.T0_NOT_ASSOCIATED:
break;
case XATransactionState.TRO_FAIL:
throw new XAException(tranState.rollbackOnlyCode);
default:
throw new XAException(XAException.XAER_PROTO);
}
if (tranState.suspendedList != null && tranState.suspendedList.size() != 0)
throw new XAException(XAException.XAER_PROTO);
if (tranState.isPrepared == onePhase)
throw new XAException(XAException.XAER_PROTO);
try {
tranState.xa_commit(onePhase);
} catch (SQLException sqle) {
throw wrapInXAException(sqle);
} finally {
returnConnectionToResource(tranState, xid_im);
}
}
}
Aggregations