Skip to content

Commit d22d408

Browse files
committed
Propagate read-only status through Session.setDefaultReadOnly(true)
Issue: SPR-16956
1 parent 0b86c71 commit d22d408

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTransactionManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
495495
if (definition.isReadOnly() && txObject.isNewSession()) {
496496
// Just set to MANUAL in case of a new Session for this transaction.
497497
session.setFlushMode(FlushMode.MANUAL);
498+
// As of 5.1, we're also setting Hibernate's read-only entity mode by default.
499+
session.setDefaultReadOnly(true);
498500
}
499501

500502
if (!definition.isReadOnly() && !txObject.isNewSession()) {

spring-test/src/test/java/org/springframework/test/context/junit4/orm/HibernateSessionFlushingTests.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import javax.persistence.PersistenceException;
2020

21+
import org.hibernate.Session;
2122
import org.hibernate.SessionFactory;
2223
import org.hibernate.exception.ConstraintViolationException;
2324
import org.junit.Before;
@@ -29,6 +30,7 @@
2930
import org.springframework.test.context.junit4.orm.domain.DriversLicense;
3031
import org.springframework.test.context.junit4.orm.domain.Person;
3132
import org.springframework.test.context.junit4.orm.service.PersonService;
33+
import org.springframework.transaction.annotation.Transactional;
3234

3335
import static org.junit.Assert.*;
3436
import static org.springframework.test.transaction.TransactionTestUtils.*;
@@ -38,6 +40,8 @@
3840
* Hibernate.
3941
*
4042
* @author Sam Brannen
43+
* @author Juergen Hoeller
44+
* @author Vlad Mihalcea
4145
* @since 3.0
4246
*/
4347
@ContextConfiguration
@@ -53,21 +57,14 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
5357
private SessionFactory sessionFactory;
5458

5559

56-
protected int countRowsInPersonTable() {
57-
return countRowsInTable("person");
58-
}
59-
60-
protected void assertPersonCount(int expectedCount) {
61-
assertEquals("Verifying number of rows in the 'person' table.", expectedCount, countRowsInPersonTable());
62-
}
63-
6460
@Before
65-
public void setUp() {
61+
public void setup() {
6662
assertInTransaction(true);
6763
assertNotNull("PersonService should have been autowired.", personService);
6864
assertNotNull("SessionFactory should have been autowired.", sessionFactory);
6965
}
7066

67+
7168
@Test
7269
public void findSam() {
7370
Person sam = personService.findByName(SAM);
@@ -77,13 +74,25 @@ public void findSam() {
7774
assertEquals("Verifying Sam's driver's license number", Long.valueOf(1234), driversLicense.getNumber());
7875
}
7976

77+
@Test // SPR-16956
78+
@Transactional(readOnly = true)
79+
public void findSamWithReadOnlySession() {
80+
Person sam = personService.findByName(SAM);
81+
sam.setName("Vlad");
82+
// By setting setDefaultReadOnly(true), the user can no longer modify any entity...
83+
Session session = sessionFactory.getCurrentSession();
84+
session.flush();
85+
session.refresh(sam);
86+
assertEquals("Sam", sam.getName());
87+
}
88+
8089
@Test
8190
public void saveJuergenWithDriversLicense() {
8291
DriversLicense driversLicense = new DriversLicense(2L, 2222L);
8392
Person juergen = new Person(JUERGEN, driversLicense);
84-
int numRows = countRowsInPersonTable();
93+
int numRows = countRowsInTable("person");
8594
personService.save(juergen);
86-
assertPersonCount(numRows + 1);
95+
assertEquals("Verifying number of rows in the 'person' table.", numRows + 1, countRowsInTable("person"));
8796
assertNotNull("Should be able to save and retrieve Juergen", personService.findByName(JUERGEN));
8897
assertNotNull("Juergen's ID should have been set", juergen.getId());
8998
}
@@ -93,13 +102,6 @@ public void saveJuergenWithNullDriversLicense() {
93102
personService.save(new Person(JUERGEN));
94103
}
95104

96-
private void updateSamWithNullDriversLicense() {
97-
Person sam = personService.findByName(SAM);
98-
assertNotNull("Should be able to find Sam", sam);
99-
sam.setDriversLicense(null);
100-
personService.save(sam);
101-
}
102-
103105
@Test
104106
// no expected exception!
105107
public void updateSamWithNullDriversLicenseWithoutSessionFlush() {
@@ -121,4 +123,11 @@ public void updateSamWithNullDriversLicenseWithSessionFlush() throws Throwable {
121123
}
122124
}
123125

126+
private void updateSamWithNullDriversLicense() {
127+
Person sam = personService.findByName(SAM);
128+
assertNotNull("Should be able to find Sam", sam);
129+
sam.setDriversLicense(null);
130+
personService.save(sam);
131+
}
132+
124133
}

0 commit comments

Comments
 (0)