CacheCoordinationType usage

View: New views
2 Messages — Rating Filter:   Alert me  

CacheCoordinationType usage

by Seckin Yazan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi;
I have an application deployed on glassfish cluster environment and doing some sort of DB operations, and currently having problem with the following case

There are two classes as follows;

@Cache(coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES, type = CacheType.FULL, shared = true, size = 100, alwaysRefresh = true)
public class Users implements Serializable {
    @Id
    @Column(name = "USERID", nullable = false, insertable = true, updatable = true, length = 100)
    private String                 userId;
    @ManyToOne
    @JoinColumn(name = "ProfileId", referencedColumnName = "ProfileId", nullable = true, insertable = true, updatable = true)
    private Profiles         profile;

@Cache(coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES, type = CacheType.FULL, shared = true, size = 100, alwaysRefresh = true)
public class Profiles implements java.io.Serializable {

    @Id
    @Column(name = "ProfileId", nullable = false, insertable = true, updatable = true)
    private int                  profileId;

    @OneToMany(mappedBy = "Profile")
    private Collection<Users> profile;


somewhere inside the code i do the following operations
//Create a new user
User user = new User();

//Set profile which is already in DB for user
Profile profile =  entityManager.find(Policy.class, id)
user.setProfile(Profile);

//Insert user to DB
entityMan.persist(user);

right after this operation i get duplicate entry exception for profile, because while inserting user to DB it also tries to insert Profile which is already exist in DB. If I change CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES to CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS it works fine. As far as i know i need to use SEND_NEW_OBJECTS_WITH_CHANGES in order to inform other instances in the cluster for this newly created user object,

Any comment?

Thanks

Seckin



Re: CacheCoordinationType usage

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That is odd.  When you find the profile, what is its id, is it valid?  Can't see how it would be possible for it to be considered a new object.  Are you finding the profile in the same EntityManager as you persist the User?  Does your setProfile method somehow create a copy of the Profile?

SEND_NEW_OBJECTS_WITH_CHANGES is not required, using INVALIDATE_CHANGED_OBJECTS is fine (even recommend).

Also using caching a setting alwaysRefresh to true makes little sense.  If you always want to access the database, then don't bother caching, set shared=false instead.


Seckin Yazan wrote:
Hi;
I have an application deployed on glassfish cluster environment and doing some sort of DB operations, and currently having problem with the following case

There are two classes as follows;

@Cache(coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES, type = CacheType.FULL, shared = true, size = 100, alwaysRefresh = true)
public class Users implements Serializable {
    @Id
    @Column(name = "USERID", nullable = false, insertable = true, updatable = true, length = 100)
    private String                 userId;
    @ManyToOne
    @JoinColumn(name = "ProfileId", referencedColumnName = "ProfileId", nullable = true, insertable = true, updatable = true)
    private Profiles         profile;

@Cache(coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES, type = CacheType.FULL, shared = true, size = 100, alwaysRefresh = true)
public class Profiles implements java.io.Serializable {

    @Id
    @Column(name = "ProfileId", nullable = false, insertable = true, updatable = true)
    private int                  profileId;

    @OneToMany(mappedBy = "Profile")
    private Collection<Users> profile;


somewhere inside the code i do the following operations
//Create a new user
User user = new User();

//Set profile which is already in DB for user
Profile profile =  entityManager.find(Policy.class, id)
user.setProfile(Profile);

//Insert user to DB
entityMan.persist(user);

right after this operation i get duplicate entry exception for profile, because while inserting user to DB it also tries to insert Profile which is already exist in DB. If I change CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES to CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS it works fine. As far as i know i need to use SEND_NEW_OBJECTS_WITH_CHANGES in order to inform other instances in the cluster for this newly created user object,

Any comment?

Thanks

Seckin