Java Code Examples for org.springframework.jdbc.datasource.DataSourceTransactionManager#rollback()

The following examples show how to use org.springframework.jdbc.datasource.DataSourceTransactionManager#rollback() . 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: JDBCTemplateTest.java    From tddl with Apache License 2.0 8 votes vote down vote up
@Test
public void tractionRollBackTest() {
    JdbcTemplate andorJT = new JdbcTemplate(us);
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(us);
    TransactionStatus ts = transactionManager.getTransaction(def);

    try {
        sql = String.format("insert into %s (pk,name) values(?,?)", normaltblTableName);
        andorJT.update(sql, new Object[] { RANDOM_ID, name });
        sql = String.format("select * from %s where pk= ?", normaltblTableName);
        Map re = andorJT.queryForMap(sql, new Object[] { RANDOM_ID });
        Assert.assertEquals(name, String.valueOf(re.get("NAME")));
        // 回滚
        transactionManager.rollback(ts);
    } catch (DataAccessException ex) {
        transactionManager.rollback(ts);
        throw ex;
    } finally {
    }
    // 验证查询不到数据
    sql = String.format("select * from %s where pk= ?", normaltblTableName);
    List le = andorJT.queryForList(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(0, le.size());
}
 
Example 2
Source File: JDBCTemplateTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
/**
 * traction RollBack
 * 
 * @author zhuoxue
 * @since 5.0.1
 */
@Test
public void tractionRollBackTest() {
    JdbcTemplate andorJT = new JdbcTemplate(tddlDatasource);
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(tddlDatasource);
    TransactionStatus ts = transactionManager.getTransaction(def);

    try {
        sql = String.format("insert into %s (pk,name) values(?,?)", normaltblTableName);
        andorJT.update(sql, new Object[] { RANDOM_ID, name });
        sql = String.format("select * from %s where pk= ?", normaltblTableName);
        Map re = andorJT.queryForMap(sql, new Object[] { RANDOM_ID });
        Assert.assertEquals(name, String.valueOf(re.get("name")));
        // 回滚
        transactionManager.rollback(ts);
    } catch (DataAccessException ex) {
        transactionManager.rollback(ts);
        throw ex;
    } finally {
    }
    // 验证查询不到数据
    sql = String.format("select * from %s where pk= ?", normaltblTableName);
    List le = andorJT.queryForList(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(0, le.size());
}
 
Example 3
Source File: TransactionalMethodInterceptor.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
  DataSourceTransactionManager transactionManager =
      transactionManagerProvider.get();

  DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
  TransactionStatus transaction = transactionManager.getTransaction(
      transactionDefinition);

  try {
    Object result = invocation.proceed();

    try {
      if (transaction.isNewTransaction()) {
        transactionManager.commit(transaction);
      }
    } catch (UnexpectedRollbackException ignore) {
    }

    return result;
  } catch (Exception e) {
    if (transaction.isNewTransaction()) {
      transactionManager.rollback(transaction);
    }

    throw e;
  }
}
 
Example 4
Source File: MultiDataSourcesTransactionManager.java    From uncode-dal-all with GNU General Public License v2.0 5 votes vote down vote up
@Override
	public void rollback(TransactionStatus status) throws TransactionException {
		
		Throwable ex = null;

		//Cannot deactivate transaction synchronization - not active
//		Collections.reverse(dataSources);
		
		for (int i = dataSources.size() - 1; i >= 0; i--) {
			DataSource dataSource = dataSources.get(i);
			try {
				log.debug("Rolling back JDBC transaction");
				
				rollbackCount.addAndGet(1);
				
				DataSourceTransactionManager txManager = this.transactionManagers.get(dataSource);
				TransactionStatus currentStatus = ((MultiDataSourcesTransactionStatus)status).get(dataSource);

				txManager.rollback(currentStatus);

				log.info("Roll back JDBC transaction success");
			} catch (Throwable e) {
				log.info("Could not roll back JDBC transaction", e);
				ex = e;
			} finally {
				rollbackCount.addAndGet(-1);
			}
		}
		
		if (ex != null) {
			throw new RuntimeException(ex);
		}
		
	}