本文整理匯總了Java中org.springframework.transaction.TransactionDefinition類的典型用法代碼示例。如果您正苦於以下問題:Java TransactionDefinition類的具體用法?Java TransactionDefinition怎麽用?Java TransactionDefinition使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TransactionDefinition類屬於org.springframework.transaction包,在下文中一共展示了TransactionDefinition類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: execGroovyRetObjByDbTran
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
public Object execGroovyRetObjByDbTran(String groovyName, String methodName,
Object... paramArray) throws Exception{
/* MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
DataSource dataSource=microDao.getMicroDataSource();
PlatformTransactionManager transactionManager=new DataSourceTransactionManager(dataSource);*/
PlatformTransactionManager transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
DefaultTransactionDefinition def =new DefaultTransactionDefinition();
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus status=transactionManager.getTransaction(def);
try
{
Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
transactionManager.commit(status);
return retObj;
}
catch(Exception ex)
{
transactionManager.rollback(status);
throw ex;
}
}
示例2: AbstractDbDialect
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler){
this.jdbcTemplate = jdbcTemplate;
this.lobHandler = lobHandler;
// 初始化transction
this.transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 初始化一些數據
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
databaseName = meta.getDatabaseProductName();
databaseMajorVersion = meta.getDatabaseMajorVersion();
databaseMinorVersion = meta.getDatabaseMinorVersion();
return null;
}
});
initTables(jdbcTemplate);
}
示例3: handler
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
/**
* 補償的時候,不走分布式事務處理
*
* @param point point 切點
* @param info 信息
* @return Object
* @throws Throwable
*/
@Override
public Object handler(ProceedingJoinPoint point, TxTransactionInfo info) throws Throwable {
TxTransactionLocal.getInstance().setTxGroupId(CommonConstant.COMPENSATE_ID);
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
try {
final Object proceed = point.proceed();
platformTransactionManager.commit(transactionStatus);
LogUtil.info(LOGGER, "補償事務執行成功!事務組id為:{}", info::getTxGroupId);
return proceed;
} catch (Throwable e) {
LogUtil.info(LOGGER, "補償事務執行失敗!事務組id為:{}", info::getTxGroupId);
platformTransactionManager.rollback(transactionStatus);
throw e;
} finally {
TxTransactionLocal.getInstance().removeTxGroupId();
CompensationLocal.getInstance().removeCompensationId();
}
}
示例4: invoke
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public EasyTransResult invoke(EasyTransFilterChain filterChain, Map<String, Object> header,
EasyTransRequest<?, ?> request) {
Integer pTrxStatus = MetaDataFilter.getMetaData(EasytransConstant.CallHeadKeys.PARENT_TRANSACTION_STATUS);
if(!pTrxStatus.equals(com.yiqiniu.easytrans.datasource.TransStatusLogger.TransactionStatus.UNKNOWN)){
// start transaction to update
PlatformTransactionManager transactionManager = getTransactionManager(filterChain, request);
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager,
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
TransactionId pTrxId = MetaDataFilter.getMetaData(EasytransConstant.CallHeadKeys.PARENT_TRX_ID_KEY);
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
TransactionId trxId = pTrxId;
transStatusLogger.updateExecuteFlagForSlaveTrx(trxId, request, pTrxStatus);
return null;
}
});
boolean commited = pTrxStatus.equals(com.yiqiniu.easytrans.datasource.TransStatusLogger.TransactionStatus.COMMITTED);
//may be concurrent,but it's ok
easyTransSynchronizer.cascadeExecuteCachedTransaction(pTrxId, commited);
}
return filterChain.invokeFilterChain(header, request);
}
示例5: execGroovyRetObjByDbTranNest
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
Object... paramArray) throws Exception{
/* MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
DataSource dataSource=microDao.getMicroDataSource();
PlatformTransactionManager transactionManager=new DataSourceTransactionManager(dataSource);*/
PlatformTransactionManager transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
DefaultTransactionDefinition def =new DefaultTransactionDefinition();
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
if(nestDef==null){
nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
}
def.setPropagationBehavior(nestDef);
TransactionStatus status=transactionManager.getTransaction(def);
try
{
Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
transactionManager.commit(status);
return retObj;
}
catch(Exception ex)
{
transactionManager.rollback(status);
throw ex;
}
}
示例6: getTransaction
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
Transaction.IsolationLevel isolationLevel = translateIsolationLevel(definition.getIsolationLevel());
Transaction t;
if (isolationLevel == null) {
t = sql.transaction();
} else {
t = sql.transaction(isolationLevel);
}
if (definition.isReadOnly()) {
Wrap.execute(() -> t.getConnection().setReadOnly(true));
}
return new SqlStreamTransactionStatus(t);
}
示例7: addBook
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public boolean addBook(Book book) {
// TODO Auto-generated method stub
TransactionDefinition definition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(definition);
if (searchBook(book.getISBN()).getISBN() == 98564567l) {
System.out.println("no book");
int rows = bookDAO.addBook(book);
if (rows > 0) {
transactionManager.commit(transactionStatus);
return true;
}
}
return false;
}
示例8: deleteBook
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public boolean deleteBook(long ISBN) {
// TODO Auto-generated method stub
TransactionDefinition definition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(definition);
if (searchBook(ISBN).getISBN() != 98564567l) {
boolean deleted = bookDAO.deleteBook(ISBN);
if (deleted) {
transactionManager.commit(transactionStatus);
return true;
}
}
return false;
}
示例9: doBegin
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
/**
* 根據Spring配置的事務設置,為當前線程選擇主庫還是從庫數據源
*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition)
{
if(definition.isReadOnly())
{
if(log.isDebugEnabled())
{
log.debug("DataSourceTransactionManager set slave");
}
DataSourceHolder.setSlave();
}
else
{
if(log.isDebugEnabled())
{
log.debug("DataSourceTransactionManager set master");
}
DataSourceHolder.setMaster();
}
super.doBegin(transaction, definition);
}
示例10: beginTransaction
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
throws PersistenceException, SQLException, TransactionException {
OpenJPAEntityManager openJpaEntityManager = getOpenJPAEntityManager(entityManager);
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
// Pass custom isolation level on to OpenJPA's JDBCFetchPlan configuration
FetchPlan fetchPlan = openJpaEntityManager.getFetchPlan();
if (fetchPlan instanceof JDBCFetchPlan) {
IsolationLevel isolation = IsolationLevel.fromConnectionConstant(definition.getIsolationLevel());
((JDBCFetchPlan) fetchPlan).setIsolation(isolation);
}
}
entityManager.getTransaction().begin();
if (!definition.isReadOnly()) {
// Like with EclipseLink, make sure to start the logic transaction early so that other
// participants using the connection (such as JdbcTemplate) run in a transaction.
openJpaEntityManager.beginStore();
}
// Custom implementation for OpenJPA savepoint handling
return new OpenJpaTransactionData(openJpaEntityManager);
}
示例11: testTransactionCommitWithSharedEntityManagerAndPropagationSupports
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Test
public void testTransactionCommitWithSharedEntityManagerAndPropagationSupports() {
given(manager.isOpen()).willReturn(true);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
bean.sharedEntityManager.clear();
return null;
}
});
verify(manager).clear();
verify(manager).close();
}
示例12: testTransactionCommitWithExtendedEntityManagerAndPropagationSupports
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Test
public void testTransactionCommitWithExtendedEntityManagerAndPropagationSupports() {
given(manager.isOpen()).willReturn(true);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
bean.extendedEntityManager.flush();
return null;
}
});
verify(manager).flush();
}
示例13: testTransactionCommitWithPropagationSupports
import org.springframework.transaction.TransactionDefinition; //導入依賴的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();
}
示例14: testIsolationLevel
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Test
public void testIsolationLevel() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
PlatformTransactionManager tm = new JdoTransactionManager(pmf);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
verify(tx).setIsolationLevel(Constants.TX_SERIALIZABLE);
verify(pm).close();
}
示例15: startTransaction
import org.springframework.transaction.TransactionDefinition; //導入依賴的package包/類
@Override
public void startTransaction() {
TransactionDefinition defn;
boolean doCommit = true;
if (transactions.isEmpty()) {
log.debug("startTransaction: default");
defn = defaultTxnDef;
} else if (writeTxnManager.isNestedTransactionAllowed()) {
log.debug("startTransaction: nested");
defn = nested;
} else {
throw new RuntimeException("nested transactions not supported by current configuration");
}
Txn txn = new Txn(writeTxnManager.getTransaction(defn), doCommit);
transactions.push(txn);
log.debug("startTransaction: " + transactions.size());
}