use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project otter by alibaba.
the class DataMatrixServiceImpl method modify.
/**
* 修改
*/
public void modify(final DataMatrix matrix) {
Assert.assertNotNull(matrix);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
DataMatrixDO matrixDo = modelToDo(matrix);
if (dataMatrixDao.checkUnique(matrixDo)) {
dataMatrixDao.update(matrixDo);
} else {
String exceptionCause = "exist the same repeat matrix in the database.";
logger.warn("WARN ## " + exceptionCause);
throw new RepeatConfigureException(exceptionCause);
}
} catch (RepeatConfigureException rce) {
throw rce;
} catch (Exception e) {
logger.error("ERROR ## modify canal(" + matrix.getId() + ") has an exception!");
throw new ManagerException(e);
}
}
});
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project otter by alibaba.
the class DataMatrixServiceImpl method remove.
/**
* 删除
*/
public void remove(final Long matrixId) {
Assert.assertNotNull(matrixId);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
dataMatrixDao.delete(matrixId);
} catch (Exception e) {
logger.error("ERROR ## remove canal(" + matrixId + ") has an exception!");
throw new ManagerException(e);
}
}
});
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project otter by alibaba.
the class NodeServiceImpl method remove.
/**
* 删除
*/
public void remove(final Long nodeId) {
Assert.assertNotNull(nodeId);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
nodeDao.delete(nodeId);
} catch (Exception e) {
logger.error("ERROR ## remove node(" + nodeId + ") has an exception!");
throw new ManagerException(e);
}
}
});
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method transactionWithTimeout.
@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
PreparedStatement ps = mock(PreparedStatement.class);
given(con.getAutoCommit()).willReturn(true);
given(con.prepareStatement("some SQL statement")).willReturn(ps);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setTimeout(timeout);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
Thread.sleep(1500);
} catch (InterruptedException ex) {
}
try {
Connection con = DataSourceUtils.getConnection(ds);
PreparedStatement ps = con.prepareStatement("some SQL statement");
DataSourceUtils.applyTransactionTimeout(ps, ds);
} catch (SQLException ex) {
throw new DataAccessResourceFailureException("", ex);
}
}
});
if (timeout <= 1) {
fail("Should have thrown TransactionTimedOutException");
}
} catch (TransactionTimedOutException ex) {
if (timeout <= 1) {
// expected
} else {
throw ex;
}
}
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
if (timeout > 1) {
verify(ps).setQueryTimeout(timeout - 1);
verify(con).commit();
} else {
verify(con).rollback();
}
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.
@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_COMMITTED) {
@Override
protected void doAfterCompletion(int status) {
super.doAfterCompletion(status);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
});
}
};
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isTrue();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isTrue();
assertThat(synch.afterCompletionCalled).isTrue();
boolean condition3 = synch.afterCompletionException instanceof IllegalStateException;
assertThat(condition3).isTrue();
verify(con, times(2)).commit();
verify(con, times(2)).close();
}
Aggregations