Java Code Examples for org.springframework.transaction.support.TransactionSynchronizationManager#setCurrentTransactionReadOnly()

The following examples show how to use org.springframework.transaction.support.TransactionSynchronizationManager#setCurrentTransactionReadOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachedRoleHierarchyImplTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testGetReachableGrantedAuthoritiesUsingCache() {
  TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

  GrantedAuthority managerAuthority = new SimpleGrantedAuthority("ROLE_MANAGER");
  GrantedAuthority editorAuthority = new SimpleGrantedAuthority("ROLE_EDITOR");
  GrantedAuthority viewerAuthority = new SimpleGrantedAuthority("ROLE_VIEWER");
  ImmutableMap<GrantedAuthority, ImmutableSet<GrantedAuthority>> authorityInclusions =
      ImmutableMap.<GrantedAuthority, ImmutableSet<GrantedAuthority>>builder()
          .put(managerAuthority, ImmutableSet.of(editorAuthority))
          .put(editorAuthority, ImmutableSet.of(viewerAuthority))
          .put(viewerAuthority, ImmutableSet.of())
          .build();
  when(dataserviceRoleHierarchy.getAllGrantedAuthorityInclusions())
      .thenReturn(authorityInclusions);
  assertEquals(
      ImmutableSet.of(managerAuthority, editorAuthority, viewerAuthority),
      cachedRoleHierarchyImpl.getReachableGrantedAuthorities(singletonList(managerAuthority)));
}
 
Example 2
Source File: CachedRoleHierarchyImplTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testGetReachableGrantedAuthoritiesUsingCacheMultiple() {
  TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

  GrantedAuthority managerAuthority = new SimpleGrantedAuthority("ROLE_MANAGER");
  GrantedAuthority editorAuthority = new SimpleGrantedAuthority("ROLE_EDITOR");
  GrantedAuthority viewerAuthority = new SimpleGrantedAuthority("ROLE_VIEWER");
  ImmutableMap<GrantedAuthority, ImmutableSet<GrantedAuthority>> authorityInclusions =
      ImmutableMap.<GrantedAuthority, ImmutableSet<GrantedAuthority>>builder()
          .put(managerAuthority, ImmutableSet.of(editorAuthority))
          .put(editorAuthority, ImmutableSet.of(viewerAuthority))
          .put(viewerAuthority, ImmutableSet.of())
          .build();
  when(dataserviceRoleHierarchy.getAllGrantedAuthorityInclusions())
      .thenReturn(authorityInclusions);
  assertEquals(
      ImmutableSet.of(managerAuthority, editorAuthority, viewerAuthority),
      cachedRoleHierarchyImpl.getReachableGrantedAuthorities(
          asList(managerAuthority, editorAuthority)));
}
 
Example 3
Source File: CachedRoleHierarchyImplTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testGetReachableGrantedAuthoritiesWithCircularHierarchy() {
  TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

  GrantedAuthority managerAuthority = new SimpleGrantedAuthority("ROLE_MANAGER");
  GrantedAuthority editorAuthority = new SimpleGrantedAuthority("ROLE_EDITOR");
  GrantedAuthority circularAuthority = new SimpleGrantedAuthority("ROLE_CIRCULAR");
  ImmutableMap<GrantedAuthority, ImmutableSet<GrantedAuthority>> authorityInclusions =
      ImmutableMap.<GrantedAuthority, ImmutableSet<GrantedAuthority>>builder()
          .put(managerAuthority, ImmutableSet.of(editorAuthority))
          .put(editorAuthority, ImmutableSet.of(circularAuthority))
          .put(circularAuthority, ImmutableSet.of(managerAuthority))
          .build();
  when(dataserviceRoleHierarchy.getAllGrantedAuthorityInclusions())
      .thenReturn(authorityInclusions);
  assertEquals(
      ImmutableSet.of(managerAuthority, editorAuthority, circularAuthority),
      cachedRoleHierarchyImpl.getReachableGrantedAuthorities(
          asList(managerAuthority, editorAuthority)));
}
 
