當前位置: 首頁>>代碼示例>>Java>>正文


Java TransactionTemplate.execute方法代碼示例

本文整理匯總了Java中org.springframework.transaction.support.TransactionTemplate.execute方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionTemplate.execute方法的具體用法?Java TransactionTemplate.execute怎麽用?Java TransactionTemplate.execute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.transaction.support.TransactionTemplate的用法示例。


在下文中一共展示了TransactionTemplate.execute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: updateSql

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
/**
 * 批處理更新操作
 * 
 * @param dbInfoId
 * @param sqls
 * @return 返回更新的列的數量
 * @throws Exception
 */
public int[] updateSql(String dbInfoId, String[] sqls) throws Exception {
	final String[] fsqls = this.getFormatArrays(sqls);
	if (log.isDebugEnabled()) {
		for (String s : fsqls) {
			log.debug(s);
		}
	}
	DataSource ds = getDataSourceByDbInfoId(dbInfoId);
	final TransactionTemplate transactionTemplate = SpringJdbcUtils.getTransactionTemplate(ds);
	return transactionTemplate.execute(new TransactionCallback<int[]>() {
		public int[] doInTransaction(TransactionStatus status) {
			JdbcTemplate jdbcTemplate = SpringJdbcUtils.getJdbcTemplate(transactionTemplate);
			int[] i = jdbcTemplate.batchUpdate(fsqls);
			return i;
		}
	});
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:26,代碼來源:DbService.java

示例2: testSpringLocalTx

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringLocalTx() throws Exception {
    ConnectionFactory cf = createCF(BROKER_URL);
    JmsTemplate jms = new JmsTemplate(cf);
    jms.setDefaultDestinationName(QUEUE);
    jms.setReceiveTimeout(1000);
    PlatformTransactionManager tm = new JmsTransactionManager(cf);
    TransactionTemplate localTx = new TransactionTemplate(tm);

    localTx.execute(ts -> {
        jms.convertAndSend("Hello");
        return null;
    });
    Object msg = localTx.execute(ts -> jms.receiveAndConvert());
    assertEquals("Hello", msg);

    localTx.execute(ts -> {
        jms.convertAndSend("Hello");
        ts.setRollbackOnly();
        return null;
    });
    msg = localTx.execute(ts -> jms.receiveAndConvert());
    assertNull(msg);
}
 
開發者ID:ops4j,項目名稱:org.ops4j.pax.transx,代碼行數:25,代碼來源:ActiveMQTest.java

示例3: testSpringXaTx

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringXaTx() throws Exception {
    DataSource ds = wrap(createHsqlDataSource());
    JdbcTemplate jdbc = new JdbcTemplate(ds);
    TransactionTemplate tx = new TransactionTemplate(ptm);

    jdbc.execute(DROP_USER);
    jdbc.execute(CREATE_TABLE_USER);

    tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
    User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
    assertEquals(new User(1, "user1"), user);
    tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));

    tx.execute(ts -> {
        int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
        ts.setRollbackOnly();
        return nb;
    });
    try {
        user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
        fail("Expected a EmptyResultDataAccessException");
    } catch (EmptyResultDataAccessException e) {
        // expected
    }
}
 
開發者ID:ops4j,項目名稱:org.ops4j.pax.transx,代碼行數:27,代碼來源:HsqlTest.java

示例4: testSpringLocalTx

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringLocalTx() throws Exception {
    DataSource ds = wrap(createH2DataSource());
    JdbcTemplate jdbc = new JdbcTemplate(ds);
    TransactionTemplate tx = new TransactionTemplate(new DataSourceTransactionManager(ds));

    jdbc.execute(DROP_USER);
    jdbc.execute(CREATE_TABLE_USER);

    tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
    User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
    assertEquals(new User(1, "user1"), user);
    tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));

    tx.execute(ts -> {
        int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
        ts.setRollbackOnly();
        return nb;
    });
    try {
        user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
        fail("Expected a EmptyResultDataAccessException");
    } catch (EmptyResultDataAccessException e) {
        // expected
    }
}
 
