Search in sources :

Example 1 with AccessType

use of org.hibernate.cache.spi.access.AccessType in project hibernate-orm by hibernate.

the class AbstractRegionAccessStrategyTest method expectPutWithValue.

protected CountDownLatch expectPutWithValue(Predicate<Object> valuePredicate) {
    if (!isUsingInvalidation() && accessType != AccessType.NONSTRICT_READ_WRITE) {
        CountDownLatch latch = new CountDownLatch(1);
        ExpectingInterceptor.get(remoteRegion.getCache()).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand && valuePredicate.test(((PutKeyValueCommand) cmd).getValue())).countDown(latch);
        cleanup.add(() -> ExpectingInterceptor.cleanup(remoteRegion.getCache()));
        return latch;
    } else {
        return new CountDownLatch(0);
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) BatchModeJtaPlatform(org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform) Cache(org.infinispan.Cache) Transaction(org.hibernate.Transaction) AdvancedCache(org.infinispan.AdvancedCache) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CacheDataDescriptionImpl(org.hibernate.cache.internal.CacheDataDescriptionImpl) TestingUtil(org.infinispan.test.TestingUtil) AccessType(org.hibernate.cache.spi.access.AccessType) Predicate(java.util.function.Predicate) TombstoneUpdate(org.hibernate.cache.infinispan.util.TombstoneUpdate) JdbcResourceTransactionAccess(org.hibernate.resource.transaction.backend.jdbc.spi.JdbcResourceTransactionAccess) TestSynchronization(org.hibernate.test.cache.infinispan.util.TestSynchronization) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SoftLock(org.hibernate.cache.spi.access.SoftLock) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) AfterClassOnce(org.hibernate.testing.AfterClassOnce) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Mockito.mock(org.mockito.Mockito.mock) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) TestResourceTracker(org.infinispan.test.fwk.TestResourceTracker) Logger(org.jboss.logging.Logger) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) RegionAccessStrategy(org.hibernate.cache.spi.access.RegionAccessStrategy) SQLException(java.sql.SQLException) TestTimeService(org.hibernate.test.cache.infinispan.util.TestTimeService) RollbackException(javax.transaction.RollbackException) TransactionCoordinatorOwner(org.hibernate.resource.transaction.spi.TransactionCoordinatorOwner) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) ComparableComparator(org.hibernate.internal.util.compare.ComparableComparator) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) JdbcResourceTransactionMock(org.hibernate.test.cache.infinispan.util.JdbcResourceTransactionMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Mockito.when(org.mockito.Mockito.when) BaseRegion(org.hibernate.cache.infinispan.impl.BaseRegion) ServiceRegistry(org.hibernate.service.ServiceRegistry) TimeUnit(java.util.concurrent.TimeUnit) CacheDataDescription(org.hibernate.cache.spi.CacheDataDescription) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Assert.assertNull(org.junit.Assert.assertNull) SystemException(javax.transaction.SystemException) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) BeforeClassOnce(org.hibernate.testing.BeforeClassOnce) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 2 with AccessType

use of org.hibernate.cache.spi.access.AccessType in project hibernate-orm by hibernate.

the class AbstractFunctionalTest method expectPutWithValue.

