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");
* 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,8 +327,10 @@ public abstract class EntityManagerFactoryUtils {
if (em != null) {
logger.debug("Closing JPA EntityManager");
try {
if (em.isOpen()) {
em.close();
}
}
catch (PersistenceException 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");
* 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();

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");
* 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;

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");
* 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();

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");
* 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();