Search in sources :

Example 1 with CacheException

use of javax.cache.CacheException in project pratilipi by Pratilipi.

the class MemcacheGaeImpl method flush.

@Override
public void flush() {
    try {
        Cache cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        cache.clear();
    } catch (CacheException ex) {
        logger.log(Level.SEVERE, "Failed to create cache instance.", ex);
    }
}
Also used : GCacheException(com.google.appengine.api.memcache.stdimpl.GCacheException) CacheException(javax.cache.CacheException) Cache(javax.cache.Cache)

Example 2 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteH2Indexing method queryDistributedSqlFields.

/** {@inheritDoc} */
@Override
public FieldsQueryCursor<List<?>> queryDistributedSqlFields(String schemaName, SqlFieldsQuery qry, boolean keepBinary, GridQueryCancel cancel, @Nullable Integer mainCacheId) {
    final String sqlQry = qry.getSql();
    Connection c = connectionForSchema(schemaName);
    final boolean enforceJoinOrder = qry.isEnforceJoinOrder();
    final boolean distributedJoins = qry.isDistributedJoins();
    final boolean grpByCollocated = qry.isCollocated();
    final DistributedJoinMode distributedJoinMode = distributedJoinMode(qry.isLocal(), distributedJoins);
    GridCacheTwoStepQuery twoStepQry = null;
    List<GridQueryFieldMetadata> meta;
    final H2TwoStepCachedQueryKey cachedQryKey = new H2TwoStepCachedQueryKey(schemaName, sqlQry, grpByCollocated, distributedJoins, enforceJoinOrder, qry.isLocal());
    H2TwoStepCachedQuery cachedQry = twoStepCache.get(cachedQryKey);
    if (cachedQry != null) {
        twoStepQry = cachedQry.query().copy();
        meta = cachedQry.meta();
    } else {
        final UUID locNodeId = ctx.localNodeId();
        // Here we will just parse the statement, no need to optimize it at all.
        H2Utils.setupConnection(c, /*distributedJoins*/
        false, /*enforceJoinOrder*/
        true);
        GridH2QueryContext.set(new GridH2QueryContext(locNodeId, locNodeId, 0, PREPARE).distributedJoinMode(distributedJoinMode));
        PreparedStatement stmt = null;
        Prepared prepared;
        boolean cachesCreated = false;
        try {
            try {
                while (true) {
                    try {
                        // Do not cache this statement because the whole query object will be cached later on.
                        stmt = prepareStatement(c, sqlQry, false);
                        break;
                    } catch (SQLException e) {
                        if (!cachesCreated && (e.getErrorCode() == ErrorCode.SCHEMA_NOT_FOUND_1 || e.getErrorCode() == ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1 || e.getErrorCode() == ErrorCode.INDEX_NOT_FOUND_1)) {
                            try {
                                ctx.cache().createMissingQueryCaches();
                            } catch (IgniteCheckedException ignored) {
                                throw new CacheException("Failed to create missing caches.", e);
                            }
                            cachesCreated = true;
                        } else
                            throw new IgniteSQLException("Failed to parse query: " + sqlQry, IgniteQueryErrorCode.PARSING, e);
                    }
                }
                prepared = GridSqlQueryParser.prepared(stmt);
                if (qry instanceof JdbcSqlFieldsQuery && ((JdbcSqlFieldsQuery) qry).isQuery() != prepared.isQuery())
                    throw new IgniteSQLException("Given statement type does not match that declared by JDBC driver", IgniteQueryErrorCode.STMT_TYPE_MISMATCH);
                if (prepared.isQuery()) {
                    bindParameters(stmt, F.asList(qry.getArgs()));
                    twoStepQry = GridSqlQuerySplitter.split((JdbcPreparedStatement) stmt, qry.getArgs(), grpByCollocated, distributedJoins, enforceJoinOrder, this);
                    assert twoStepQry != null;
                }
            } finally {
                GridH2QueryContext.clearThreadLocal();
            }
            // It is a DML statement if we did not create a twoStepQuery.
            if (twoStepQry == null) {
                if (DmlStatementsProcessor.isDmlStatement(prepared)) {
                    try {
                        return dmlProc.updateSqlFieldsDistributed(schemaName, stmt, qry, cancel);
                    } catch (IgniteCheckedException e) {
                        throw new IgniteSQLException("Failed to execute DML statement [stmt=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
                    }
                }
                if (DdlStatementsProcessor.isDdlStatement(prepared)) {
                    try {
                        return ddlProc.runDdlStatement(sqlQry, stmt);
                    } catch (IgniteCheckedException e) {
                        throw new IgniteSQLException("Failed to execute DDL statement [stmt=" + sqlQry + ']', e);
                    }
                }
            }
            LinkedHashSet<Integer> caches0 = new LinkedHashSet<>();
            assert twoStepQry != null;
            int tblCnt = twoStepQry.tablesCount();
            if (mainCacheId != null)
                caches0.add(mainCacheId);
            if (tblCnt > 0) {
                for (QueryTable tblKey : twoStepQry.tables()) {
                    GridH2Table tbl = dataTable(tblKey);
                    int cacheId = CU.cacheId(tbl.cacheName());
                    caches0.add(cacheId);
                }
            }
            if (caches0.isEmpty())
                twoStepQry.local(true);
            else {
                //Prohibit usage indices with different numbers of segments in same query.
                List<Integer> cacheIds = new ArrayList<>(caches0);
                checkCacheIndexSegmentation(cacheIds);
                twoStepQry.cacheIds(cacheIds);
                twoStepQry.local(qry.isLocal());
            }
            meta = H2Utils.meta(stmt.getMetaData());
        } catch (IgniteCheckedException e) {
            throw new CacheException("Failed to bind parameters: [qry=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
        } catch (SQLException e) {
            throw new IgniteSQLException(e);
        } finally {
            U.close(stmt, log);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Parsed query: `" + sqlQry + "` into two step query: " + twoStepQry);
    twoStepQry.pageSize(qry.getPageSize());
    if (cancel == null)
        cancel = new GridQueryCancel();
    int[] partitions = qry.getPartitions();
    if (partitions == null && twoStepQry.derivedPartitions() != null) {
        try {
            partitions = calculateQueryPartitions(twoStepQry.derivedPartitions(), qry.getArgs());
        } catch (IgniteCheckedException e) {
            throw new CacheException("Failed to calculate derived partitions: [qry=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
        }
    }
    QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(runQueryTwoStep(schemaName, twoStepQry, keepBinary, enforceJoinOrder, qry.getTimeout(), cancel, qry.getArgs(), partitions), cancel);
    cursor.fieldsMeta(meta);
    if (cachedQry == null && !twoStepQry.explain()) {
        cachedQry = new H2TwoStepCachedQuery(meta, twoStepQry.copy());
        twoStepCache.putIfAbsent(cachedQryKey, cachedQry);
    }
    return cursor;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) CacheException(javax.cache.CacheException) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryTable(org.apache.ignite.internal.processors.cache.query.QueryTable) IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) DistributedJoinMode(org.apache.ignite.internal.processors.query.h2.opt.DistributedJoinMode) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) JdbcSqlFieldsQuery(org.apache.ignite.internal.jdbc2.JdbcSqlFieldsQuery)

Example 3 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class GridQueryProcessor method querySqlFieldsNoCache.

/**
     * Query SQL fields without strict dependency on concrete cache.
     *
     * @param qry Query.
     * @param keepBinary Keep binary flag.
     * @return Cursor.
     */
public FieldsQueryCursor<List<?>> querySqlFieldsNoCache(final SqlFieldsQuery qry, final boolean keepBinary) {
    checkxEnabled();
    validateSqlFieldsQuery(qry);
    if (qry.isLocal())
        throw new IgniteException("Local query is not supported without specific cache.");
    if (qry.getSchema() == null)
        qry.setSchema(QueryUtils.DFLT_SCHEMA);
    if (!busyLock.enterBusy())
        throw new IllegalStateException("Failed to execute query (grid is stopping).");
    try {
        IgniteOutClosureX<FieldsQueryCursor<List<?>>> clo = new IgniteOutClosureX<FieldsQueryCursor<List<?>>>() {

            @Override
            public FieldsQueryCursor<List<?>> applyx() throws IgniteCheckedException {
                GridQueryCancel cancel = new GridQueryCancel();
                return idx.queryDistributedSqlFields(qry.getSchema(), qry, keepBinary, cancel, null);
            }
        };
        return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), null, clo, true);
    } catch (IgniteCheckedException e) {
        throw new CacheException(e);
    } finally {
        busyLock.leaveBusy();
    }
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 4 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class CacheConfiguration method processAnnotationsInClass.

/**
     * Process annotations for class.
     *
     * @param key If given class relates to key.
     * @param cls Class.
     * @param type Type descriptor.
     * @param parent Parent in case of embeddable.
     */
private static void processAnnotationsInClass(boolean key, Class<?> cls, TypeDescriptor type, @Nullable ClassProperty parent) {
    if (U.isJdk(cls) || QueryUtils.isGeometryClass(cls)) {
        if (parent == null && !key && QueryUtils.isSqlType(cls)) {
            // We have to index primitive _val.
            String idxName = cls.getSimpleName() + "_" + QueryUtils.VAL_FIELD_NAME + "_idx";
            type.addIndex(idxName, QueryUtils.isGeometryClass(cls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            type.addFieldToIndex(idxName, QueryUtils.VAL_FIELD_NAME, 0, false);
        }
        return;
    }
    if (parent != null && parent.knowsClass(cls))
        throw new CacheException("Recursive reference found in type: " + cls.getName());
    if (parent == null) {
        // Check class annotation at top level only.
        QueryTextField txtAnnCls = cls.getAnnotation(QueryTextField.class);
        if (txtAnnCls != null)
            type.valueTextIndex(true);
        QueryGroupIndex grpIdx = cls.getAnnotation(QueryGroupIndex.class);
        if (grpIdx != null)
            type.addIndex(grpIdx.name(), QueryIndexType.SORTED);
        QueryGroupIndex.List grpIdxList = cls.getAnnotation(QueryGroupIndex.List.class);
        if (grpIdxList != null && !F.isEmpty(grpIdxList.value())) {
            for (QueryGroupIndex idx : grpIdxList.value()) type.addIndex(idx.name(), QueryIndexType.SORTED);
        }
    }
    for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            QuerySqlField sqlAnn = field.getAnnotation(QuerySqlField.class);
            QueryTextField txtAnn = field.getAnnotation(QueryTextField.class);
            if (sqlAnn != null || txtAnn != null) {
                ClassProperty prop = new ClassProperty(field);
                prop.parent(parent);
                // Add parent property before its possible nested properties so that
                // resulting parent column comes before columns corresponding to those
                // nested properties in the resulting table - that way nested
                // properties override will happen properly (first parent, then children).
                type.addProperty(prop, key, true);
                processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type);
            }
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) Field(java.lang.reflect.Field) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QueryGroupIndex(org.apache.ignite.cache.query.annotations.QueryGroupIndex) CacheException(javax.cache.CacheException)

Example 5 with CacheException

use of javax.cache.CacheException in project caffeine by ben-manes.

the class JmxRegistration method getObjectName.

/** Returns the object name of the management bean. */
private static ObjectName getObjectName(Cache<?, ?> cache, MBeanType type) {
    String cacheManagerName = sanitize(cache.getCacheManager().getURI().toString());
    String cacheName = sanitize(cache.getName());
    try {
        String name = String.format("javax.cache:type=Cache%s,CacheManager=%s,Cache=%s", type, cacheManagerName, cacheName);
        return new ObjectName(name);
    } catch (MalformedObjectNameException e) {
        String msg = String.format("Illegal ObjectName for cacheManager=[%s], cache=[%s]", cacheManagerName, cacheName);
        throw new CacheException(msg, e);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CacheException(javax.cache.CacheException) ObjectName(javax.management.ObjectName)

Aggregations

CacheException (javax.cache.CacheException)264 Ignite (org.apache.ignite.Ignite)84 Test (org.junit.Test)57 IgniteException (org.apache.ignite.IgniteException)55 Transaction (org.apache.ignite.transactions.Transaction)53 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)52 ArrayList (java.util.ArrayList)45 IgniteCache (org.apache.ignite.IgniteCache)39 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)37 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)34 List (java.util.List)32 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)29 CountDownLatch (java.util.concurrent.CountDownLatch)26 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)25 EntryProcessorException (javax.cache.processor.EntryProcessorException)25 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)24 Map (java.util.Map)21 ClusterNode (org.apache.ignite.cluster.ClusterNode)21 TransactionRollbackException (org.apache.ignite.transactions.TransactionRollbackException)19