0
votes

i use Eclipselink in JTA with Wildfly 8, the problem is after commit the user transaction and inserting the data to the database it not update the related entity so if i reload the page or open a new instance of the page it loads the data without the inserted rows.

@ManagedBean
@ViewScoped
public class empcreator {

UserTransaction ut=null;

@PersistenceContext(unitName="game")
private EntityManager em;

Context icontext=null;
public empcreator() throws NamingException{
    icontext=new InitialContext();
    ut=(UserTransaction)icontext.lookup("java:comp/UserTransaction");
}

the persist method is:

    public void addElementToIsland() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
    int objectid=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("objectid"));
    int x=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("x"));
    int y=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("y"));
    int z=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("z"));
    int r=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("r"));
    int islid=selectedisland;

    Query q=em.createNamedQuery("Element.findOne");
    q.setParameter("id", objectid);
    Element element=(Element)q.getResultList().get(0);

    q=em.createNamedQuery("Island.findOne");
    q.setParameter("islandid", islid);
    Island isl=(Island)q.getResultList().get(0);

    islandsobjects el=new islandsobjects();
    el.setElement(element);
    el.setX(x);
    el.setY(y);
    el.setZ(z);
    el.setR(r);
    el.setIsland(isl);

    ut.begin();
    em.persist(el);
    ut.commit();
}

and the load function is:

    public void loadIsland(ActionEvent e){
    Query q=em.createNamedQuery("Island.findOne");
    q.setParameter("islandid", selectedisland);
    List<islandsobjects> iobjects=((Island)q.getResultList().get(0)).getIobjects();
    JsonObjectBuilder jsonOB=Json.createObjectBuilder();
    JsonArrayBuilder jsonAB=Json.createArrayBuilder();
    for(int i=0;i<iobjects.size();i++){
        jsonOB.add("id", iobjects.get(i).getId());
        Element element=iobjects.get(i).getElement();
        jsonOB.add("name", element.getName());
        jsonOB.add("x", iobjects.get(i).getX());
        jsonOB.add("y", iobjects.get(i).getY());
        jsonOB.add("z", iobjects.get(i).getZ());
        jsonOB.add("r", iobjects.get(i).getR());
        jsonOB.add("objectid", iobjects.get(i).getObjectid());
        jsonOB.add("islandid", selectedisland);
        jsonAB.add(jsonOB);
    }
    JsonObject jsElements=Json.createObjectBuilder().add("elements", jsonAB).build();
    RequestContext.getCurrentInstance().execute("loadisland("+jsElements.toString()+")");
}

this is the persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="game" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/boddooo</jta-data-source>
    <class>boddooo.entity.Empire</class>
    <class>boddooo.entity.Empiresobject</class>
    <class>boddooo.entity.Island</class>
    <class>boddooo.entity.Element</class>
    <class>boddooo.entity.User</class>
    <class>boddooo.entity.World</class>
    <class>boddooo.entity.islandsobjects</class>
      <properties>
          <property name="eclipselink.target-server" value="JBoss"/>
          <property name="eclipselink.logging.level" value="FINE"/>
          <property name="eclipselink.persistence-context.flush-mode" value="auto" />
      </properties>
</persistence-unit>
</persistence>

i also change the flush mode to commit but no result.

1
You provided a solution, but you never really showed the problem. What entity isn't being updated, and what changes are you not seeing? Make sure that your code is modifying both sides of bidirectional relationships, as that seems to be the most common reason the cache might not be updated even though the database isChris

1 Answers

0
votes

when i disabled caching it solve the problem but became more slow then i use this

query.setHint("javax.persistence.cache.storeMode", "REFRESH");

it updated the data without disable caching