Skip to content

SPR-16956 - Propagate read-only status as FlushMode.MANUAL to Hibernate Session #1861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
if (definition.isReadOnly() && txObject.isNewSession()) {
// Just set to MANUAL in case of a new Session for this transaction.
session.setFlushMode(FlushMode.MANUAL);
txObject.getSessionHolder().setPreviousReadOnly( session.isDefaultReadOnly() );
session.setDefaultReadOnly(true);
}

if (!definition.isReadOnly() && !txObject.isNewSession()) {
Expand Down Expand Up @@ -717,6 +719,7 @@ protected void doCleanupAfterCompletion(Object transaction) {
}
if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
session.setDefaultReadOnly(txObject.getSessionHolder().isPreviousReadOnly());
}
if (!this.allowResultAccessAfterCompletion && !this.hibernateManagedSession) {
disconnectOnCompletion(session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class SessionHolder extends ResourceHolderSupport {
@Nullable
private FlushMode previousFlushMode;

private boolean previousReadOnly;

public SessionHolder(Session session) {
Assert.notNull(session, "Session must not be null");
Expand Down Expand Up @@ -75,12 +76,20 @@ public FlushMode getPreviousFlushMode() {
return this.previousFlushMode;
}

public boolean isPreviousReadOnly() {
return previousReadOnly;
}

public void setPreviousReadOnly(boolean previousReadOnly) {
this.previousReadOnly = previousReadOnly;
}

@Override
public void clear() {
super.clear();
this.transaction = null;
this.previousFlushMode = null;
this.previousReadOnly = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ else if (isolationLevelNeeded) {

// Adapt flush mode and store previous isolation level, if any.
FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
boolean previousReadOnly = prepareReadOnly(session, definition.isReadOnly());
return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel, previousReadOnly);
}

@Override
Expand All @@ -174,7 +175,8 @@ public Object prepareTransaction(EntityManager entityManager, boolean readOnly,

Session session = getSession(entityManager);
FlushMode previousFlushMode = prepareFlushMode(session, readOnly);
return new SessionTransactionData(session, previousFlushMode, null, null);
boolean previousReadOnly = prepareReadOnly(session, readOnly);
return new SessionTransactionData(session, previousFlushMode, null, null, previousReadOnly);
}

@SuppressWarnings("deprecation")
Expand All @@ -200,6 +202,12 @@ protected FlushMode prepareFlushMode(Session session, boolean readOnly) throws P
return null;
}

protected boolean prepareReadOnly(Session session, boolean readOnly) {
boolean previousReadOnly = session.isDefaultReadOnly();
session.setDefaultReadOnly(readOnly);
return previousReadOnly;
}

@Override
public void cleanupTransaction(@Nullable Object transactionData) {
if (transactionData instanceof SessionTransactionData) {
Expand Down Expand Up @@ -332,20 +340,24 @@ private static class SessionTransactionData {
@Nullable
private final Integer previousIsolationLevel;

private final boolean previousReadOnly;

public SessionTransactionData(Session session, @Nullable FlushMode previousFlushMode,
@Nullable Connection preparedCon, @Nullable Integer previousIsolationLevel) {
@Nullable Connection preparedCon, @Nullable Integer previousIsolationLevel, boolean previousReadOnly) {

this.session = session;
this.previousFlushMode = previousFlushMode;
this.preparedCon = preparedCon;
this.previousIsolationLevel = previousIsolationLevel;
this.previousReadOnly = previousReadOnly;
}

@SuppressWarnings("deprecation")
public void resetSessionState() {
if (this.previousFlushMode != null) {
this.session.setFlushMode(this.previousFlushMode);
}
session.setDefaultReadOnly(previousReadOnly);
if (this.preparedCon != null && this.session.isConnected()) {
Connection conToReset = HibernateConnectionHandle.doGetConnection(this.session);
if (conToReset != this.preparedCon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import javax.persistence.PersistenceException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -29,9 +31,12 @@
import org.springframework.test.context.junit4.orm.domain.DriversLicense;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.junit4.orm.service.PersonService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;

/**
* Transactional integration tests regarding <i>manual</i> session flushing with
Expand All @@ -52,7 +57,6 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
@Autowired
private SessionFactory sessionFactory;


protected int countRowsInPersonTable() {
return countRowsInTable("person");
}
Expand All @@ -77,6 +81,18 @@ public void findSam() {
assertEquals("Verifying Sam's driver's license number", Long.valueOf(1234), driversLicense.getNumber());
}

@Test
@Transactional(readOnly = true)
public void readOnlySession() {
Session session = sessionFactory.getCurrentSession();
Person sam = personService.findByName(SAM);
sam.setName("Vlad");
//By setting setDefaultReadOnly(true), the user can no longer modify any entity.
session.flush();
session.refresh(sam);
assertEquals("Sam", sam.getName());
}

@Test
public void saveJuergenWithDriversLicense() {
DriversLicense driversLicense = new DriversLicense(2L, 2222L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.transaction.ejb;

import java.util.concurrent.atomic.AtomicReference;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.ejb.dao.TestEntityService;
import org.springframework.test.context.transaction.ejb.model.TestEntity;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import static org.junit.Assert.assertEquals;

/**
*
* @author Vlad MIhalcea
*/
@ContextConfiguration("required-tx-config.xml")
public class ReadOnlyJpaTxDaoTests extends AbstractJUnit4SpringContextTests {

protected static final String TEST_NAME = "test-name";

@PersistenceContext
private EntityManager em;

@Autowired
private TestEntityService testEntityService;

@Autowired
protected JpaTransactionManager transactionManager;

@Before
public void init() {
new TransactionTemplate(transactionManager).execute(
(TransactionCallback<Void>) status -> {
TestEntity testEntity = new TestEntity();
testEntity.setName(TEST_NAME);

em.persist(testEntity);
return null;
} );
}

@Test
public void testReadOnlyTx() {
AtomicReference<TestEntity> entityHolder = new AtomicReference<>();
assertEquals(0, em.find(TestEntity.class, TEST_NAME).getCount());

testEntityService.onReadOnly( em -> {
TestEntity entity = em.find(TestEntity.class, TEST_NAME);
entity.setCount(100);
//By setting setDefaultReadOnly(true), the user can no longer modify any entity.
em.flush();
em.refresh(entity);
entityHolder.set(entity);
});

assertEquals(0, entityHolder.get().getCount());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.transaction.ejb.dao;

import java.util.function.Consumer;
import javax.persistence.EntityManager;

/**
* @author Vlad Mihalcea
*/
public interface TestEntityService {

void onReadOnly(Consumer<EntityManager> callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.transaction.ejb.dao;

import java.util.function.Consumer;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TestEntityServiceImpl implements TestEntityService {

@PersistenceContext
protected EntityManager em;

@Override
@Transactional(readOnly = true)
public void onReadOnly(Consumer<EntityManager> callback) {
callback.accept(em);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

<bean class="org.springframework.test.context.transaction.ejb.dao.RequiredEjbTxTestEntityDao" />

<bean class="org.springframework.test.context.transaction.ejb.dao.TestEntityServiceImpl" />

</beans>