Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Two persistence units share same cache
Two persistence units share same cache [message #1689088] Sun, 22 March 2015 17:29 Go to next message
Vasily Laktionov is currently offline Vasily LaktionovFriend
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 #1689770 is a reply to message #1689088] Tue, 24 March 2015 09:30 Go to previous messageGo to next message
Petros Splinakis is currently offline Petros SplinakisFriend
Messages: 12
Registered: September 2014
Junior Member
It is not very clear to me what you are trying to do, but in general it is my understanding that the way for two entity managers to share a L2 cache is for them to refer to the same persistence unit.
The persistence unit is mapped by session name, so what is mentioned in the guide is that you can use eclipselink.session-name property for making sure that the persistence unit session name will be the same between two different applications deployed under the same Java EE container using the same persistence unit configuration. These two applications will effectively be sharing the same persistence unit.
Re: Two persistence units share same cache [message #1689808 is a reply to message #1689770] Tue, 24 March 2015 11:22 Go to previous messageGo to next message
Vasily Laktionov is currently offline Vasily LaktionovFriend
Messages: 2
Registered: March 2015
Junior Member
Thank you for you reply, Petros.
Petros Splinakis wrote on Tue, 24 March 2015 09:30
It is not very clear to me what you are trying to do

I will try to describe the problem in detail.
In our system we have master-view DB architecture implemented as three DBs: jdbc/pinets_web1_dev_local and jdbc/pinets_web2_dev_local (view DBs) both serve only select requests and jdbc/pinets_web_dev_local (master DB) that returns the number of active view DB.
One of web1 or web2 can be active at a given moment, ie serve select requests.
Table Org (maps to Org entity) is in both view DBs.
Suppose current inactive view DB is web1.
web1 drops all data from Org and then fresh data is bulk loaded into Org.
Then web1 becomes active and web2 becomes inactive.
What we need to do is
1. Load fresh data from in cache each time when active DB is switched.
2. Make all application requests in cache.
Hope this is more detailed explanation.
Can we solve our problem in one application with two persistence units for web1 and web2?
Thanks in advance.
Re: Two persistence units share same cache [message #1691364 is a reply to message #1689808] Mon, 06 April 2015 15:47 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
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

Previous Topic:Relationship Inheritance
Next Topic:Ripple Load across optional ManyToOne using EclipseLink 2.6.0
Goto Forum:
  


Current Time: Fri Dec 06 04:57:56 GMT 2024

Powered by FUDForum. Page generated in 0.03344 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top