avoid EntityManager close() exception through isOpen() check (SPR-7215)

This commit is contained in:
Juergen Hoeller 2010-06-08 10:21:05 +00:00
parent 65622bd546
commit 928f5423af
5 changed files with 23 additions and 16 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.ResourceHolder;
import org.springframework.transaction.support.ResourceHolderSynchronization; import org.springframework.transaction.support.ResourceHolderSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -328,7 +327,9 @@ public abstract class EntityManagerFactoryUtils {
if (em != null) { if (em != null) {
logger.debug("Closing JPA EntityManager"); logger.debug("Closing JPA EntityManager");
try { try {
em.close(); if (em.isOpen()) {
em.close();
}
} }
catch (PersistenceException ex) { catch (PersistenceException ex) {
logger.debug("Could not close JPA EntityManager", ex); logger.debug("Could not close JPA EntityManager", ex);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.AccessibleObject;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
@ -65,6 +64,7 @@ public class JpaInterceptorTests extends TestCase {
public void testInterceptorWithNewEntityManager() throws PersistenceException { public void testInterceptorWithNewEntityManager() throws PersistenceException {
factoryControl.expectAndReturn(factory.createEntityManager(), entityManager); factoryControl.expectAndReturn(factory.createEntityManager(), entityManager);
managerControl.expectAndReturn(entityManager.isOpen(), true);
entityManager.close(); entityManager.close();
factoryControl.replay(); factoryControl.replay();
@ -85,6 +85,7 @@ public class JpaInterceptorTests extends TestCase {
public void testInterceptorWithNewEntityManagerAndLazyFlush() throws PersistenceException { public void testInterceptorWithNewEntityManagerAndLazyFlush() throws PersistenceException {
factoryControl.expectAndReturn(factory.createEntityManager(), entityManager); factoryControl.expectAndReturn(factory.createEntityManager(), entityManager);
managerControl.expectAndReturn(entityManager.isOpen(), true);
entityManager.close(); entityManager.close();
factoryControl.replay(); factoryControl.replay();
@ -181,6 +182,7 @@ public class JpaInterceptorTests extends TestCase {
PersistenceException exception = new PersistenceException(); PersistenceException exception = new PersistenceException();
managerControl.setThrowable(exception, 1); managerControl.setThrowable(exception, 1);
managerControl.expectAndReturn(entityManager.isOpen(), true);
entityManager.close(); entityManager.close();
factoryControl.replay(); factoryControl.replay();
@ -208,6 +210,7 @@ public class JpaInterceptorTests extends TestCase {
PersistenceException exception = new PersistenceException(); PersistenceException exception = new PersistenceException();
managerControl.setThrowable(exception, 1); managerControl.setThrowable(exception, 1);
managerControl.expectAndReturn(entityManager.isOpen(), true);
entityManager.close(); entityManager.close();
factoryControl.replay(); factoryControl.replay();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
@ -155,13 +154,13 @@ public class JpaTemplateTests extends TestCase {
template.afterPropertiesSet(); template.afterPropertiesSet();
factoryControl.expectAndReturn(factory.createEntityManager(), manager); factoryControl.expectAndReturn(factory.createEntityManager(), manager);
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
managerControl.replay(); managerControl.replay();
factoryControl.replay(); factoryControl.replay();
template.execute(new JpaCallback() { template.execute(new JpaCallback() {
public Object doInJpa(EntityManager em) throws PersistenceException { public Object doInJpa(EntityManager em) throws PersistenceException {
assertSame(em, manager); assertSame(em, manager);
return null; return null;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
@ -77,6 +76,7 @@ public class JpaTransactionManagerTests extends TestCase {
factoryControl.expectAndReturn(factory.createEntityManager(), manager); factoryControl.expectAndReturn(factory.createEntityManager(), manager);
managerControl.expectAndReturn(manager.getTransaction(), tx); managerControl.expectAndReturn(manager.getTransaction(), tx);
tx.begin(); tx.begin();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
} }
@ -448,6 +448,7 @@ public class JpaTransactionManagerTests extends TestCase {
factoryControl.expectAndReturn(factory.createEntityManager(), manager); factoryControl.expectAndReturn(factory.createEntityManager(), manager);
managerControl.expectAndReturn(manager.getTransaction(), tx, 5); managerControl.expectAndReturn(manager.getTransaction(), tx, 5);
manager.flush(); manager.flush();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
factoryControl.replay(); factoryControl.replay();
@ -613,6 +614,7 @@ public class JpaTransactionManagerTests extends TestCase {
factoryControl.expectAndReturn(factory.createEntityManager(), manager); factoryControl.expectAndReturn(factory.createEntityManager(), manager);
managerControl.expectAndReturn(manager.getTransaction(), tx, 2); managerControl.expectAndReturn(manager.getTransaction(), tx, 2);
manager.flush(); manager.flush();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
txControl.expectAndReturn(tx.getRollbackOnly(), false); txControl.expectAndReturn(tx.getRollbackOnly(), false);
tx.commit(); tx.commit();
@ -679,6 +681,7 @@ public class JpaTransactionManagerTests extends TestCase {
tx2.begin(); tx2.begin();
tx2.commit(); tx2.commit();
manager2.flush(); manager2.flush();
managerControl2.expectAndReturn(manager2.isOpen(), true);
manager2.close(); manager2.close();
factoryControl.replay(); factoryControl.replay();
@ -731,6 +734,7 @@ public class JpaTransactionManagerTests extends TestCase {
txControl.reset(); txControl.reset();
manager.flush(); manager.flush();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
factoryControl.replay(); factoryControl.replay();
@ -773,6 +777,7 @@ public class JpaTransactionManagerTests extends TestCase {
txControl.reset(); txControl.reset();
manager.flush(); manager.flush();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
factoryControl.replay(); factoryControl.replay();
@ -1049,6 +1054,7 @@ public class JpaTransactionManagerTests extends TestCase {
txControl.reset(); txControl.reset();
managerControl.reset(); managerControl.reset();
managerControl.expectAndReturn(manager.isOpen(), true);
manager.close(); manager.close();
factoryControl.replay(); factoryControl.replay();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,16 +16,13 @@
package org.springframework.orm.jpa.support; 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.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import org.easymock.MockControl; import org.easymock.MockControl;
import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.orm.jpa.EntityManagerHolder; import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.orm.jpa.EntityManagerProxy; import org.springframework.orm.jpa.EntityManagerProxy;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
@ -46,6 +43,7 @@ public class SharedEntityManagerFactoryTests {
mockEm.contains(o); mockEm.contains(o);
emMc.setReturnValue(false, 1); emMc.setReturnValue(false, 1);
emMc.expectAndReturn(mockEm.isOpen(), true);
mockEm.close(); mockEm.close();
emMc.setVoidCallable(1); emMc.setVoidCallable(1);
emMc.replay(); emMc.replay();