Java Code Examples for org.springframework.transaction.support.DefaultTransactionDefinition#setReadOnly()

The following examples show how to use org.springframework.transaction.support.DefaultTransactionDefinition#setReadOnly() . 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: TransactionImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
public TransactionImpl(PlatformTransactionManager transactionManager, PersistenceImpl persistence, boolean join,
                       @Nullable TransactionParams params, String storeName) {
    this.tm = transactionManager;
    this.persistence = persistence;
    this.storeName = storeName;

    log.trace("Creating transaction: store='{}' join={} params={}", storeName, join, params);

    td = new DefaultTransactionDefinition();
    if (join)
        td.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    else
        td.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

    if (params != null) {
        if (params.getTimeout() != 0)
            td.setTimeout(params.getTimeout());
        if (params.isReadOnly())
            td.setReadOnly(true);
    }

    ts = tm.getTransaction(td);

    persistence.registerSynchronizations(storeName);
}
 
Example 2
Source File: SpannerTransactionManagerTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoBeginReadOnly() {
	when(transactionManager.begin()).thenReturn(transactionContext);

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setReadOnly(true);

	TransactionSynchronizationManager.unbindResource(this.databaseClient);

	manager.doBegin(tx, definition);

	Assert.assertNull(tx.getTransactionManager());
	Assert.assertNotNull(tx.getTransactionContext());
	Assert.assertNotEquals(tx.getTransactionContext(), transactionContext);
	Assert.assertTrue(tx.isReadOnly());

	verify(transactionManager, times(0)).begin();
	verify(transactionManager, times(0)).getState();
}
 
Example 3
Source File: SpringJdbcTransactionOperations.java    From micronaut-data with Apache License 2.0 6 votes vote down vote up
@Override
public <R> R execute(@NonNull TransactionDefinition definition, @NonNull TransactionCallback<Connection, R> callback) {
    ArgumentUtils.requireNonNull("callback", callback);
    ArgumentUtils.requireNonNull("definition", definition);

    final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setReadOnly(definition.isReadOnly());
    def.setIsolationLevel(definition.getIsolationLevel().getCode());
    def.setPropagationBehavior(definition.getPropagationBehavior().ordinal());
    def.setName(definition.getName());
    final Duration timeout = definition.getTimeout();
    if (!timeout.isNegative()) {
        def.setTimeout((int) timeout.getSeconds());
    }
    TransactionTemplate template = new TransactionTemplate(transactionManager, def);
    return template.execute(status -> {
                try {
                    return callback.call(new JdbcTransactionStatus(status));
                } catch (RuntimeException | Error ex) {
                    throw ex;
                } catch (Exception e) {
                    throw new UndeclaredThrowableException(e, "TransactionCallback threw undeclared checked exception");
                }
            }
    );
}
 