Example 4
Source File: L2CacheRepositoryDecoratorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testFindAllTransactionReadonly() {
  boolean previousReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
  try {
    TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

    List<Object> ids = asList("0", "1", "2");
    Entity entity0 = when(mock(Entity.class).getIdValue()).thenReturn("0").getMock();
    Entity entity1 = when(mock(Entity.class).getIdValue()).thenReturn("1").getMock();
    Entity entity2 = when(mock(Entity.class).getIdValue()).thenReturn("2").getMock();
    List<Entity> entities = asList(entity0, entity1, entity2);
    when(l2Cache.getBatch(delegateRepository, ids)).thenReturn(entities);
    assertEquals(entities, l2CacheRepositoryDecorator.findAll(ids.stream()).collect(toList()));
  } finally {
    TransactionSynchronizationManager.setCurrentTransactionReadOnly(previousReadOnly);
  }
}
 
Example 5
Source File: L2CacheRepositoryDecoratorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testFindAllFetchTransactionReadonly() {
  boolean previousReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
  try {
    TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

    Fetch fetch = mock(Fetch.class);
    List<Object> ids = asList("0", "1", "2");
    Entity entity0 = when(mock(Entity.class).getIdValue()).thenReturn("0").getMock();
    Entity entity1 = when(mock(Entity.class).getIdValue()).thenReturn("1").getMock();
    Entity entity2 = when(mock(Entity.class).getIdValue()).thenReturn("2").getMock();
    List<Entity> entities = asList(entity0, entity1, entity2);
    when(l2Cache.getBatch(delegateRepository, ids, fetch)).thenReturn(entities);
    assertEquals(
        entities, l2CacheRepositoryDecorator.findAll(ids.stream(), fetch).collect(toList()));
  } finally {
    TransactionSynchronizationManager.setCurrentTransactionReadOnly(previousReadOnly);
  }
}
 
Example 6
Source File: Neo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
	Neo4jTransactionObject transactionObject = extractNeo4jTransaction(transaction);

	TransactionConfig transactionConfig = createTransactionConfigFrom(definition);
	boolean readOnly = definition.isReadOnly();


	TransactionSynchronizationManager.setCurrentTransactionReadOnly(readOnly);

	try {
		// Prepare configuration data
		Neo4jTransactionContext context = new Neo4jTransactionContext(
			databaseSelectionProvider.getDatabaseSelection().getValue(),
			bookmarkManager.getBookmarks()
		);

		// Configure and open session together with a native transaction
		Session session = this.driver
			.session(sessionConfig(readOnly, context.getBookmarks(), context.getDatabaseName()));
		Transaction nativeTransaction = session.beginTransaction(transactionConfig);

		// Synchronize on that
		Neo4jTransactionHolder transactionHolder = new Neo4jTransactionHolder(context, session, nativeTransaction);
		transactionHolder.setSynchronizedWithTransaction(true);
		transactionObject.setResourceHolder(transactionHolder);

		TransactionSynchronizationManager.bindResource(this.driver, transactionHolder);
	} catch (Exception ex) {
		throw new TransactionSystemException(String.format("Could not open a new Neo4j session: %s", ex.getMessage()));
	}
}
 
Example 7
Source File: CachedRoleHierarchyImplTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testGetReachableGrantedAuthoritiesUsingCacheUnknownAuthority() {
  TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);

  GrantedAuthority unknownAuthority = new SimpleGrantedAuthority("ROLE_UNKNOWN");
  when(dataserviceRoleHierarchy.getAllGrantedAuthorityInclusions()).thenReturn(ImmutableMap.of());
  assertEquals(
      singleton(unknownAuthority),
      cachedRoleHierarchyImpl.getReachableGrantedAuthorities(singleton(unknownAuthority)));
}