diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java index 73ce6020091..ecaca5890f2 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -42,7 +42,6 @@ import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.transaction.support.ResourceHolder; import org.springframework.transaction.support.ResourceHolderSynchronization; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.Assert; @@ -328,7 +327,9 @@ public abstract class EntityManagerFactoryUtils { if (em != null) { logger.debug("Closing JPA EntityManager"); try { - em.close(); + if (em.isOpen()) { + em.close(); + } } catch (PersistenceException ex) { logger.debug("Could not close JPA EntityManager", ex); diff --git a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaInterceptorTests.java b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaInterceptorTests.java index 6075c524d39..b70e89752ff 100644 --- a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaInterceptorTests.java +++ b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaInterceptorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2010 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. @@ -18,7 +18,6 @@ package org.springframework.orm.jpa; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Method; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; @@ -65,6 +64,7 @@ public class JpaInterceptorTests extends TestCase { public void testInterceptorWithNewEntityManager() throws PersistenceException { factoryControl.expectAndReturn(factory.createEntityManager(), entityManager); + managerControl.expectAndReturn(entityManager.isOpen(), true); entityManager.close(); factoryControl.replay(); @@ -85,6 +85,7 @@ public class JpaInterceptorTests extends TestCase { public void testInterceptorWithNewEntityManagerAndLazyFlush() throws PersistenceException { factoryControl.expectAndReturn(factory.createEntityManager(), entityManager); + managerControl.expectAndReturn(entityManager.isOpen(), true); entityManager.close(); factoryControl.replay(); @@ -181,6 +182,7 @@ public class JpaInterceptorTests extends TestCase { PersistenceException exception = new PersistenceException(); managerControl.setThrowable(exception, 1); + managerControl.expectAndReturn(entityManager.isOpen(), true); entityManager.close(); factoryControl.replay(); @@ -208,6 +210,7 @@ public class JpaInterceptorTests extends TestCase { PersistenceException exception = new PersistenceException(); managerControl.setThrowable(exception, 1); + managerControl.expectAndReturn(entityManager.isOpen(), true); entityManager.close(); factoryControl.replay(); diff --git a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTemplateTests.java b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTemplateTests.java index 60386d548c6..c78959f5cc9 100644 --- a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTemplateTests.java +++ b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2010 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. @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; @@ -155,13 +154,13 @@ public class JpaTemplateTests extends TestCase { template.afterPropertiesSet(); factoryControl.expectAndReturn(factory.createEntityManager(), manager); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); managerControl.replay(); factoryControl.replay(); template.execute(new JpaCallback() { - public Object doInJpa(EntityManager em) throws PersistenceException { assertSame(em, manager); return null; diff --git a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java index 5a6f76807bf..722f25adfee 100644 --- a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java +++ b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -19,7 +19,6 @@ package org.springframework.orm.jpa; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -77,6 +76,7 @@ public class JpaTransactionManagerTests extends TestCase { factoryControl.expectAndReturn(factory.createEntityManager(), manager); managerControl.expectAndReturn(manager.getTransaction(), tx); tx.begin(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); } @@ -448,6 +448,7 @@ public class JpaTransactionManagerTests extends TestCase { factoryControl.expectAndReturn(factory.createEntityManager(), manager); managerControl.expectAndReturn(manager.getTransaction(), tx, 5); manager.flush(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); factoryControl.replay(); @@ -613,6 +614,7 @@ public class JpaTransactionManagerTests extends TestCase { factoryControl.expectAndReturn(factory.createEntityManager(), manager); managerControl.expectAndReturn(manager.getTransaction(), tx, 2); manager.flush(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); txControl.expectAndReturn(tx.getRollbackOnly(), false); tx.commit(); @@ -679,6 +681,7 @@ public class JpaTransactionManagerTests extends TestCase { tx2.begin(); tx2.commit(); manager2.flush(); + managerControl2.expectAndReturn(manager2.isOpen(), true); manager2.close(); factoryControl.replay(); @@ -731,6 +734,7 @@ public class JpaTransactionManagerTests extends TestCase { txControl.reset(); manager.flush(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); factoryControl.replay(); @@ -773,6 +777,7 @@ public class JpaTransactionManagerTests extends TestCase { txControl.reset(); manager.flush(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); factoryControl.replay(); @@ -1049,6 +1054,7 @@ public class JpaTransactionManagerTests extends TestCase { txControl.reset(); managerControl.reset(); + managerControl.expectAndReturn(manager.isOpen(), true); manager.close(); factoryControl.replay(); diff --git a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/SharedEntityManagerFactoryTests.java b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/SharedEntityManagerFactoryTests.java index bb103c0caac..b22527cdab0 100644 --- a/org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/SharedEntityManagerFactoryTests.java +++ b/org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/SharedEntityManagerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2010 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. @@ -16,16 +16,13 @@ package org.springframework.orm.jpa.support; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import org.easymock.MockControl; +import static org.junit.Assert.*; import org.junit.Test; + import org.springframework.orm.jpa.EntityManagerHolder; import org.springframework.orm.jpa.EntityManagerProxy; import org.springframework.transaction.support.TransactionSynchronizationManager; @@ -46,6 +43,7 @@ public class SharedEntityManagerFactoryTests { mockEm.contains(o); emMc.setReturnValue(false, 1); + emMc.expectAndReturn(mockEm.isOpen(), true); mockEm.close(); emMc.setVoidCallable(1); emMc.replay();