Skip to content

Commit 928f542

Browse files
committed
avoid EntityManager close() exception through isOpen() check (SPR-7215)
1 parent 65622bd commit 928f542

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

org.springframework.orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,7 +42,6 @@
4242
import org.springframework.dao.IncorrectResultSizeDataAccessException;
4343
import org.springframework.dao.InvalidDataAccessApiUsageException;
4444
import org.springframework.jdbc.datasource.DataSourceUtils;
45-
import org.springframework.transaction.support.ResourceHolder;
4645
import org.springframework.transaction.support.ResourceHolderSynchronization;
4746
import org.springframework.transaction.support.TransactionSynchronizationManager;
4847
import org.springframework.util.Assert;
@@ -328,7 +327,9 @@ public static void closeEntityManager(EntityManager em) {
328327
if (em != null) {
329328
logger.debug("Closing JPA EntityManager");
330329
try {
331-
em.close();
330+
if (em.isOpen()) {
331+
em.close();
332+
}
332333
}
333334
catch (PersistenceException ex) {
334335
logger.debug("Could not close JPA EntityManager", ex);

org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaInterceptorTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.AccessibleObject;
2020
import java.lang.reflect.Method;
21-
2221
import javax.persistence.EntityManager;
2322
import javax.persistence.EntityManagerFactory;
2423
import javax.persistence.PersistenceException;
@@ -65,6 +64,7 @@ protected void tearDown() throws Exception {
6564

6665
public void testInterceptorWithNewEntityManager() throws PersistenceException {
6766
factoryControl.expectAndReturn(factory.createEntityManager(), entityManager);
67+
managerControl.expectAndReturn(entityManager.isOpen(), true);
6868
entityManager.close();
6969

7070
factoryControl.replay();
@@ -85,6 +85,7 @@ public void testInterceptorWithNewEntityManager() throws PersistenceException {
8585

8686
public void testInterceptorWithNewEntityManagerAndLazyFlush() throws PersistenceException {
8787
factoryControl.expectAndReturn(factory.createEntityManager(), entityManager);
88+
managerControl.expectAndReturn(entityManager.isOpen(), true);
8889
entityManager.close();
8990

9091
factoryControl.replay();
@@ -181,6 +182,7 @@ public void testInterceptorWithFlushFailure() throws Throwable {
181182

182183
PersistenceException exception = new PersistenceException();
183184
managerControl.setThrowable(exception, 1);
185+
managerControl.expectAndReturn(entityManager.isOpen(), true);
184186
entityManager.close();
185187

186188
factoryControl.replay();
@@ -208,6 +210,7 @@ public void testInterceptorWithFlushFailureWithoutConversion() throws Throwable
208210

209211
PersistenceException exception = new PersistenceException();
210212
managerControl.setThrowable(exception, 1);
213+
managerControl.expectAndReturn(entityManager.isOpen(), true);
211214
entityManager.close();
212215

213216
factoryControl.replay();

org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTemplateTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23-
2423
import javax.persistence.EntityManager;
2524
import javax.persistence.EntityManagerFactory;
2625
import javax.persistence.PersistenceException;
@@ -155,13 +154,13 @@ public void testExecuteJpaCallbackBoolean() {
155154
template.afterPropertiesSet();
156155

157156
factoryControl.expectAndReturn(factory.createEntityManager(), manager);
157+
managerControl.expectAndReturn(manager.isOpen(), true);
158158
manager.close();
159159

160160
managerControl.replay();
161161
factoryControl.replay();
162162

163163
template.execute(new JpaCallback() {
164-
165164
public Object doInJpa(EntityManager em) throws PersistenceException {
166165
assertSame(em, manager);
167166
return null;

org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.sql.SQLException;
2020
import java.util.ArrayList;
2121
import java.util.List;
22-
2322
import javax.persistence.EntityManager;
2423
import javax.persistence.EntityManagerFactory;
2524
import javax.persistence.EntityTransaction;
@@ -77,6 +76,7 @@ protected void setUp() throws Exception {
7776
factoryControl.expectAndReturn(factory.createEntityManager(), manager);
7877
managerControl.expectAndReturn(manager.getTransaction(), tx);
7978
tx.begin();
79+
managerControl.expectAndReturn(manager.isOpen(), true);
8080
manager.close();
8181
}
8282

@@ -448,6 +448,7 @@ public void testParticipatingTransactionWithRequiresNew() {
448448
factoryControl.expectAndReturn(factory.createEntityManager(), manager);
449449
managerControl.expectAndReturn(manager.getTransaction(), tx, 5);
450450
manager.flush();
451+
managerControl.expectAndReturn(manager.isOpen(), true);
451452
manager.close();
452453

453454
factoryControl.replay();
@@ -613,6 +614,7 @@ public void testPropagationSupportsAndRequiresNewAndEarlyAccess() {
613614
factoryControl.expectAndReturn(factory.createEntityManager(), manager);
614615
managerControl.expectAndReturn(manager.getTransaction(), tx, 2);
615616
manager.flush();
617+
managerControl.expectAndReturn(manager.isOpen(), true);
616618
manager.close();
617619
txControl.expectAndReturn(tx.getRollbackOnly(), false);
618620
tx.commit();
@@ -679,6 +681,7 @@ public void testTransactionWithRequiresNewInAfterCompletion() {
679681
tx2.begin();
680682
tx2.commit();
681683
manager2.flush();
684+
managerControl2.expectAndReturn(manager2.isOpen(), true);
682685
manager2.close();
683686

684687
factoryControl.replay();
@@ -731,6 +734,7 @@ public void testTransactionCommitWithPropagationSupports() {
731734
txControl.reset();
732735

733736
manager.flush();
737+
managerControl.expectAndReturn(manager.isOpen(), true);
734738
manager.close();
735739

736740
factoryControl.replay();
@@ -773,6 +777,7 @@ public void testTransactionRollbackWithPropagationSupports() {
773777
txControl.reset();
774778

775779
manager.flush();
780+
managerControl.expectAndReturn(manager.isOpen(), true);
776781
manager.close();
777782

778783
factoryControl.replay();
@@ -1049,6 +1054,7 @@ public void testInvalidIsolation() {
10491054
txControl.reset();
10501055
managerControl.reset();
10511056

1057+
managerControl.expectAndReturn(manager.isOpen(), true);
10521058
manager.close();
10531059

10541060
factoryControl.replay();

org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/SharedEntityManagerFactoryTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,13 @@
1616

1717
package org.springframework.orm.jpa.support;
1818

19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertSame;
21-
import static org.junit.Assert.assertTrue;
22-
import static org.junit.Assert.fail;
23-
2419
import javax.persistence.EntityManager;
2520
import javax.persistence.EntityManagerFactory;
2621

2722
import org.easymock.MockControl;
23+
import static org.junit.Assert.*;
2824
import org.junit.Test;
25+
2926
import org.springframework.orm.jpa.EntityManagerHolder;
3027
import org.springframework.orm.jpa.EntityManagerProxy;
3128
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -46,6 +43,7 @@ public void testValidUsage() {
4643
mockEm.contains(o);
4744
emMc.setReturnValue(false, 1);
4845

46+
emMc.expectAndReturn(mockEm.isOpen(), true);
4947
mockEm.close();
5048
emMc.setVoidCallable(1);
5149
emMc.replay();

0 commit comments

Comments
 (0)