Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Best practices for clearing the entity manager

Hi John,

Where are you running? How is your persistence unit set up? Here's what the spec says:

In section 3.8.6 of the JPA 2.0 spec:

"If an entity manager with transaction-scoped persistence context is in use, the resulting entities will be detached; if an entity manager with an extended persistence context is used, they will be
managed."

Note: EclipseLink uses a second level cache by default, so if you are in a situation where the entities should be returned unmanaged, you may need to turn off shared caching as I indicated below to avoid seeing cached objects.

-Tom

John Ament wrote:
Well, my conundrum, and I think I just need to read a bit more, is that I want it to cache elements as part of an EntityManager.find, for a certain amount of time, and refresh from the database afterwards. What I'm concerned about though is when I have code like this:

Query q = em.createNamedQuery("Evaluation.findByInstAndEmp");
q.setParameter("inst",inst);
q.setParameter("emps",emps);
List<Evaluation> evals = q.getResultList();

The result of evals seems to be cached. This is the behavior I'm trying to avoid. My understanding though is that Queries return unmanaged entities, detached from the PC.

Am I wrong in this understanding?

Thanks,

John

On Wed, Feb 24, 2010 at 10:24 AM, Gordon Yorke <gordon.yorke@xxxxxxxxxx <mailto:gordon.yorke@xxxxxxxxxx>> wrote:

    Have you considered utilizing EclipseLink's cache co-ordination?

    Alternatively JPA 2.0 has added Cache configuration settings to the
    Persistence Unit.  You can set the persistence.xml element
    "shared-cache-mode" to "ENABLE_SELECTIVE"  (
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> )

    --Gordon

    John Ament wrote:
    Tom,

    No, I realize that the results of a find are cached, and this is
    expected.  What I am getting concerned with is that I have no
    cache configuration in my app, yet the results of a
    Query.getResultList are getting cached, which I don't expect.  Is
    there a way to force these to not cache at all?

    Thanks,

    John

    On Wed, Feb 24, 2010 at 9:06 AM, Tom Ware <tom.ware@xxxxxxxxxx
    <mailto:tom.ware@xxxxxxxxxx>> wrote:

        Hi John,

         What do you mean when you say caching is not enabled?  What
        settings are you using to disable the cache? Note that within
        an open entity manager there will always be a cache of the
        objects that are managed within that entity manager.

         Rarely does disabling the cache actually achieve what you
        want it to since the cache is also used to maintain identity.
         There are a number of options you can use to fine tune your
        cache and/or force queries to the database.

         Take a look at the following link for some caching options.
         Some things that might be good to try are using a CacheType
        of WEAK, setting the cache to not be shared, and having it
        expire after a certain amount of time.

        http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Entity_Caching

         Also have a look at our query hints.  You could specify the
        eclipselink.cache-usage hint to tell a query to avoid the
        cache or the eclipselink.refresh hint to tell the query to
        refresh from the database.

        http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#How_to_Use_EclipseLink_JPA_Query_Hints

         There is also the entityManager.refresh() operation that
        forces objects to be refreshed and the entityManager.clear()
        operation that cleans our the individual entityManager.

        -Tom


        John Ament wrote:

            Hey everyone

            So I've begun to notice that there's some odd behavior in
            eclipse link when it comes to a multiple server
            environment that's not cluster (e.g. 4 instances of
            glassfish running on 2 servers).  Basically, it looks like
            repeated calls to em.find as well as execution of queries
            returns the same data rather than actually running the
            queries against the database.  Does anyone have any tips
            on how to avoid issues like this?  Caching is not enabled.

            Thanks,

            John


            ------------------------------------------------------------------------

            _______________________________________________
            eclipselink-users mailing list
            eclipselink-users@xxxxxxxxxxx
            <mailto:eclipselink-users@xxxxxxxxxxx>
            https://dev.eclipse.org/mailman/listinfo/eclipselink-users

        _______________________________________________
        eclipselink-users mailing list
        eclipselink-users@xxxxxxxxxxx
        <mailto:eclipselink-users@xxxxxxxxxxx>
        https://dev.eclipse.org/mailman/listinfo/eclipselink-users


    ------------------------------------------------------------------------
    _______________________________________________ eclipselink-users
    mailing list eclipselink-users@xxxxxxxxxxx
    <mailto:eclipselink-users@xxxxxxxxxxx>
    https://dev.eclipse.org/mailman/listinfo/eclipselink-users

    _______________________________________________
    eclipselink-users mailing list
    eclipselink-users@xxxxxxxxxxx <mailto:eclipselink-users@xxxxxxxxxxx>
    https://dev.eclipse.org/mailman/listinfo/eclipselink-users



------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top