|
|
|
@ -25,9 +25,7 @@ import org.mockito.InOrder; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.dao.ConcurrencyFailureException; |
|
|
|
import org.springframework.dao.ConcurrencyFailureException; |
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManagerTests; |
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManagerTests; |
|
|
|
import org.springframework.transaction.TransactionStatus; |
|
|
|
|
|
|
|
import org.springframework.transaction.TransactionSystemException; |
|
|
|
import org.springframework.transaction.TransactionSystemException; |
|
|
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
|
|
|
|
|
|
|
import org.springframework.transaction.support.TransactionSynchronizationManager; |
|
|
|
import org.springframework.transaction.support.TransactionSynchronizationManager; |
|
|
|
import org.springframework.transaction.support.TransactionTemplate; |
|
|
|
import org.springframework.transaction.support.TransactionTemplate; |
|
|
|
|
|
|
|
|
|
|
|
@ -53,17 +51,14 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
protected void testTransactionWithExceptionOnCommit() throws Exception { |
|
|
|
protected void transactionWithExceptionOnCommit() throws Exception { |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
|
|
|
|
|
|
|
|
// plain TransactionSystemException
|
|
|
|
// plain TransactionSystemException
|
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
|
|
|
|
|
// something transactional
|
|
|
|
// something transactional
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
@ -71,18 +66,15 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testTransactionWithDataAccessExceptionOnCommit() throws Exception { |
|
|
|
void transactionWithDataAccessExceptionOnCommit() throws Exception { |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
((JdbcTransactionManager) tm).setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task)); |
|
|
|
((JdbcTransactionManager) tm).setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task)); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
|
|
|
|
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
|
|
|
|
|
// something transactional
|
|
|
|
// something transactional
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
@ -90,17 +82,14 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testTransactionWithDataAccessExceptionOnCommitFromLazyExceptionTranslator() throws Exception { |
|
|
|
void transactionWithDataAccessExceptionOnCommitFromLazyExceptionTranslator() throws Exception { |
|
|
|
willThrow(new SQLException("Cannot commit", "40")).given(con).commit(); |
|
|
|
willThrow(new SQLException("Cannot commit", "40")).given(con).commit(); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
|
|
|
|
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
|
|
|
|
|
// something transactional
|
|
|
|
// something transactional
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
@ -109,7 +98,7 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
protected void testTransactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception { |
|
|
|
protected void transactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception { |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
willThrow(new SQLException("Cannot commit")).given(con).commit(); |
|
|
|
|
|
|
|
|
|
|
|
tm.setRollbackOnCommitFailure(true); |
|
|
|
tm.setRollbackOnCommitFailure(true); |
|
|
|
@ -117,11 +106,8 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
// plain TransactionSystemException
|
|
|
|
// plain TransactionSystemException
|
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
|
|
|
|
|
// something transactional
|
|
|
|
// something transactional
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
@ -131,16 +117,14 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
protected void testTransactionWithExceptionOnRollback() throws Exception { |
|
|
|
protected void transactionWithExceptionOnRollback() throws Exception { |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
willThrow(new SQLException("Cannot rollback")).given(con).rollback(); |
|
|
|
willThrow(new SQLException("Cannot rollback")).given(con).rollback(); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
|
|
|
|
|
|
|
|
// plain TransactionSystemException
|
|
|
|
// plain TransactionSystemException
|
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { |
|
|
|
|
|
|
|
assertThat(status.getTransactionName()).isEmpty(); |
|
|
|
assertThat(status.getTransactionName()).isEmpty(); |
|
|
|
assertThat(status.hasTransaction()).isTrue(); |
|
|
|
assertThat(status.hasTransaction()).isTrue(); |
|
|
|
assertThat(status.isNewTransaction()).isTrue(); |
|
|
|
assertThat(status.isNewTransaction()).isTrue(); |
|
|
|
@ -151,7 +135,6 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
status.setRollbackOnly(); |
|
|
|
status.setRollbackOnly(); |
|
|
|
assertThat(status.isRollbackOnly()).isTrue(); |
|
|
|
assertThat(status.isRollbackOnly()).isTrue(); |
|
|
|
assertThat(status.isCompleted()).isFalse(); |
|
|
|
assertThat(status.isCompleted()).isFalse(); |
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
@ -163,7 +146,7 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testTransactionWithDataAccessExceptionOnRollback() throws Exception { |
|
|
|
void transactionWithDataAccessExceptionOnRollback() throws Exception { |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
willThrow(new SQLException("Cannot rollback")).given(con).rollback(); |
|
|
|
willThrow(new SQLException("Cannot rollback")).given(con).rollback(); |
|
|
|
((JdbcTransactionManager) tm).setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task)); |
|
|
|
((JdbcTransactionManager) tm).setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task)); |
|
|
|
@ -171,12 +154,7 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> status.setRollbackOnly())); |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { |
|
|
|
|
|
|
|
status.setRollbackOnly(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
InOrder ordered = inOrder(con); |
|
|
|
InOrder ordered = inOrder(con); |
|
|
|
@ -187,16 +165,14 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testTransactionWithDataAccessExceptionOnRollbackFromLazyExceptionTranslator() throws Exception { |
|
|
|
void transactionWithDataAccessExceptionOnRollbackFromLazyExceptionTranslator() throws Exception { |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
given(con.getAutoCommit()).willReturn(true); |
|
|
|
willThrow(new SQLException("Cannot rollback", "40")).given(con).rollback(); |
|
|
|
willThrow(new SQLException("Cannot rollback", "40")).given(con).rollback(); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
TransactionTemplate tt = new TransactionTemplate(tm); |
|
|
|
|
|
|
|
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
// specific ConcurrencyFailureException
|
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> |
|
|
|
tt.execute(new TransactionCallbackWithoutResult() { |
|
|
|
tt.executeWithoutResult(status -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { |
|
|
|
|
|
|
|
assertThat(status.getTransactionName()).isEmpty(); |
|
|
|
assertThat(status.getTransactionName()).isEmpty(); |
|
|
|
assertThat(status.hasTransaction()).isTrue(); |
|
|
|
assertThat(status.hasTransaction()).isTrue(); |
|
|
|
assertThat(status.isNewTransaction()).isTrue(); |
|
|
|
assertThat(status.isNewTransaction()).isTrue(); |
|
|
|
@ -207,7 +183,6 @@ class JdbcTransactionManagerTests extends DataSourceTransactionManagerTests { |
|
|
|
status.setRollbackOnly(); |
|
|
|
status.setRollbackOnly(); |
|
|
|
assertThat(status.isRollbackOnly()).isTrue(); |
|
|
|
assertThat(status.isRollbackOnly()).isTrue(); |
|
|
|
assertThat(status.isCompleted()).isFalse(); |
|
|
|
assertThat(status.isCompleted()).isFalse(); |
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse(); |
|
|
|
|