開發者ID:ops4j,項目名稱:org.ops4j.pax.transx,代碼行數:27,代碼來源:H2Test.java

示例5: transactionTemplateWithException

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void transactionTemplateWithException() {
	TestTransactionManager tm = new TestTransactionManager(false, true);
	TransactionTemplate template = new TransactionTemplate(tm);
	final RuntimeException ex = new RuntimeException("Some application exception");
	try {
		template.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				throw ex;
			}
		});
		fail("Should have propagated RuntimeException");
	}
	catch (RuntimeException caught) {
		// expected
		assertTrue("Correct exception", caught == ex);
		assertTrue("triggered begin", tm.begin);
		assertTrue("no commit", !tm.commit);
		assertTrue("triggered rollback", tm.rollback);
		assertTrue("no rollbackOnly", !tm.rollbackOnly);
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:TransactionSupportTests.java

示例6: jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback

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

	final TransactionSynchronization synch = mock(TransactionSynchronization.class);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	ptm.setGlobalRollbackOnParticipationFailure(false);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	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

示例7: testTransactionCommitWithPropagationSupports

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionCommitWithPropagationSupports() {
	given(pmf.getPersistenceManager()).willReturn(pm);

	PlatformTransactionManager tm = new JdoTransactionManager(pmf);
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	final List l = new ArrayList();
	l.add("test");
	assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));

	Object result = tt.execute(new TransactionCallback() {
		@Override
		public Object doInTransaction(TransactionStatus status) {
			assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
			assertTrue("Is not new transaction", !status.isNewTransaction());
			PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
			return l;
		}
	});
	assertTrue("Correct result list", result == l);

	assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));

	verify(pm, times(2)).close();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:27,代碼來源:JdoTransactionManagerTests.java

示例8: 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

示例9: jtaTransactionManagerWithPropagationRequiresNewAndAdapter

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
	TransactionManager tm = mock(TransactionManager.class);
	Transaction tx = mock(Transaction.class);
	given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.suspend()).willReturn(tx);

	JtaTransactionManager ptm = newJtaTransactionManager(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(tm).begin();
	verify(tm).commit();
	verify(tm).resume(tx);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:JtaTransactionManagerTests.java

示例10: 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

示例11: 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

示例12: 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

示例13: createFolderAction

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
public static Object createFolderAction(final String name, final String path, final String children,
										final Long datasetId, final boolean flag, final Long bindId) {
	TransactionTemplate transactionTemplate = getTransactionTemplate();
	Object object = transactionTemplate.execute(new TransactionCallback<Object>() {
		public Object doInTransaction(TransactionStatus status) {
			int res = 0;
			try {
				// insert the record and get the folder id.
				KeyHolder keyHolder = new GeneratedKeyHolder();
				getJdbcTemplate().update(new PreparedStatementCreator() {
							public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
								PreparedStatement ps = getJdbcTemplate().getDataSource().getConnection().
										prepareStatement(CREATE_LOGIC_DATASET_FOLDER, new String[]{ "title" ,"path"});
								ps.setString(1, name);
								ps.setString(2, path);
								return ps;
							}
						}, keyHolder);
				res = keyHolder.getKey().intValue();
				if (res <= 0) throw new Exception();

				String childrenList = children + (children.length() == 0?children:",") + res;
				int row = getJdbcTemplate().update(UPDATE_LOGIC_DATASET_CHILDREN, childrenList, datasetId);
				if (row <= 0) throw new Exception();

				if (!flag) {
					row = getJdbcTemplate().update(UPDATE_LOGIC_DATASET_DATASETID, bindId, 0, res);
					if (row <= 0) throw new Exception();
				}
			} catch (Exception e) {
				status.setRollbackOnly();
				e.printStackTrace();
			}
			return res;
		}
	});
	return object;
}
 
開發者ID:thomas-young-2013,項目名稱:wherehowsX,代碼行數:39,代碼來源:DatasetsDAO.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.execute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。