Support for read-only transactions through explicit SET TRANSACTION statement

Issue: SPR-15210
(cherry picked from commit 02195f5)
This commit is contained in:
Juergen Hoeller
2017-02-02 20:06:28 +01:00
parent d5daa8e3a7
commit e038631a6d
2 changed files with 108 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import javax.sql.DataSource;
import org.junit.After;
@@ -66,7 +67,7 @@ public class DataSourceTransactionManagerTests {
@Before
public void setUp() throws Exception {
con = mock(Connection.class);
ds = mock(DataSource.class);
ds = mock(DataSource.class);
tm = new DataSourceTransactionManager(ds);
given(ds.getConnection()).willReturn(con);
}
@@ -111,8 +112,8 @@ public class DataSourceTransactionManagerTests {
}
private void doTestTransactionCommitRestoringAutoCommit(
boolean autoCommit, boolean lazyConnection, final boolean createStatement)
throws Exception {
boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
@@ -198,10 +199,10 @@ public class DataSourceTransactionManagerTests {
private void doTestTransactionRollbackRestoringAutoCommit(
boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(
Connection.TRANSACTION_READ_COMMITTED);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
}
if (!lazyConnection || createStatement) {
@@ -835,6 +836,37 @@ public class DataSourceTransactionManagerTests {
verify(con).close();
}
@Test
public void testTransactionWithEnforceReadOnly() throws Exception {
tm.setEnforceReadOnly(true);
given(con.getAutoCommit()).willReturn(true);
Statement stmt = mock(Statement.class);
given(con.createStatement()).willReturn(stmt);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.setReadOnly(true);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
// something transactional
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
InOrder ordered = inOrder(con, stmt);
ordered.verify(con).setAutoCommit(false);
ordered.verify(stmt).executeUpdate("SET TRANSACTION READ ONLY");
ordered.verify(stmt).close();
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
ordered.verify(con).close();
}
@Test
public void testTransactionWithLongTimeout() throws Exception {
doTestTransactionWithTimeout(10);