Search in sources :

Example 6 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class GenericLanguageConnectionContext method removeStatement.

/**
 *  This method will remove a statement from the  statement cache.
 *  It should only be called if there is an exception preparing
 *  the statement. The caller must have set the flag
 *  {@code preparedStmt.compilingStatement} in the {@code GenericStatement}
 *  before calling this method in order to prevent race conditions when
 *  calling {@link CacheManager#remove(Cacheable)}.
 *
 *  @param statement Statement to remove
 *  @exception StandardException thrown if lookup goes wrong.
 */
public void removeStatement(GenericStatement statement) throws StandardException {
    CacheManager statementCache = getLanguageConnectionFactory().getStatementCache();
    if (statementCache == null)
        return;
    Cacheable cachedItem = statementCache.findCached(statement);
    // No need to do anything if the statement is already removed
    if (cachedItem != null) {
        CachedStatement cs = (CachedStatement) cachedItem;
        if (statement.getPreparedStatement() != cs.getPreparedStatement()) {
            // DERBY-3786: Someone else has removed the statement from
            // the cache, probably because of the same error that brought
            // us here. In addition, someone else has recreated the
            // statement. Since the recreated statement is not the same
            // object as the one we are working on, we don't have the
            // proper guarding (through the synchronized flag
            // GenericStatement.preparedStmt.compilingStatement) to ensure
            // that we're the only ones calling CacheManager.remove() on
            // this statement. Therefore, just release the statement here
            // so that we don't get in the way for the other thread that
            // is trying to compile the same query.
            statementCache.release(cachedItem);
        } else {
            // The statement object that we were trying to compile is still
            // in the cache. Since the compilation failed, remove it.
            statementCache.remove(cachedItem);
        }
    }
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable) CacheManager(org.apache.derby.iapi.services.cache.CacheManager)

Example 7 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class GenericLanguageConnectionContext method lookupStatement.

/**
 * See if a given statement has already been compiled for this user, and
 * if so use its prepared statement. Returns null if not found.
 *
 * @exception StandardException thrown if lookup goes wrong.
 * @return the prepared statement for the given string, null
 *  if none was found.
 */
public PreparedStatement lookupStatement(GenericStatement statement) throws StandardException {
    CacheManager statementCache = getLanguageConnectionFactory().getStatementCache();
    if (statementCache == null)
        return null;
    // statement caching disable when in DDL mode
    if (dataDictionaryInWriteMode()) {
        return null;
    }
    Cacheable cachedItem = statementCache.find(statement);
    CachedStatement cs = (CachedStatement) cachedItem;
    GenericPreparedStatement ps = cs.getPreparedStatement();
    synchronized (ps) {
        if (ps.upToDate()) {
            GeneratedClass ac = ps.getActivationClass();
            // Check to see if the statement was prepared before some change
            // in the class loading set. If this is the case then force it to be invalid
            int currentClasses = getLanguageConnectionFactory().getClassFactory().getClassLoaderVersion();
            if (ac.getClassLoaderVersion() != currentClasses) {
                ps.makeInvalid(DependencyManager.INTERNAL_RECOMPILE_REQUEST, this);
            }
        // note that the PreparedStatement is not kept in the cache. This is because
        // having items kept in the cache that ultimately are held onto by
        // user code is impossible to manage. E.g. an open ResultSet would hold onto
        // a PreparedStatement (through its activation) and the user can allow
        // this object to be garbage collected. Pushing a context stack is impossible
        // in garbage collection as it may deadlock with the open connection and
        // the context manager assumes a singel current thread per context stack
        }
    }
    statementCache.release(cachedItem);
    return ps;
}
Also used : GeneratedClass(org.apache.derby.iapi.services.loader.GeneratedClass) Cacheable(org.apache.derby.iapi.services.cache.Cacheable) CacheManager(org.apache.derby.iapi.services.cache.CacheManager) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement)

Example 8 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class BaseDataFileFactory method removeDroppedContainerFileStubs.

/**
 * Delete the stub files that are not required for recovery. A stub file
 * is not required to be around if the recovery is not going to see
 * any log record that belongs to that container. Since the stub files
 * are created as a post commit operation, they are not necessary during
 * undo operation of the recovery.
 *
 * To remove a stub file we have to be sure that it was created before the
 * redoLWM in the check point record. We can be sure that the stub is not
 * required if the log instant when it was created is less than the redoLWM.
 */