protected CountDownLatch expectPutWithValue(AdvancedCache cache, Predicate<Object> valuePredicate, int numUpdates) {
    if (!cacheMode.isInvalidation() && accessType != AccessType.NONSTRICT_READ_WRITE) {
        CountDownLatch latch = new CountDownLatch(numUpdates);
        ExpectingInterceptor.get(cache).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand && valuePredicate.test(((PutKeyValueCommand) cmd).getValue())).countDown(latch);
        cleanup.add(() -> ExpectingInterceptor.cleanup(cache));
        return latch;
    } else {
        return new CountDownLatch(0);
    }
}
Also used : Property(org.hibernate.mapping.Property) AvailableSettings(org.hibernate.cfg.AvailableSettings) TxUtil(org.hibernate.test.cache.infinispan.util.TxUtil) RootClass(org.hibernate.mapping.RootClass) RunWith(org.junit.runner.RunWith) Session(org.hibernate.Session) JtaPlatformImpl(org.hibernate.test.cache.infinispan.tm.JtaPlatformImpl) Metadata(org.hibernate.boot.Metadata) ArrayList(java.util.ArrayList) XaConnectionProvider(org.hibernate.test.cache.infinispan.tm.XaConnectionProvider) AdvancedCache(org.infinispan.AdvancedCache) RegionFactory(org.hibernate.cache.spi.RegionFactory) PersistentClass(org.hibernate.mapping.PersistentClass) Map(java.util.Map) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) After(org.junit.After) JtaTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) AccessType(org.hibernate.cache.spi.access.AccessType) Environment(org.hibernate.cfg.Environment) ClassRule(org.junit.ClassRule) Parameterized(org.junit.runners.Parameterized) SimpleCacheKeysFactory(org.hibernate.cache.internal.SimpleCacheKeysFactory) CustomParameterized(org.hibernate.testing.junit4.CustomParameterized) BaseNonConfigCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase) Predicate(java.util.function.Predicate) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) TombstoneUpdate(org.hibernate.cache.infinispan.util.TombstoneUpdate) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheMode(org.infinispan.configuration.cache.CacheMode) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) BeforeClassOnce(org.hibernate.testing.BeforeClassOnce) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) InfinispanTestingSetup(org.hibernate.test.cache.infinispan.util.InfinispanTestingSetup) CountDownLatch(java.util.concurrent.CountDownLatch) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 3 with AccessType

use of org.hibernate.cache.spi.access.AccessType in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method createCustomer.

private IdContainer createCustomer(SessionFactory sessionFactory) throws Exception {
    log.debug("CREATE CUSTOMER");
    Customer customer = new Customer();
    customer.setName("JBoss");
    Set<Contact> contacts = new HashSet<Contact>();
    Contact kabir = new Contact();
    kabir.setCustomer(customer);
    kabir.setName("Kabir");
    kabir.setTlf("1111");
    contacts.add(kabir);
    Contact bill = new Contact();
    bill.setCustomer(customer);
    bill.setName("Bill");
    bill.setTlf("2222");
    contacts.add(bill);
    customer.setContacts(contacts);
    ArrayList<Runnable> cleanup = new ArrayList<>();
    CountDownLatch customerLatch = new CountDownLatch(1);
    CountDownLatch collectionLatch = new CountDownLatch(1);
    CountDownLatch contactsLatch = new CountDownLatch(2);
    if (cacheMode.isInvalidation()) {
        cleanup.add(mockValidator(remoteCustomerCache, customerLatch));
        cleanup.add(mockValidator(remoteCollectionCache, collectionLatch));
        cleanup.add(mockValidator(remoteContactCache, contactsLatch));
    } else if (accessType == AccessType.NONSTRICT_READ_WRITE) {
        // ATM nonstrict mode has sync after-invalidation update
        Stream.of(customerLatch, collectionLatch, contactsLatch, contactsLatch).forEach(l -> l.countDown());
    } else {
        ExpectingInterceptor.get(remoteCustomerCache).when(this::isFutureUpdate).countDown(collectionLatch);
        ExpectingInterceptor.get(remoteCollectionCache).when(this::isFutureUpdate).countDown(customerLatch);
        ExpectingInterceptor.get(remoteContactCache).when(this::isFutureUpdate).countDown(contactsLatch);
        cleanup.add(() -> ExpectingInterceptor.cleanup(remoteCustomerCache, remoteCollectionCache, remoteContactCache));
    }
    withTxSession(sessionFactory, session -> session.save(customer));
    assertTrue(customerLatch.await(2, TimeUnit.SECONDS));
    assertTrue(collectionLatch.await(2, TimeUnit.SECONDS));
    assertTrue(contactsLatch.await(2, TimeUnit.SECONDS));
    cleanup.forEach(Runnable::run);
    IdContainer ids = new IdContainer();
    ids.customerId = customer.getId();
    Set contactIds = new HashSet();
    contactIds.add(kabir.getId());
    contactIds.add(bill.getId());
    ids.contactIds = contactIds;
    log.debug("CREATE CUSTOMER -  END");
    return ids;
}
Also used : ConcurrentSet(org.jboss.util.collection.ConcurrentSet) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) CacheEntryVisitedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent) TimeoutException(java.util.concurrent.TimeoutException) Session(org.hibernate.Session) Cache(org.infinispan.Cache) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) TestForIssue(org.hibernate.testing.TestForIssue) InvocationContext(org.infinispan.context.InvocationContext) CacheEntryVisited(org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited) AdvancedCache(org.infinispan.AdvancedCache) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) AccessType(org.hibernate.cache.spi.access.AccessType) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) Listener(org.infinispan.notifications.Listener) Iterator(java.util.Iterator) SessionFactory(org.hibernate.SessionFactory) Util(org.infinispan.commons.util.Util) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) Phaser(java.util.concurrent.Phaser) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) Assert.assertEquals(org.junit.Assert.assertEquals) ConcurrentSet(org.jboss.util.collection.ConcurrentSet) HashSet(java.util.HashSet) Set(java.util.Set) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 4 with AccessType

