mirror of https://github.com/apache/openjpa.git
OPENJPA-1884: setting svn eol style
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.1.x@1044029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8cf5f35488
commit
25e0ed7d4f
|
@ -1,223 +1,223 @@
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
* or more contributor license agreements. See the NOTICE file
|
* or more contributor license agreements. See the NOTICE file
|
||||||
* distributed with this work for additional information
|
* distributed with this work for additional information
|
||||||
* regarding copyright ownership. The ASF licenses this file
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
* to you under the Apache License, Version 2.0 (the
|
* to you under the Apache License, Version 2.0 (the
|
||||||
* "License"); you may not use this file except in compliance
|
* "License"); you may not use this file except in compliance
|
||||||
* with the License. You may obtain a copy of the License at
|
* with the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
* software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.apache.openjpa.persistence.cache.jpa;
|
package org.apache.openjpa.persistence.cache.jpa;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import javax.persistence.Cache;
|
import javax.persistence.Cache;
|
||||||
import javax.persistence.CacheStoreMode;
|
import javax.persistence.CacheStoreMode;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.LockModeType;
|
import javax.persistence.LockModeType;
|
||||||
|
|
||||||
|
|
||||||
import org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity;
|
import org.apache.openjpa.persistence.cache.jpa.model.CacheableEntity;
|
||||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies L2 operations using multiple entity manager factories. The EMF which
|
* Verifies L2 operations using multiple entity manager factories. The EMF which
|
||||||
* does updates does not have the data cache enabled thus, it does not send
|
* does updates does not have the data cache enabled thus, it does not send
|
||||||
* sjvm updates upon commit.
|
* sjvm updates upon commit.
|
||||||
*/
|
*/
|
||||||
public class TestMultiEMFCacheModes extends SingleEMFTestCase {
|
public class TestMultiEMFCacheModes extends SingleEMFTestCase {
|
||||||
|
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
setUp( CLEAR_TABLES, CacheableEntity.class,
|
setUp( CLEAR_TABLES, CacheableEntity.class,
|
||||||
"openjpa.ConnectionFactoryProperties", "PrintParameters=true",
|
"openjpa.ConnectionFactoryProperties", "PrintParameters=true",
|
||||||
"openjpa.DataCache", "true");
|
"openjpa.DataCache", "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the data cache us updated via a em.refresh operation when
|
* Verifies that the data cache us updated via a em.refresh operation when
|
||||||
* javax.persistence.cache.storeMode = CacheStoreMode.REFRESH and the
|
* javax.persistence.cache.storeMode = CacheStoreMode.REFRESH and the
|
||||||
* entity is updated in the database.
|
* entity is updated in the database.
|
||||||
*/
|
*/
|
||||||
public void testCacheRefreshModeRefresh() {
|
public void testCacheRefreshModeRefresh() {
|
||||||
|
|
||||||
EntityManager em = emf.createEntityManager();
|
EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
// Create a new cacheable entity
|
// Create a new cacheable entity
|
||||||
CacheableEntity ce = createEntity(em);
|
CacheableEntity ce = createEntity(em);
|
||||||
int ceid = ce.getId();
|
int ceid = ce.getId();
|
||||||
|
|
||||||
// Clear the L1
|
// Clear the L1
|
||||||
em.clear();
|
em.clear();
|
||||||
|
|
||||||
// Clear the L2 cache
|
// Clear the L2 cache
|
||||||
Cache cache = emf.getCache();
|
Cache cache = emf.getCache();
|
||||||
cache.evictAll();
|
cache.evictAll();
|
||||||
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
||||||
|
|
||||||
// Find the entity, reloading it into the L2
|
// Find the entity, reloading it into the L2
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
assertTrue(em.contains(ce));
|
assertTrue(em.contains(ce));
|
||||||
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
||||||
assertTrue(em.getLockMode(ce) == LockModeType.NONE);
|
assertTrue(em.getLockMode(ce) == LockModeType.NONE);
|
||||||
assertEquals(ce.getName(), "Cached Entity");
|
assertEquals(ce.getName(), "Cached Entity");
|
||||||
assertEquals(ce.getVersion(), 1);
|
assertEquals(ce.getVersion(), 1);
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
|
|
||||||
// Create a new EMF -WITHOUT- the L2 enabled. If the L2 was enabled, the
|
// Create a new EMF -WITHOUT- the L2 enabled. If the L2 was enabled, the
|
||||||
// sjvm remote commit provider would evict the entity upon update, throwing
|
// sjvm remote commit provider would evict the entity upon update, throwing
|
||||||
// off the intent of this variation.
|
// off the intent of this variation.
|
||||||
EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
|
EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
|
||||||
"openjpa.LockManager", "mixed",
|
"openjpa.LockManager", "mixed",
|
||||||
"openjpa.ConnectionFactoryProperties", "PrintParameters=true");
|
"openjpa.ConnectionFactoryProperties", "PrintParameters=true");
|
||||||
EntityManager em2 = emf2.createEntityManager();
|
EntityManager em2 = emf2.createEntityManager();
|
||||||
|
|
||||||
// Find + lock, then update the entity and commit
|
// Find + lock, then update the entity and commit
|
||||||
em2.getTransaction().begin();
|
em2.getTransaction().begin();
|
||||||
CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
||||||
ce2.setName("Updated Cached Entity");
|
ce2.setName("Updated Cached Entity");
|
||||||
em2.getTransaction().commit();
|
em2.getTransaction().commit();
|
||||||
em2.close();
|
em2.close();
|
||||||
emf2.close();
|
emf2.close();
|
||||||
|
|
||||||
// Refresh the entity - this will load the entity into the L1 and with storeMode=REFRESH,
|
// Refresh the entity - this will load the entity into the L1 and with storeMode=REFRESH,
|
||||||
// should also refresh it in the L2
|
// should also refresh it in the L2
|
||||||
java.util.Map<String, Object> cacheStoreModeMap = new java.util.HashMap<String, Object>();
|
java.util.Map<String, Object> cacheStoreModeMap = new java.util.HashMap<String, Object>();
|
||||||
cacheStoreModeMap.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
|
cacheStoreModeMap.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
|
||||||
em.refresh(ce, cacheStoreModeMap);
|
em.refresh(ce, cacheStoreModeMap);
|
||||||
|
|
||||||
// Verify the entity was updated
|
// Verify the entity was updated
|
||||||
verifyUpdatedEntity(ce, ceid);
|
verifyUpdatedEntity(ce, ceid);
|
||||||
|
|
||||||
// Verify loading from the L1
|
// Verify loading from the L1
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
|
|
||||||
// Verify the entity was updated
|
// Verify the entity was updated
|
||||||
verifyUpdatedEntity(ce, ceid);
|
verifyUpdatedEntity(ce, ceid);
|
||||||
|
|
||||||
// Clear the L1
|
// Clear the L1
|
||||||
em.clear();
|
em.clear();
|
||||||
|
|
||||||
// Assert the L2 contains the entity
|
// Assert the L2 contains the entity
|
||||||
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
||||||
|
|
||||||
// Reload the entity from the L2
|
// Reload the entity from the L2
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
|
|
||||||
// Verify the entity in the L2 was updated
|
// Verify the entity in the L2 was updated
|
||||||
verifyUpdatedEntity(ce, ceid);
|
verifyUpdatedEntity(ce, ceid);
|
||||||
|
|
||||||
em.close();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the data cache us updated via a em.refresh operation when
|
* Verifies that the data cache us updated via a em.refresh operation when
|
||||||
* javax.persistence.cache.storeMode = CacheStoreMode.REFRESH and the
|
* javax.persistence.cache.storeMode = CacheStoreMode.REFRESH and the
|
||||||
* record is removed from the database.
|
* record is removed from the database.
|
||||||
*/
|
*/
|
||||||
public void testCacheRefreshModeRefreshDelete() {
|
public void testCacheRefreshModeRefreshDelete() {
|
||||||
|
|
||||||
EntityManager em = emf.createEntityManager();
|
EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
// Create a new cachable entity
|
// Create a new cachable entity
|
||||||
CacheableEntity ce = createEntity(em);
|
CacheableEntity ce = createEntity(em);
|
||||||
int ceid = ce.getId();
|
int ceid = ce.getId();
|
||||||
|
|
||||||
// Clear the L2 cache
|
// Clear the L2 cache
|
||||||
Cache cache = emf.getCache();
|
Cache cache = emf.getCache();
|
||||||
cache.evictAll();
|
cache.evictAll();
|
||||||
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
||||||
|
|
||||||
// Find the entity, reloading it into the L2
|
// Find the entity, reloading it into the L2
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
assertTrue(em.contains(ce));
|
assertTrue(em.contains(ce));
|
||||||
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
assertTrue(cache.contains(CacheableEntity.class, ceid));
|
||||||
assertTrue(em.getLockMode(ce) == LockModeType.NONE);
|
assertTrue(em.getLockMode(ce) == LockModeType.NONE);
|
||||||
assertEquals(ce.getName(), "Cached Entity");
|
assertEquals(ce.getName(), "Cached Entity");
|
||||||
assertEquals(ce.getVersion(), 1);
|
assertEquals(ce.getVersion(), 1);
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
|
|
||||||
// Create a new EMF -WITHOUT- the L2 enabled. If the L2 was enabled, the
|
// Create a new EMF -WITHOUT- the L2 enabled. If the L2 was enabled, the
|
||||||
// sjvm remote commit provider would evict the entity upon delete, throwing
|
// sjvm remote commit provider would evict the entity upon delete, throwing
|
||||||
// off the intent of this variation.
|
// off the intent of this variation.
|
||||||
EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
|
EntityManagerFactory emf2 = this.createEMF(CacheableEntity.class,
|
||||||
"openjpa.LockManager", "mixed",
|
"openjpa.LockManager", "mixed",
|
||||||
"openjpa.ConnectionFactoryProperties", "PrintParameters=true");
|
"openjpa.ConnectionFactoryProperties", "PrintParameters=true");
|
||||||
EntityManager em2 = emf2.createEntityManager();
|
EntityManager em2 = emf2.createEntityManager();
|
||||||
|
|
||||||
// Find and delete the entity in a separate context with no L2 configured
|
// Find and delete the entity in a separate context with no L2 configured
|
||||||
em2.getTransaction().begin();
|
em2.getTransaction().begin();
|
||||||
CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid);
|
CacheableEntity ce2 = em2.find(CacheableEntity.class, ceid);
|
||||||
assertNotNull(ce2);
|
assertNotNull(ce2);
|
||||||
em2.remove(ce2);
|
em2.remove(ce2);
|
||||||
em2.getTransaction().commit();
|
em2.getTransaction().commit();
|
||||||
em2.close();
|
em2.close();
|
||||||
emf2.close();
|
emf2.close();
|
||||||
|
|
||||||
// Refresh the entity with storeMode=REFRESH. The entity has been deleted so it will be
|
// Refresh the entity with storeMode=REFRESH. The entity has been deleted so it will be
|
||||||
// purged from the L2 cache when the DB load fails.
|
// purged from the L2 cache when the DB load fails.
|
||||||
java.util.Map<String, Object> cacheStoreModeMap = new java.util.HashMap<String, Object>();
|
java.util.Map<String, Object> cacheStoreModeMap = new java.util.HashMap<String, Object>();
|
||||||
cacheStoreModeMap.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
|
cacheStoreModeMap.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
|
||||||
try {
|
try {
|
||||||
em.refresh(ce, cacheStoreModeMap);
|
em.refresh(ce, cacheStoreModeMap);
|
||||||
fail("Refresh operation should have thrown an exception");
|
fail("Refresh operation should have thrown an exception");
|
||||||
} catch (EntityNotFoundException e) {
|
} catch (EntityNotFoundException e) {
|
||||||
// expected exception
|
// expected exception
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try loading from the L1 - OpenJPA will detect the entity was
|
// Try loading from the L1 - OpenJPA will detect the entity was
|
||||||
// removed in another transaction.
|
// removed in another transaction.
|
||||||
try {
|
try {
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
fail("OpenJPA should have detected the removed entity");
|
fail("OpenJPA should have detected the removed entity");
|
||||||
} catch (EntityNotFoundException e) {
|
} catch (EntityNotFoundException e) {
|
||||||
// expected exception
|
// expected exception
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the L1
|
// Clear the L1
|
||||||
em.clear();
|
em.clear();
|
||||||
|
|
||||||
// Assert the L2 no longer contains the entity
|
// Assert the L2 no longer contains the entity
|
||||||
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
assertFalse(cache.contains(CacheableEntity.class, ceid));
|
||||||
|
|
||||||
// Attempt to reload entity from the L2 or database
|
// Attempt to reload entity from the L2 or database
|
||||||
ce = em.find(CacheableEntity.class, ceid);
|
ce = em.find(CacheableEntity.class, ceid);
|
||||||
|
|
||||||
// Verify the entity was removed from L2 and DB
|
// Verify the entity was removed from L2 and DB
|
||||||
assertNull(ce);
|
assertNull(ce);
|
||||||
|
|
||||||
em.close();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private CacheableEntity createEntity(EntityManager em) {
|
private CacheableEntity createEntity(EntityManager em) {
|
||||||
CacheableEntity ce = new CacheableEntity();
|
CacheableEntity ce = new CacheableEntity();
|
||||||
int ceid = new Random().nextInt();
|
int ceid = new Random().nextInt();
|
||||||
ce.setId(ceid);
|
ce.setId(ceid);
|
||||||
ce.setName("Cached Entity");
|
ce.setName("Cached Entity");
|
||||||
|
|
||||||
// Persist the new cachable entity
|
// Persist the new cachable entity
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(ce);
|
em.persist(ce);
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
em.clear();
|
em.clear();
|
||||||
return ce;
|
return ce;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyUpdatedEntity(CacheableEntity ce, int id) {
|
private void verifyUpdatedEntity(CacheableEntity ce, int id) {
|
||||||
assertEquals(id, ce.getId());
|
assertEquals(id, ce.getId());
|
||||||
assertEquals("Updated Cached Entity", ce.getName());
|
assertEquals("Updated Cached Entity", ce.getName());
|
||||||
assertEquals(2, ce.getVersion());
|
assertEquals(2, ce.getVersion());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue