All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ignite.cache.hibernate.HibernateAccessStrategyAdapter Maven / Gradle / Ivy

Go to download

Java-based middleware for in-memory processing of big data in a distributed environment.

There is a newer version: 1.2.0-incubating
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ignite.cache.hibernate;

import org.apache.ignite.*;
import org.apache.ignite.cache.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.internal.processors.cache.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.resources.*;
import org.hibernate.cache.*;
import org.hibernate.cache.spi.access.*;
import org.jetbrains.annotations.*;

import java.io.*;

/**
 * Common interface used to implement Hibernate L2 cache access strategies ({@link RegionAccessStrategy},
 * {@link EntityRegionAccessStrategy} and {@link CollectionRegionAccessStrategy}).
 * 

* The expected sequences of steps related to various CRUD operations executed by Hibernate are: *

* Insert: *

    *
  • Start DB transaction.
  • *
  • Execute database insert.
  • *
  • Call {@link HibernateAccessStrategyAdapter#insert}.
  • *
  • Commit DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#afterInsert}.
  • *
* In case if some step fails and DB transaction is rolled back then * {@link HibernateAccessStrategyAdapter#afterInsert} is not called. *

* Update: *

    *
  • Start DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#lock}.
  • *
  • Execute database update.
  • *
  • Call {@link HibernateAccessStrategyAdapter#update}.
  • *
  • Commit DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#afterUpdate}.
  • *
* In case if {@link HibernateAccessStrategyAdapter#lock} was called, but some other step fails and DB * transaction is rolled back then {@link HibernateAccessStrategyAdapter#unlock} is called for all locked keys. *

* Delete: *

    *
  • Start DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#lock} for removing key.
  • *
  • Execute database delete.
  • *
  • Call {@link HibernateAccessStrategyAdapter#remove}.
  • *
  • Commit DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#unlock}.
  • *
* In case if {@link HibernateAccessStrategyAdapter#lock} was called, but some other step fails and DB * transaction is rolled back then {@link HibernateAccessStrategyAdapter#unlock} is called for all locked keys. *

* In case if custom SQL update query is executed Hibernate clears entire cache region, * for this case operations sequence is: *

    *
  • Start DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#lockRegion}.
  • *
  • Execute database query.
  • *
  • Call {@link HibernateAccessStrategyAdapter#removeAll}.
  • *
  • Commit DB transaction.
  • *
  • Call {@link HibernateAccessStrategyAdapter#unlockRegion}.
  • *
*/ public abstract class HibernateAccessStrategyAdapter { /** */ protected final GridCache cache; /** Grid. */ protected final Ignite ignite; /** */ protected final IgniteLogger log; /** * @param ignite Grid. * @param cache Cache. */ protected HibernateAccessStrategyAdapter(Ignite ignite, GridCache cache) { this.cache = cache; this.ignite = ignite; log = ignite.log(); } /** * Gets value from cache. Used by {@link RegionAccessStrategy#get}. * * @param key Key. * @return Cached value. * @throws CacheException If failed. */ @Nullable protected Object get(Object key) throws CacheException { try { return cache.get(key); } catch (IgniteCheckedException e) { throw new CacheException(e); } } /** * Puts in cache value loaded from the database. Used by {@link RegionAccessStrategy#putFromLoad}. * * @param key Key. * @param val Value. * @param minimalPutOverride MinimalPut flag * @throws CacheException If failed. */ protected void putFromLoad(Object key, Object val, boolean minimalPutOverride) throws CacheException { putFromLoad(key, val); } /** * Puts in cache value loaded from the database. Used by {@link RegionAccessStrategy#putFromLoad}. * * @param key Key. * @param val Value. * @throws CacheException If failed. */ protected void putFromLoad(Object key, Object val) throws CacheException { try { cache.putx(key, val); } catch (IgniteCheckedException e) { throw new CacheException(e); } } /** * Called during database transaction execution before Hibernate attempts to update or remove given key. * Used by {@link RegionAccessStrategy#lockItem}. * * @param key Key. * @return Lock representation or {@code null}. * @throws CacheException If failed. */ @Nullable protected abstract SoftLock lock(Object key) throws CacheException; /** * Called after Hibernate failed to update or successfully removed given key. * Used by {@link RegionAccessStrategy#unlockItem}. * * @param key Key. * @param lock The lock previously obtained from {@link #lock} * @throws CacheException If failed. */ protected abstract void unlock(Object key, SoftLock lock) throws CacheException; /** * Called after Hibernate updated object in the database but before transaction completed. * Used by {@link EntityRegionAccessStrategy#update} and {@link NaturalIdRegionAccessStrategy#update}. * * @param key Key. * @param val Value. * @return {@code True} if operation updated cache. * @throws CacheException If failed. */ protected abstract boolean update(Object key, Object val) throws CacheException; /** * Called after Hibernate updated object in the database and transaction successfully completed. * Used by {@link EntityRegionAccessStrategy#afterUpdate} and {@link NaturalIdRegionAccessStrategy#afterUpdate}. * * @param key Key. * @param val Value. * @param lock The lock previously obtained from {@link #lock} * @return {@code True} if operation updated cache. * @throws CacheException If failed. */ protected abstract boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException; /** * Called after Hibernate inserted object in the database but before transaction completed. * Used by {@link EntityRegionAccessStrategy#insert} and {@link NaturalIdRegionAccessStrategy#insert}. * * @param key Key. * @param val Value. * @return {@code True} if operation updated cache. * @throws CacheException If failed. */ protected abstract boolean insert(Object key, Object val) throws CacheException; /** * Called after Hibernate inserted object in the database and transaction successfully completed. * Used by {@link EntityRegionAccessStrategy#afterInsert} and {@link NaturalIdRegionAccessStrategy#afterInsert}. * * @param key Key. * @param val Value. * @return {@code True} if operation updated cache. * @throws CacheException If failed. */ protected abstract boolean afterInsert(Object key, Object val) throws CacheException; /** * Called after Hibernate removed object from database but before transaction completed. * Used by {@link RegionAccessStrategy#remove}. * * @param key Key, * @throws CacheException If failed. */ protected abstract void remove(Object key) throws CacheException; /** * Called to remove object from cache without regard to transaction. * Used by {@link RegionAccessStrategy#evict}. * * @param key Key. * @throws CacheException If failed. */ protected void evict(Object key) throws CacheException { evict(ignite, cache, key); } /** * Called to remove all data from cache without regard to transaction. * Used by {@link RegionAccessStrategy#evictAll}. * * @throws CacheException If failed. */ protected void evictAll() throws CacheException { evictAll(cache); } /** * Called during database transaction execution before Hibernate executed * update operation which should invalidate entire cache region. * Used by {@link RegionAccessStrategy#lockRegion}. * * @throws CacheException If failed. * @return Lock representation or {@code null}. */ @Nullable protected SoftLock lockRegion() throws CacheException { return null; } /** * Called after transaction clearing entire cache region completed. * Used by {@link RegionAccessStrategy#unlockRegion}. * * @param lock The lock previously obtained from {@link #lockRegion} * @throws CacheException If failed. */ protected void unlockRegion(SoftLock lock) throws CacheException { // No-op. } /** * Called during database transaction execution to clear entire cache region after * Hibernate executed database update, but before transaction completed. * Used by {@link RegionAccessStrategy#removeAll}. * * @throws CacheException If failed. */ protected final void removeAll() throws CacheException { evictAll(); } /** * Called to remove object from cache without regard to transaction. * * @param ignite Grid. * @param cache Cache. * @param key Key. * @throws CacheException If failed. */ static void evict(Ignite ignite, CacheProjection cache, Object key) throws CacheException { try { ignite.compute(cache.gridProjection()).call(new ClearKeyCallable(key, cache.name())); } catch (IgniteException e) { throw new CacheException(e); } } /** * Called to remove all data from cache without regard to transaction. * * @param cache Cache. * @throws CacheException If failed. */ static void evictAll(CacheProjection cache) throws CacheException { try { cache.clear(); } catch (IgniteCheckedException e) { throw new CacheException(e); } } /** * Callable invalidates given key. */ private static class ClearKeyCallable implements IgniteCallable, Externalizable { /** */ private static final long serialVersionUID = 0L; /** */ @IgniteInstanceResource private Ignite ignite; /** */ private Object key; /** */ private String cacheName; /** * Empty constructor required by {@link Externalizable}. */ public ClearKeyCallable() { // No-op. } /** * @param key Key to clear. * @param cacheName Cache name. */ private ClearKeyCallable(Object key, String cacheName) { this.key = key; this.cacheName = cacheName; } /** {@inheritDoc} */ @Override public Void call() throws IgniteCheckedException { GridCache cache = ((IgniteKernal)ignite).cache(cacheName); assert cache != null; cache.clearLocally(key); return null; } /** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(key); U.writeString(out, cacheName); } /** {@inheritDoc} */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { key = in.readObject(); cacheName = U.readString(in); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy