Skip to content

Commit

Permalink
Add isolation level test and restore null return value from beginTran…
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller authored and mdeinum committed Jun 29, 2023
1 parent 64e02bf commit 58e5958
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Object beginTransaction(EntityManager entityManager, TransactionDefinitio
}
}

return entityManager;
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -26,8 +26,10 @@
import org.junit.jupiter.api.Test;

import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Person;
import org.springframework.transaction.TransactionDefinition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
Expand Down Expand Up @@ -113,24 +115,34 @@ public void testGetReferenceWhenNoRow() {
}

@Test
public void testLazyLoading() {
public void testLazyLoading() throws Exception {
try {
Person tony = new Person();
tony.setFirstName("Tony");
tony.setLastName("Blair");
tony.setDriversLicense(new DriversLicense("8439DK"));
sharedEntityManager.persist(tony);
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_READ_COMMITTED);
setComplete();
endTransaction();

transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
startNewTransaction();
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_SERIALIZABLE);
sharedEntityManager.clear();
Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
assertThat(tony).isNotSameAs(newTony);
endTransaction();

assertThat(newTony.getDriversLicense()).isNotNull();
transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);
startNewTransaction();
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_READ_COMMITTED);
endTransaction();

assertThat(newTony.getDriversLicense()).isNotNull();
newTony.getDriversLicense().getSerialNumber();
}
finally {
Expand Down

0 comments on commit 58e5958

Please sign in to comment.