当前位置: 首页>>代码示例>>Java>>正文


Java TransactionTemplate.setPropagationBehavior方法代码示例

本文整理汇总了Java中org.springframework.transaction.support.TransactionTemplate.setPropagationBehavior方法的典型用法代码示例。如果您正苦于以下问题:Java TransactionTemplate.setPropagationBehavior方法的具体用法?Java TransactionTemplate.setPropagationBehavior怎么用?Java TransactionTemplate.setPropagationBehavior使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.transaction.support.TransactionTemplate的用法示例。


在下文中一共展示了TransactionTemplate.setPropagationBehavior方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: jtaTransactionManagerWithExistingAndPropagationSupports

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	final TransactionSynchronization synch = mock(TransactionSynchronization.class);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			TransactionSynchronizationManager.registerSynchronization(synch);
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
	verify(synch).beforeCompletion();
	verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:JtaTransactionManagerTests.java

示例2: testTransactionWithPropagationNestedAndRollback

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionWithPropagationNestedAndRollback() throws Exception {
	final TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Is new transaction", status.isNewTransaction());
			status.setRollbackOnly();
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).rollback();
	verify(con).close();
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:DataSourceTransactionManagerTests.java

示例3: jtaTransactionManagerWithPropagationRequiresNewAndExisting

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	Transaction tx = mock(Transaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.suspend()).willReturn(tx);

	JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).begin();
	verify(ut).commit();
	verify(tm).resume(tx);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:25,代码来源:JtaTransactionManagerTests.java

示例4: execute

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Override
public <T> T execute(final CommandConfig config, final Command<T> command) {
    LOGGER.debug("Running command with propagation {}", config.getTransactionPropagation());

    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.setPropagationBehavior(getPropagation(config));

    T result = transactionTemplate.execute(new TransactionCallback<T>() {
        @Override
        public T doInTransaction(TransactionStatus status) {
            return next.execute(config, command);
        }
    });

    return result;
}
 
开发者ID:flowable,项目名称:flowable-engine,代码行数:17,代码来源:SpringTransactionInterceptor.java

示例5: testTransactionWithPropagationNested

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionWithPropagationNested() throws Exception {
	final TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Is new transaction", status.isNewTransaction());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).commit();
	verify(con).close();
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:DataSourceTransactionManagerTests.java

示例6: jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
			}
		});
		fail("Should have thrown TransactionSuspensionNotSupportedException");
	}
	catch (TransactionSuspensionNotSupportedException ex) {
		// expected
	}
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:JtaTransactionManagerTests.java

示例7: jtaTransactionManagerWithNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
		}
	});

	verify(ut).begin();
	verify(ut).commit();
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:JtaTransactionManagerTests.java

示例8: jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new NotSupportedException("not supported")).given(ut).begin();

	try {
		JtaTransactionManager ptm = newJtaTransactionManager(ut);
		TransactionTemplate tt = new TransactionTemplate(ptm);
		tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:JtaTransactionManagerTests.java

示例9: jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();

	try {
		JtaTransactionManager ptm = newJtaTransactionManager(ut);
		TransactionTemplate tt = new TransactionTemplate(ptm);
		tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:JtaTransactionManagerTests.java

示例10: testTransactionWithPropagationSupports

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionWithPropagationSupports() throws Exception {
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
			assertTrue("Is not new transaction", !status.isNewTransaction());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:DataSourceTransactionManagerTests.java

示例11: testTransactionWithPropagationNever

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionWithPropagationNever() throws Exception {
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
			assertTrue("Is not new transaction", !status.isNewTransaction());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:DataSourceTransactionManagerTests.java

示例12: init

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
private void init(final JdbcTemplate jdbcTemplate, final String markTableName, final String markTableColumn) {
    int count = jdbcTemplate.queryForInt(MessageFormat.format(checkDataSql, markTableName, GLOBAL_THREAD_COUNT - 1));
    if (count != GLOBAL_THREAD_COUNT) {
        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: init " + markTableName + "'s data.");
        }
        TransactionTemplate transactionTemplate = new TransactionTemplate();
        transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);// 注意这里强制使用非事务,保证多线程的可见性
        transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus status) {
                jdbcTemplate.execute(MessageFormat.format(deleteDataSql, markTableName));
                String batchSql = MessageFormat.format(updateSql, new Object[] { markTableName, markTableColumn });
                jdbcTemplate.batchUpdate(batchSql, new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int idx) throws SQLException {
                        ps.setInt(1, idx);
                        ps.setInt(2, 0);
                        // ps.setNull(3, Types.VARCHAR);
                    }

                    public int getBatchSize() {
                        return GLOBAL_THREAD_COUNT;
                    }
                });
                return null;
            }
        });

        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: Init EROSA Client Data: " + updateSql);
        }
    }

}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:37,代码来源:AbstractOperationInterceptor.java

示例13: put

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
/**
 * puts the block into the database.
 *
 * @param blocks
 *            the blocks to use.
 */
@Override
public void put(final Block... blocks) {
	synchronized (this) {
		if (closed) {
			return;
		}
	}
	if (LOG.isInfoEnabled()) {
		LOG.info("STARTED put, {} blocks", NumberFormat.getIntegerInstance().format(blocks.length));
	}
	final DataSourceTransactionManager tsMan = new DataSourceTransactionManager(ds);

	final TransactionTemplate txTemplate = new TransactionTemplate(tsMan);
	// set behavior
	txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	try {
		txTemplate.execute(new TransactionCallback(blocks));
	} catch (final DataAccessException e) {
		if (LOG.isErrorEnabled()) {
			LOG.error("data access exception", e);
		}
	}
	if (LOG.isInfoEnabled()) {
		LOG.info("SUCCESS put, {} blocks", NumberFormat.getIntegerInstance().format(blocks.length));
	}
}
 
开发者ID:coranos,项目名称:neo-java,代码行数:33,代码来源:BlockDbH2Impl.java

示例14: executeCommand

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
public <T> T executeCommand(final Command<T> command) {
	TransactionTemplate template = new TransactionTemplate(platformTransactionManager);
    template.setPropagationBehavior(springPropagationBehaviour);
    return template.execute(new TransactionCallback<T>() {
		public T doInTransaction(TransactionStatus status) {
			return command.execute(context);
		}
    	
	});
}
 
开发者ID:youseries,项目名称:uflo,代码行数:11,代码来源:SpringTransactionCommandService.java

示例15: executeCommandInNewTransaction

import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
public <T> T executeCommandInNewTransaction(final Command<T> command) {
	TransactionTemplate template = new TransactionTemplate(platformTransactionManager);
	template.setPropagationBehavior(newSpringPropagationBehaviour);
	return template.execute(new TransactionCallback<T>() {
		public T doInTransaction(TransactionStatus status) {
			return command.execute(context);
		}
		
	});
}
 
开发者ID:youseries,项目名称:uflo,代码行数:11,代码来源:SpringTransactionCommandService.java


注:本文中的org.springframework.transaction.support.TransactionTemplate.setPropagationBehavior方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。