Example 4
Source File: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void doTestNewTransactionWithCommit(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode != WebSphereUowTransactionManager.SYNCHRONIZATION_NEVER) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 5
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void newTransactionWithCommitAndTimeout() {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setTimeout(10);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
			assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(10, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 6
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void doTestNewTransactionWithCommit(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode != WebSphereUowTransactionManager.SYNCHRONIZATION_NEVER) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 7
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void doTestNewTransactionSynchronization(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode == WebSphereUowTransactionManager.SYNCHRONIZATION_ALWAYS) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 8
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void newTransactionWithCommitAndTimeout() {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setTimeout(10);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
			assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(10, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 9
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void doTestNewTransactionWithCommit(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode != WebSphereUowTransactionManager.SYNCHRONIZATION_NEVER) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 10
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void doTestNewTransactionSynchronization(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode == WebSphereUowTransactionManager.SYNCHRONIZATION_ALWAYS) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 11
Source File: ReadOnlyRoutingDataSourceTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getConnection_NoReadReplicaAvailableReadOnlyTransactionActive_returnsDefaultDataSource()
		throws Exception {

	// Arrange
	DataSource defaultDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	when(defaultDataSource.getConnection()).thenReturn(connection);

	ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
	readOnlyRoutingDataSource.setTargetDataSources(Collections.emptyMap());
	readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
	readOnlyRoutingDataSource.afterPropertiesSet();

	LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(
			readOnlyRoutingDataSource);

	DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
	transactionDefinition.setReadOnly(true);

	TransactionTemplate transactionTemplate = new TransactionTemplate(
			new DataSourceTransactionManager(dataSource), transactionDefinition);

	// Act
	Connection connectionReturned = transactionTemplate.execute(status -> {
		try {
			return ((ConnectionProxy) dataSource.getConnection())
					.getTargetConnection();
		}
		catch (SQLException e) {
			fail(e.getMessage());
		}
		return null;
	});

	// Assert
	assertThat(connectionReturned).isSameAs(connection);
}
 
Example 12
Source File: SpringHibernateTransactionOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param hibernateTransactionManager The hibernate transaction manager.
 */
protected SpringHibernateTransactionOperations(HibernateTransactionManager hibernateTransactionManager) {
    this.sessionFactory = hibernateTransactionManager.getSessionFactory();
    this.transactionManager = hibernateTransactionManager;
    this.writeTransactionTemplate = new TransactionTemplate(hibernateTransactionManager);
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(true);
    this.readTransactionTemplate = new TransactionTemplate(hibernateTransactionManager, transactionDefinition);
}
 
Example 13
Source File: SpringJdbcTransactionOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param transactionManager The transaction manager
 */
protected SpringJdbcTransactionOperations(
        DataSourceTransactionManager transactionManager) {
    this.dataSource = transactionManager.getDataSource();
    this.transactionManager = transactionManager;
    this.writeTransactionTemplate = new TransactionTemplate(transactionManager);
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(true);
    this.readTransactionTemplate = new TransactionTemplate(transactionManager, transactionDefinition);
}
 
Example 14
Source File: TransactionUtil.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 开启新的只读事务
 */
public static TransactionStatus beginNewReadTransaction() {
	DefaultTransactionDefinition td = new DefaultTransactionDefinition(
			TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	td.setReadOnly(true);
	TransactionStatus status = getTransactionManager().getTransaction(td);
	return status;
}
 
Example 15
Source File: TransactionUtil.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 开启新的只读事务
 */
public static TransactionStatus beginNewReadTransaction() {
	DefaultTransactionDefinition td = new DefaultTransactionDefinition(
			TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	td.setReadOnly(true);
	TransactionStatus status = getTransactionManager().getTransaction(td);
	return status;
}
 
Example 16
Source File: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void newTransactionWithCommitAndTimeout() {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setTimeout(10);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
			assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(10, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 17
Source File: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void doTestNewTransactionSynchronization(int propagationBehavior, final int synchMode) {
	MockUOWManager manager = new MockUOWManager();
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	ptm.setTransactionSynchronization(synchMode);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(propagationBehavior);
	definition.setReadOnly(true);

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			if (synchMode == WebSphereUowTransactionManager.SYNCHRONIZATION_ALWAYS) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			else {
				assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
				assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
				assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			}
			return "result";
		}
	}));

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	assertEquals(0, manager.getUOWTimeout());
	assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
}
 
Example 18
Source File: FirestoreIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void transactionTest() {
	User alice = new User("Alice", 29);
	User bob = new User("Bob", 60);


	DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
	transactionDefinition.setReadOnly(false);
	TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);

	reset(this.txManager);

	this.firestoreTemplate.save(alice).then(this.firestoreTemplate.save(bob))
			.as(operator::transactional)
			.block();

	assertThat(this.firestoreTemplate.findAll(User.class).collectList().block())
			.containsExactlyInAnyOrder(bob, alice);

	verify(this.txManager, times(1)).commit(any());
	verify(this.txManager, times(0)).rollback(any());
	verify(this.txManager, times(1)).getReactiveTransaction(any());

	reset(this.txManager);

	// test rollback
	this.firestoreTemplate.saveAll(Mono.defer(() -> {
		throw new FirestoreDataException("BOOM!");
	}))
			.then(this.firestoreTemplate.deleteAll(User.class))
			.as(operator::transactional)
			.onErrorResume(throwable -> Mono.empty())
			.block();

	verify(this.txManager, times(0)).commit(any());
	verify(this.txManager, times(1)).rollback(any());
	verify(this.txManager, times(1)).getReactiveTransaction(any());

	assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(2);

	this.firestoreTemplate.findAll(User.class)
			.flatMap(a -> {
				a.setAge(a.getAge() - 1);
				return this.firestoreTemplate.save(a);
			})
			.as(operator::transactional).collectList().block();

	assertThat(this.firestoreTemplate.findAll(User.class).map(User::getAge).collectList().block())
			.containsExactlyInAnyOrder(28, 59);

	this.firestoreTemplate.deleteAll(User.class).as(operator::transactional).block();
	assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
}
 
Example 19
Source File: TransactionCommponentInterceptor.java    From framework with Apache License 2.0 4 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param flowBean
 * @param flowContext
 * @return <br>
 */
@Override
public boolean before(final Serializable flowBean, final FlowContext flowContext) {
    Map<String, Object> configAttrMap = flowContext.getFlowConfig().getConfigAttrMap();

    String transactional = (String) configAttrMap.get("transactional");
    if ("true".equals(transactional)) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();

        String propagation = (String) configAttrMap.get("propagation");
        if (StringUtils.isNotEmpty(propagation)) {
            switch (propagation) {
                case "PROPAGATION_REQUIRED":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
                    break;
                case "PROPAGATION_REQUIRES_NEW":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
                    break;
                case "PROPAGATION_SUPPORTS":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
                    break;
                case "PROPAGATION_NOT_SUPPORTED":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
                    break;
                case "PROPAGATION_MANDATORY":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
                    break;
                case "PROPAGATION_NESTED":
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
                    break;
                default:
                    throw new ServiceException(ErrorCodeDef.UNSUPPORT_TRASACTION_TYPE, propagation);
            }
        }

        if ("true".equals(configAttrMap.get("readOnly"))) {
            def.setReadOnly(true);
        }
        statusHolder.get().push(TransactionManagerHolder.getTransactionManager().getTransaction(def));
    }
    return true;
}
 
Example 20
Source File: DeploymentHelper.java    From hawkbit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Executes the modifying action in new transaction
 *
 * @param txManager
 *            transaction manager interface
 * @param transactionName
 *            the name of the new transaction
 * @param isolationLevel
 *            isolation level of the new transaction
 * @param action
 *            the callback to execute in new tranaction
 *
 * @return the result of the action
 */
public static <T> T runInNewTransaction(@NotNull final PlatformTransactionManager txManager,
        final String transactionName, final int isolationLevel, @NotNull final TransactionCallback<T> action) {
    final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName(transactionName);
    def.setReadOnly(false);
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    def.setIsolationLevel(isolationLevel);
    return new TransactionTemplate(txManager, def).execute(action);
}