📄 datasourcetransactionmanagertests.java
字号:
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
conControl.verify();
dsControl.verify();
}
/**
* Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
*/
public void testTransactionWithExceptionOnBegin() throws Exception {
MockControl conControl = MockControl.createControl(Connection.class);
final Connection con = (Connection) conControl.getMock();
MockControl dsControl = MockControl.createControl(DataSource.class);
DataSource ds = (DataSource) dsControl.getMock();
ds.getConnection();
dsControl.setReturnValue(con, 1);
con.getAutoCommit();
conControl.setThrowable(new SQLException("Cannot begin"));
con.close();
conControl.setVoidCallable();
conControl.replay();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown CannotCreateTransactionException");
}
catch (CannotCreateTransactionException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
conControl.verify();
}
public void testTransactionWithExceptionOnCommit() throws Exception {
MockControl conControl = MockControl.createControl(Connection.class);
final Connection con = (Connection) conControl.getMock();
MockControl dsControl = MockControl.createControl(DataSource.class);
DataSource ds = (DataSource) dsControl.getMock();
ds.getConnection();
dsControl.setReturnValue(con, 1);
con.getAutoCommit();
// No need to restore it
conControl.setReturnValue(false, 1);
con.commit();
conControl.setThrowable(new SQLException("Cannot commit"), 1);
con.isReadOnly();
conControl.setReturnValue(false, 1);
con.close();
conControl.setVoidCallable(1);
conControl.replay();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
conControl.verify();
}
public void testTransactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception {
MockControl conControl = MockControl.createControl(Connection.class);
final Connection con = (Connection) conControl.getMock();
MockControl dsControl = MockControl.createControl(DataSource.class);
DataSource ds = (DataSource) dsControl.getMock();
ds.getConnection();
dsControl.setReturnValue(con, 1);
con.getAutoCommit();
// No need to change or restore
conControl.setReturnValue(false);
con.commit();
conControl.setThrowable(new SQLException("Cannot commit"), 1);
con.rollback();
conControl.setVoidCallable(1);
con.isReadOnly();
conControl.setReturnValue(false, 1);
con.close();
conControl.setVoidCallable(1);
conControl.replay();
dsControl.replay();
DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);
tm.setRollbackOnCommitFailure(true);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
conControl.verify();
}
public void testTransactionWithExceptionOnRollback() throws Exception {
MockControl conControl = MockControl.createControl(Connection.class);
final Connection con = (Connection) conControl.getMock();
MockControl dsControl = MockControl.createControl(DataSource.class);
DataSource ds = (DataSource) dsControl.getMock();
ds.getConnection();
dsControl.setReturnValue(con, 1);
con.getAutoCommit();
conControl.setReturnValue(true, 1);
// Must restore
con.setAutoCommit(false);
conControl.setVoidCallable(1);
con.rollback();
conControl.setThrowable(new SQLException("Cannot rollback"), 1);
con.setAutoCommit(true);
conControl.setVoidCallable(1);
con.isReadOnly();
conControl.setReturnValue(false, 1);
con.close();
conControl.setVoidCallable(1);
conControl.replay();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
conControl.verify();
}
public void testTransactionWithPropagationSupports() throws Exception {
MockControl dsControl = MockControl.createControl(DataSource.class);
final DataSource ds = (DataSource) dsControl.getMock();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Is not new transaction", !status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
dsControl.verify();
}
public void testTransactionWithPropagationNotSupported() throws Exception {
MockControl dsControl = MockControl.createControl(DataSource.class);
final DataSource ds = (DataSource) dsControl.getMock();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Is not new transaction", !status.isNewTransaction());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
dsControl.verify();
}
public void testTransactionWithPropagationNever() throws Exception {
MockControl dsControl = MockControl.createControl(DataSource.class);
final DataSource ds = (DataSource) dsControl.getMock();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Is not new transaction", !status.isNewTransaction());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
dsControl.verify();
}
public void testExistingTransactionWithPropagationNested() throws Exception {
doTestExistingTransactionWithPropagationNested(1);
}
public void testExistingTransactionWithPropagationNestedTwice() throws Exception {
doTestExistingTransactionWithPropagationNested(2);
}
private void doTestExistingTransactionWithPropagationNested(final int count) throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
MockControl dsControl = MockControl.createControl(DataSource.class);
final DataSource ds = (DataSource) dsControl.getMock();
MockControl conControl = MockControl.createControl(Connection.class);
Connection con = (Connection) conControl.getMock();
MockControl mdControl = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData md = (DatabaseMetaData) mdControl.getMock();
MockControl spControl = MockControl.createControl(Savepoint.class);
Savepoint sp = (Savepoint) spControl.getMock();
con.getAutoCommit();
conControl.setReturnValue(false, 1);
md.supportsSavepoints();
mdControl.setReturnValue(true, 1);
con.getMetaData();
conControl.setReturnValue(md, 1);
for (int i = 1; i <= count; i++) {
con.setSavepoint(ConnectionHolder.SAVEPOINT_NAME_PREFIX + i);
conControl.setReturnValue(sp, 1);
con.releaseSavepoint(sp);
conControl.setVoidCallable(1);
}
con.commit();
conControl.setVoidCallable(1);
con.isReadOnly();
conControl.setReturnValue(false, 1);
con.close();
conControl.setVoidCallable(1);
ds.getConnection();
dsControl.setReturnValue(con, 1);
spControl.replay();
mdControl.replay();
conControl.replay();
dsControl.replay();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
for (int i = 0; i < count; i++) {
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Isn't new transaction", !status.isNewTransaction());
assertTrue("Is nested transaction", status.hasSavepoint());
}
});
}
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
spControl.verify();
mdControl.verify();
conControl.verify();
dsControl.verify();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -