Search in sources :

Example 26 with TransactionController

use of org.apache.derby.iapi.store.access.TransactionController in project derby by apache.

the class InsertNode method makeConstantAction.

/**
 * Compile constants that Execution will use
 *
 * @exception StandardException		Thrown on failure
 */
@Override
public ConstantAction makeConstantAction() throws StandardException {
    /* Different constant actions for base tables and updatable VTIs */
    if (targetTableDescriptor != null) {
        // Base table
        long heapConglomId = targetTableDescriptor.getHeapConglomerateId();
        TransactionController tc = getLanguageConnectionContext().getTransactionCompile();
        int numIndexes = (targetTableDescriptor != null) ? indexConglomerateNumbers.length : 0;
        StaticCompiledOpenConglomInfo[] indexSCOCIs = new StaticCompiledOpenConglomInfo[numIndexes];
        for (int index = 0; index < numIndexes; index++) {
            indexSCOCIs[index] = tc.getStaticCompiledConglomInfo(indexConglomerateNumbers[index]);
        }
        /*
			** If we're doing bulk insert, do table locking regardless of
			** what the optimizer decided.  This is because bulk insert is
			** generally done with a large number of rows into an empty table.
			** We also do table locking if the table's lock granularity is
			** set to table.
			*/
        if (bulkInsert || targetTableDescriptor.getLockGranularity() == TableDescriptor.TABLE_LOCK_GRANULARITY) {
            lockMode = TransactionController.MODE_TABLE;
        }
        return getGenericConstantActionFactory().getInsertConstantAction(targetTableDescriptor, heapConglomId, tc.getStaticCompiledConglomInfo(heapConglomId), indicesToMaintain, indexConglomerateNumbers, indexSCOCIs, indexNames, deferred, false, hasDeferrableCheckConstraints, targetTableDescriptor.getUUID(), lockMode, null, null, targetProperties, getFKInfo(), getTriggerInfo(), resultColumnList.getStreamStorableColIds(targetTableDescriptor.getNumberOfColumns()), getIndexedCols(), (UUID) null, null, null, resultSet.isOneRowResultSet(), autoincRowLocation, inMatchingClause(), identitySequenceUUIDString);
    } else {
        /* Return constant action for VTI
			 * NOTE: ConstantAction responsible for preserving instantiated
			 * VTIs for in-memory queries and for only preserving VTIs
			 * that implement Serializable for SPSs.
			 */
        return getGenericConstantActionFactory().getUpdatableVTIConstantAction(DeferModification.INSERT_STATEMENT, deferred);
    }
}
Also used : StaticCompiledOpenConglomInfo(org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 27 with TransactionController

use of org.apache.derby.iapi.store.access.TransactionController in project derby by apache.

the class Deadlock method buildException.

/**
 * Build an exception that describes a deadlock.
 *
 * @param factory the lock factory requesting the exception
 * @param data an array with information about who's involved in
 * a deadlock (as returned by {@link #handle})
 * @return a deadlock exception
 */
static StandardException buildException(AbstractPool factory, Object[] data) {
    Stack chain = (Stack) data[0];
    Dictionary waiters = (Dictionary) data[1];
    LanguageConnectionContext lcc = (LanguageConnectionContext) getContext(LanguageConnectionContext.CONTEXT_ID);
    TableNameInfo tabInfo = null;
    TransactionInfo[] tt = null;
    TransactionController tc = null;
    if (lcc != null) {
        try {
            tc = lcc.getTransactionExecute();
            tabInfo = new TableNameInfo(lcc, false);
            tt = tc.getAccessManager().getTransactionInfo();
        } catch (StandardException se) {
        // just don't get any table info.
        }
    }
    StringBuffer sb = new StringBuffer(200);
    Hashtable<String, Object> attributes = new Hashtable<String, Object>(17);
    String victimXID = null;
    for (int i = 0; i < chain.size(); i++) {
        Object space = chain.elementAt(i);
        if (space instanceof List) {
            List grants = (List) space;
            if (grants.size() != 0) {
                sb.append("  Granted XID : ");
                for (int j = 0; j < grants.size(); j++) {
                    if (j != 0)
                        sb.append(", ");
                    Lock gl = (Lock) grants.get(j);
                    sb.append("{");
                    sb.append(gl.getCompatabilitySpace().getOwner());
                    sb.append(", ");
                    sb.append(gl.getQualifier());
                    sb.append("} ");
                }
                sb.append('\n');
            }
            continue;
        }
        // Information about the lock we are waiting on
        // TYPE |TABLENAME                     |LOCKNAME
        Lock lock = ((Lock) waiters.get(space));
        // see if this lockable object wants to participate
        lock.getLockable().lockAttributes(VirtualLockTable.ALL, attributes);
        addInfo(sb, "Lock : ", attributes.get(VirtualLockTable.LOCKTYPE));
        if (tabInfo != null) {
            Long conglomId = (Long) attributes.get(VirtualLockTable.CONGLOMID);
            if (conglomId == null) {
                Long containerId = (Long) attributes.get(VirtualLockTable.CONTAINERID);
                try {
                    conglomId = tc.findConglomid(containerId.longValue());
                } catch (StandardException se) {
                }
            }
            addInfo(sb, ", ", tabInfo.getTableName(conglomId));
        }
        addInfo(sb, ", ", attributes.get(VirtualLockTable.LOCKNAME));
        sb.append('\n');
        String xid = String.valueOf(lock.getCompatabilitySpace().getOwner());
        if (i == 0)
            victimXID = xid;
        addInfo(sb, "  Waiting XID : {", xid);
        addInfo(sb, ", ", lock.getQualifier());
        sb.append("} ");
        if (tt != null) {
            for (int tti = tt.length - 1; tti >= 0; tti--) {
                TransactionInfo ti = tt[tti];
                // ti.getTransactionIdString() or ti can return null.
                if (ti != null) {
                    String idString = ti.getTransactionIdString();
                    if (idString != null && idString.equals(xid)) {
                        addInfo(sb, ", ", ti.getUsernameString());
                        addInfo(sb, ", ", ti.getStatementTextString());
                        break;
                    }
                }
            }
        }
        sb.append('\n');
        attributes.clear();
    }
    StandardException se = StandardException.newException(SQLState.DEADLOCK, sb.toString(), victimXID);
    se.setReport(factory.deadlockMonitor);
    return se;
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) Stack(java.util.Stack) StandardException(org.apache.derby.shared.common.error.StandardException) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) TransactionInfo(org.apache.derby.iapi.store.access.TransactionInfo) List(java.util.List) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 28 with TransactionController