use of org.hibernate.cache.spi.access.AccessType in project hibernate-orm by hibernate.

the class CacheImpl method determineEntityRegionAccessStrategy.

@Override
public EntityRegionAccessStrategy determineEntityRegionAccessStrategy(PersistentClass model) {
    final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
    EntityRegionAccessStrategy accessStrategy = entityRegionAccessStrategyMap.get(cacheRegionName);
    if (accessStrategy == null && settings.isSecondLevelCacheEnabled()) {
        final AccessType accessType = AccessType.fromExternalName(model.getCacheConcurrencyStrategy());
        if (accessType != null) {
            LOG.tracef("Building shared cache region for entity data [%s]", model.getEntityName());
            EntityRegion entityRegion = regionFactory.buildEntityRegion(cacheRegionName, sessionFactory.getProperties(), CacheDataDescriptionImpl.decode(model));
            accessStrategy = entityRegion.buildAccessStrategy(accessType);
            entityRegionAccessStrategyMap.put(cacheRegionName, accessStrategy);
        }
    }
    return accessStrategy;
}
Also used : EntityRegion(org.hibernate.cache.spi.EntityRegion) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) AccessType(org.hibernate.cache.spi.access.AccessType)

Example 5 with AccessType

use of org.hibernate.cache.spi.access.AccessType in project hibernate-orm by hibernate.

the class CorrectnessTestCase method buildMetadata.

private Metadata buildMetadata(StandardServiceRegistry registry) {
    MetadataSources metadataSources = new MetadataSources(registry);
    for (Class entityClass : getAnnotatedClasses()) {
        metadataSources.addAnnotatedClass(entityClass);
    }
    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (!entityBinding.isInherited()) {
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(accessType.getExternalName());
        }
    }
    // Collections don't have integrated version, these piggyback on parent's owner version (for DB).
    // However, this version number isn't extractable and is not passed to cache methods.
    AccessType collectionAccessType = accessType == AccessType.NONSTRICT_READ_WRITE ? AccessType.READ_WRITE : accessType;
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy(collectionAccessType.getExternalName());
    }
    return metadata;
}
Also used : RootClass(org.hibernate.mapping.RootClass) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Collection(org.hibernate.mapping.Collection) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) AccessType(org.hibernate.cache.spi.access.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

AccessType (org.hibernate.cache.spi.access.AccessType)7 ArrayList (java.util.ArrayList)4 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Session (org.hibernate.Session)3 FutureUpdate (org.hibernate.cache.infinispan.util.FutureUpdate)3 ExpectingInterceptor (org.hibernate.test.cache.infinispan.util.ExpectingInterceptor)3 TestInfinispanRegionFactory (org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory)3 AdvancedCache (org.infinispan.AdvancedCache)3 PutKeyValueCommand (org.infinispan.commands.write.PutKeyValueCommand)3 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 Predicate (java.util.function.Predicate)2 PutFromLoadValidator (org.hibernate.cache.infinispan.access.PutFromLoadValidator)2 TombstoneUpdate (org.hibernate.cache.infinispan.util.TombstoneUpdate)2 JdbcResourceLocalTransactionCoordinatorBuilderImpl (org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl)2 BeforeClassOnce (org.hibernate.testing.BeforeClassOnce)2 Cache (org.infinispan.Cache)2 After (org.junit.After)2 Assert.assertEquals (org.junit.Assert.assertEquals)2