Two persistence units share same cache [message #1689088] |
Sun, 22 March 2015 17:29 |
Vasily Laktionov Messages: 2 Registered: March 2015 |
Junior Member |
|
|
Hello everyone.
I need to cache some data via EntityManager 1 and access that cache via EntityManager 2.
That possibility is mentioned in "Persistence Unit Cache" section at eclipse.org/eclipselink/documentation/2.4/concepts/cache001.htm. But i can't find a proper configurations/code to do that.
Here is my persistence.xml (xmlns, xmlns:xi and xsi:sxhemaLocation are ok):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
<persistence-unit name="WebEJBPU1" transaction-type="JTA">
<jta-data-source>jdbc/pinets_web1_dev_local</jta-data-source>
<class>controller.org.param.Org</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.level" value="WARNING"/>
<property name="eclipselink.logging.level.sql" value="WARNING"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.canonicalmodel.subpackage" value="prod"/>
</properties>
</persistence-unit>
<persistence-unit name="WebEJBPU2" transaction-type="JTA">
<jta-data-source>jdbc/pinets_web2_dev_local</jta-data-source>
<class>controller.org.param.Org</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.level" value="WARNING"/>
<property name="eclipselink.logging.level.sql" value="WARNING"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.canonicalmodel.subpackage" value="prod"/>
</properties>
</persistence-unit>
</persistence>
The only entity is:
@Entity
@Cacheable(true)
@Cache
(
size = 5000,
isolation = CacheIsolationType.SHARED,
expiry = -1
)
@ReadOnly
@NamedQueries(
{
@NamedQuery(name = "Org.findAll", query = "SELECT o FROM Org o")
})
public class Org implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "org_id")
private Integer orgId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 78)
private String orgfname;
public Org(){}
... getters and setters for orgId and orgfname fields
... overridden hashCode(), equals() and toString() methods
}
The simple singleton is:
@Singleton
public class COrg
{
@PersistenceUnit(unitName = "WebEJBPU1")
private EntityManagerFactory _emf1;
@PersistenceUnit(unitName = "WebEJBPU2")
private EntityManagerFactory _emf2;
public void cacheOrgs()
{
Query q = _emf1.createEntityManager().createNamedQuery("Org.findAll");
List<Org> rl = q.getResultList();
}
public List<Org> getOrgs(int rowCount, int emNum)
{
Query q = (emNum == 1 ? _emf1.createEntityManager().createNamedQuery("Org.findAll") : _emf2.createEntityManager().createNamedQuery("Org.findAll"));
q.setHint("eclipselink.cache-usage", "CheckCacheOnly");
q.setHint("eclipselink.read-only", "true");
return q.setMaxResults(rowCount).getResultList();
}
}
Org entity is successfully cached by cacheOrgs() and then getOrgs(..., 1) returns data from cache but how to return data from the same cache via _emf2?
Thanks in advance.
|
|
|
|
|
Re: Two persistence units share same cache [message #1691364 is a reply to message #1689808] |
Mon, 06 April 2015 15:47 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
This won't work with your current design, and you don't really want to share a cache anyway, as you will have both persistence units using the same stale cache forever despite the complex context switching, table clearing/reloading going on behind the scene. What you need is a way to signal to your application that the cache is stale and should be reloaded, so that _emf.getCache().evictAll(); is called and then re-execute cacheOrgs on your persistence unit. Since your application must know which persistence unit should be used, it should also be able to tell when it changes so that the appropriate persistence unit is cleared and refreshed at that time. You might want to clear the persistence unit that the app is switching away from so that the cached resources can be garbage collected.
[Updated on: Mon, 06 April 2015 15:48] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03344 seconds