Search in sources :

Example 1 with CacheManager

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

the class DataDictionaryImpl method addTableDescriptorToOtherCache.

/**
 * Add a table descriptor to the "other" cache. The other cache is
 * determined by the type of the object c.
 *
 * @param td	TableDescriptor to add to the other cache.
 * @param c		Cacheable Object which lets us figure out the other cache.
 *
 * @exception	StandardException
 */
public void addTableDescriptorToOtherCache(TableDescriptor td, Cacheable c) throws StandardException {
    // get the other cache. if the entry we are setting in the cache is of
    // type oidtdcacheable then use the nametdcache
    CacheManager otherCache = (c instanceof OIDTDCacheable) ? nameTdCache : OIDTdCache;
    Object key;
    TDCacheable otherCacheEntry = null;
    if (otherCache == nameTdCache)
        key = new TableKey(td.getSchemaDescriptor().getUUID(), td.getName());
    else
        key = td.getUUID();
    try {
        // insert the entry into the the other cache.
        otherCacheEntry = (TDCacheable) otherCache.create(key, td);
    } catch (StandardException se) {
        // otherwise throw the error.
        if (!(se.getMessageId().equals(SQLState.OBJECT_EXISTS_IN_CACHE)))
            throw se;
    } finally {
        if (otherCacheEntry != null)
            otherCache.release(otherCacheEntry);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) CacheManager(org.apache.derby.iapi.services.cache.CacheManager)

Example 2 with CacheManager

use of org.apache.derby.iapi.services.cache.CacheManager 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 3 with CacheManager

use of org.apache.derby.iapi.services.cache.CacheManager 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 4 with CacheManager

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

the class SystemProcedures method SYSCS_EMPTY_STATEMENT_CACHE.

/**
 * Empty as much of the cache as possible. It is not guaranteed
 * that the cache is empty after this call, as statements may be kept
 * by currently executing queries, activations that are about to be garbage
 * collected.
 */
public static void SYSCS_EMPTY_STATEMENT_CACHE() throws SQLException {
    try {
        // make sure that application code doesn't bypass security checks
        // by calling this public entry point
        SecurityUtil.authorize(Securable.EMPTY_STATEMENT_CACHE);
    } catch (StandardException se) {
        throw PublicAPI.wrapStandardException(se);
    }
    LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
    CacheManager statementCache = lcc.getLanguageConnectionFactory().getStatementCache();
    if (statementCache != null) {
        statementCache.ageOut();
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) CacheManager(org.apache.derby.iapi.services.cache.CacheManager)

Aggregations

CacheManager (org.apache.derby.iapi.services.cache.CacheManager)4 Cacheable (org.apache.derby.iapi.services.cache.Cacheable)2 StandardException (org.apache.derby.shared.common.error.StandardException)2 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 GenericPreparedStatement (org.apache.derby.impl.sql.GenericPreparedStatement)1