use of org.apache.derby.iapi.store.access.TransactionController in project derby by apache.

the class AuthenticationServiceBase method getProperty.

/**
 * Returns a property if it was set at the database or
 * system level. Treated as SERVICE property by default.
 *
 * @return a property string value.
 */
public String getProperty(String key) {
    String propertyValue = null;
    TransactionController tc = null;
    try {
        tc = getTransaction();
        propertyValue = PropertyUtil.getServiceProperty(tc, key, (String) null);
        if (tc != null) {
            tc.commit();
            tc = null;
        }
    } catch (StandardException se) {
    // Do nothing and just return
    }
    return propertyValue;
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 29 with TransactionController

use of org.apache.derby.iapi.store.access.TransactionController in project derby by apache.

the class AuthenticationServiceBase method getDatabaseProperty.

public String getDatabaseProperty(String key) {
    String propertyValue = null;
    TransactionController tc = null;
    try {
        if (store != null)
            tc = store.getTransaction(getContextService().getCurrentContextManager());
        propertyValue = PropertyUtil.getDatabaseProperty(tc, key);
        if (tc != null) {
            tc.commit();
            tc = null;
        }
    } catch (StandardException se) {
    // Do nothing and just return
    }
    return propertyValue;
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 30 with TransactionController

use of org.apache.derby.iapi.store.access.TransactionController in project derby by apache.

the class IndexStatisticsDaemonImpl method updateIndexStatsMinion.

/**
 * Updates the index statistics for the given table and the specified
 * indexes.
 * <p>
 * <strong>API note</strong>: Using {@code null} to update the statistics
 * for all conglomerates is preferred over explicitly passing an array with
 * all the conglomerates for the table. Doing so allows for some
 * optimizations, and will cause a disposable statistics check to be
 * performed.
 *
 * @param lcc language connection context used to perform the work
 * @param td the table to update index stats for
 * @param cds the conglomerates to update statistics for (non-index
 *      conglomerates will be ignored), {@code null} means all indexes
 * @param asBackgroundTask whether the updates are done automatically as
 *      part of a background task or if explicitly invoked by the user
 * @throws StandardException if something goes wrong
 */
private void updateIndexStatsMinion(LanguageConnectionContext lcc, TableDescriptor td, ConglomerateDescriptor[] cds, boolean asBackgroundTask) throws StandardException {
    // can only properly identify disposable stats if cds == null,
    // which means we are processing all indexes on the conglomerate.
    final boolean identifyDisposableStats = (cds == null);
    // Fetch descriptors if we're updating statistics for all indexes.
    if (cds == null) {
        cds = td.getConglomerateDescriptors();
    }
    // Extract/derive information from the table descriptor
    long[] conglomerateNumber = new long[cds.length];
    ExecIndexRow[] indexRow = new ExecIndexRow[cds.length];
    TransactionController tc = lcc.getTransactionExecute();
    ConglomerateController heapCC = tc.openConglomerate(td.getHeapConglomerateId(), false, 0, TransactionController.MODE_RECORD, asBackgroundTask ? TransactionController.ISOLATION_READ_UNCOMMITTED : TransactionController.ISOLATION_REPEATABLE_READ);
    // create a list of indexes that should have statistics, by looking
    // at all indexes on the conglomerate, and conditionally skipping
    // unique single column indexes.  This set is the "non disposable
    // stat list".
    UUID[] non_disposable_objectUUID = new UUID[cds.length];
    try {
        for (int i = 0; i < cds.length; i++) {
            // Skip non-index conglomerates
            if (!cds[i].isIndex()) {
                conglomerateNumber[i] = -1;
                continue;
            }
            IndexRowGenerator irg = cds[i].getIndexDescriptor();
            // or we are running in soft-upgrade-mode on a pre 10.9 db.
            if (skipDisposableStats) {
                if (irg.isUnique() && irg.numberOfOrderedColumns() == 1) {
                    conglomerateNumber[i] = -1;
                    continue;
                }
            }
            // at this point have found a stat for an existing
            // index which is not a single column unique index, add it
            // to the list of "non disposable stats"
            conglomerateNumber[i] = cds[i].getConglomerateNumber();
            non_disposable_objectUUID[i] = cds[i].getUUID();
            indexRow[i] = irg.getNullIndexRow(td.getColumnDescriptorList(), heapCC.newRowLocationTemplate());
        }
    } finally {
        heapCC.close();
    }
    if (identifyDisposableStats) {
        // Note this loop is not controlled by the skipDisposableStats
        // flag.  The above loop controls if we drop single column unique
        // index stats or not.  In all cases we are going to drop
        // stats with no associated index (orphaned stats).
        List<StatisticsDescriptor> existingStats = td.getStatistics();
        StatisticsDescriptor[] stats = (StatisticsDescriptor[]) existingStats.toArray(new StatisticsDescriptor[existingStats.size()]);
        // those entries that don't have a matching conglomerate in the
        for (int si = 0; si < stats.length; si++) {
            UUID referencedIndex = stats[si].getReferenceID();
            boolean isValid = false;
            for (int ci = 0; ci < conglomerateNumber.length; ci++) {
                if (referencedIndex.equals(non_disposable_objectUUID[ci])) {
                    isValid = true;
                    break;
                }
            }
            // mechanism in case of another bug like DERBY-5681 in Derby.
            if (!isValid) {
                String msg = "dropping disposable statistics entry " + stats[si].getUUID() + " for index " + stats[si].getReferenceID() + " (cols=" + stats[si].getColumnCount() + ")";
                logAlways(td, null, msg);
                trace(1, msg + " on table " + stats[si].getTableUUID());
                DataDictionary dd = lcc.getDataDictionary();
                if (!lcc.dataDictionaryInWriteMode()) {
                    dd.startWriting(lcc);
                }
                dd.dropStatisticsDescriptors(td.getUUID(), stats[si].getReferenceID(), tc);
                if (asBackgroundTask) {
                    lcc.internalCommit(true);
                }
            }
        }
    }
    // [x][0] = conglomerate number, [x][1] = start time, [x][2] = stop time
    long[][] scanTimes = new long[conglomerateNumber.length][3];
    int sci = 0;
    for (int indexNumber = 0; indexNumber < conglomerateNumber.length; indexNumber++) {
        if (conglomerateNumber[indexNumber] == -1)
            continue;
        // Check if daemon has been disabled.
        if (asBackgroundTask) {
            if (isShuttingDown()) {
                break;
            }
        }
        scanTimes[sci][0] = conglomerateNumber[indexNumber];
        scanTimes[sci][1] = System.currentTimeMillis();
        // Subtract one for the RowLocation added for indexes.
        int numCols = indexRow[indexNumber].nColumns() - 1;
        long[] cardinality = new long[numCols];
        KeyComparator cmp = new KeyComparator(indexRow[indexNumber]);
        /* Read uncommitted, with record locking. Actually CS store may
               not hold record locks */
        GroupFetchScanController gsc = tc.openGroupFetchScan(conglomerateNumber[indexNumber], // hold
        false, 0, // locking
        TransactionController.MODE_RECORD, TransactionController.ISOLATION_READ_UNCOMMITTED, // scancolumnlist-- want everything.
        null, // startkeyvalue-- start from the beginning.
        null, 0, // qualifiers, none!
        null, // stopkeyvalue,
        null, 0);
        try {
            int rowsFetched = 0;
            boolean giving_up_on_shutdown = false;
            while ((rowsFetched = cmp.fetchRows(gsc)) > 0) {
                // I/O that is processed as a convenient point.
                if (asBackgroundTask) {
                    if (isShuttingDown()) {
                        giving_up_on_shutdown = true;
                        break;
                    }
                }
                for (int i = 0; i < rowsFetched; i++) {
                    int whichPositionChanged = cmp.compareWithPrevKey(i);
                    if (whichPositionChanged >= 0) {
                        for (int j = whichPositionChanged; j < numCols; j++) cardinality[j]++;
                    }
                }
            }
            if (giving_up_on_shutdown)
                break;
            gsc.setEstimatedRowCount(cmp.getRowCount());
        } finally // try
        {
            gsc.close();
            gsc = null;
        }
        scanTimes[sci++][2] = System.currentTimeMillis();
        // We have scanned the indexes, so let's give this a few attempts
        // before giving up.
        int retries = 0;
        while (true) {
            try {
                writeUpdatedStats(lcc, td, non_disposable_objectUUID[indexNumber], cmp.getRowCount(), cardinality, asBackgroundTask);
                break;
            } catch (StandardException se) {
                retries++;
                if (se.isLockTimeout() && retries < 3) {
                    trace(2, "lock timeout when writing stats, retrying");
                    sleep(100 * retries);
                } else {
                    // o too many lock timeouts
                    throw se;
                }
            }
        }
    }
    log(asBackgroundTask, td, fmtScanTimes(scanTimes));
}
Also used : StatisticsDescriptor(org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) GroupFetchScanController(org.apache.derby.iapi.store.access.GroupFetchScanController) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) StandardException(org.apache.derby.shared.common.error.StandardException) IndexRowGenerator(org.apache.derby.iapi.sql.dictionary.IndexRowGenerator) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UUID(org.apache.derby.catalog.UUID)

Aggregations

TransactionController (org.apache.derby.iapi.store.access.TransactionController)124 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)47 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)40 StandardException (org.apache.derby.shared.common.error.StandardException)26 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)23 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)20 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)20 UUID (org.apache.derby.catalog.UUID)14 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)13 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)12 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)12 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)12 Properties (java.util.Properties)11 AccessFactory (org.apache.derby.iapi.store.access.AccessFactory)11 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)11 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)10 XATransactionController (org.apache.derby.iapi.store.access.XATransactionController)9 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)8 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)8 ScanController (org.apache.derby.iapi.store.access.ScanController)8