public void removeDroppedContainerFileStubs(LogInstant redoLWM) throws StandardException {
    if (droppedTableStubInfo != null) {
        synchronized (droppedTableStubInfo) {
            for (Enumeration<LogInstant> e = droppedTableStubInfo.keys(); e.hasMoreElements(); ) {
                LogInstant logInstant = e.nextElement();
                if (logInstant.lessThan(redoLWM)) {
                    Object[] removeInfo = droppedTableStubInfo.get(logInstant);
                    Object identity = removeInfo[1];
                    // delete the entry in the container cache.
                    Cacheable ccentry = containerCache.findCached(identity);
                    if (ccentry != null)
                        containerCache.remove(ccentry);
                    // delete the stub we don't require it during recovery
                    synchronized (this) {
                        actionFile = (StorageFile) removeInfo[0];
                        actionCode = DELETE_IF_EXISTS_ACTION;
                        try {
                            if (AccessController.doPrivileged(this) != null) {
                                // if we successfuly delete the file remove
                                // it from the hash table.
                                droppedTableStubInfo.remove(logInstant);
                            }
                        } catch (PrivilegedActionException pae) {
                        // DELETE_IF_EXISTS does not throw an exception
                        }
                    }
                }
            }
        }
    }
}
Also used : LogInstant(org.apache.derby.iapi.store.raw.log.LogInstant) Cacheable(org.apache.derby.iapi.services.cache.Cacheable) PrivilegedActionException(java.security.PrivilegedActionException)

Example 9 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method create.

/**
 * Create an object in the cache. The object is kept until
 * <code>release()</code> is called.
 *
 * @param key identity of the object to create
 * @param createParameter parameters passed to
 * <code>Cacheable.createIdentity()</code>
 * @return a reference to the cached object, or <code>null</code> if the
 * object cannot be created
 * @exception StandardException if the object is already in the cache, or
 * if some other error occurs
 * @see Cacheable#createIdentity(Object,Object)
 */
public Cacheable create(Object key, Object createParameter) throws StandardException {
    if (stopped) {
        return null;
    }
    CacheEntry entry = new CacheEntry();
    entry.lock();
    if (cache.putIfAbsent(key, entry) != null) {
        // We can't create the object if it's already in the cache.
        throw StandardException.newException(SQLState.OBJECT_EXISTS_IN_CACHE, name, key);
    }
    Cacheable item;
    try {
        item = insertIntoFreeSlot(key, entry);
    } finally {
        entry.unlock();
    }
    // Create the identity without holding the lock on the entry.
    // Otherwise, we may run into a deadlock if the user code in
    // createIdentity() re-enters the buffer manager.
    Cacheable itemWithIdentity = null;
    try {
        itemWithIdentity = item.createIdentity(key, createParameter);
    } finally {
        // Always invoke settingIdentityComplete(), also on error,
        // otherwise other threads may wait forever. If createIdentity()
        // fails, itemWithIdentity is going to be null.
        settingIdentityComplete(key, entry, itemWithIdentity);
    }
    return itemWithIdentity;
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Example 10 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method findCached.

/**
 * Find an object in the cache. If it is not present, return
 * <code>null</code>. The returned object is kept until
 * <code>release()</code> is called.
 *
 * @param key identity of the object to find
 * @return the cached object, or <code>null</code> if it's not in the cache
 */
public Cacheable findCached(Object key) throws StandardException {
    if (stopped) {
        return null;
    }
    // Use get() instead of getEntry() so that we don't insert an empty
    // entry if the requested object isn't there.
    CacheEntry entry = cache.get(key);
    if (entry == null) {
        // No such object was found in the cache.
        countMiss();
        return null;
    }
    // Lock the entry, but wait until its identity has been set.
    entry.lock();
    try {
        // If the identity of the cacheable is being set, we need to wait
        // for it to complete so that we don't return a cacheable that
        // isn't fully initialized.
        entry.waitUntilIdentityIsSet();
        // Return the cacheable. If the entry was removed right before we
        // locked it, getCacheable() returns null and so should we do.
        Cacheable item = entry.getCacheable();
        if (item != null) {
            countHit();
            entry.keep(true);
        } else {
            countMiss();
        }
        return item;
    } finally {
        entry.unlock();
    }
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Aggregations

Cacheable (org.apache.derby.iapi.services.cache.Cacheable)14 CacheManager (org.apache.derby.iapi.services.cache.CacheManager)2 PrivilegedActionException (java.security.PrivilegedActionException)1 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)1 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)1 GenericPreparedStatement (org.apache.derby.impl.sql.GenericPreparedStatement)1 StandardException (org.apache.derby.shared.common.error.StandardException)1