having trouble with jpa, looks to be reading from cache when told to ignore

Posted by jeff on Stack Overflow See other posts from Stack Overflow or by jeff
Published on 2010-05-27T18:42:43Z Indexed on 2010/05/27 18:51 UTC
Read the original article Hit count: 217

Filed under:
|

i'm using jpa and eclipselink. it says version 2.0.2.v20100323-r6872 for eclipselink. it's an SE application, small. local persistence unit. i am using a postgres database. i have some code the wakes up once per second and does a jpa query on a table. it's polling to see whether some fields on a given record change. but when i update a field in sql, it keeps showing the same value each time i query it. and this is after waiting, creating a new entitymanager, and giving a query hint. when i query the table from sql or python, i can see the changed field. but from within my jpa query, once i've read the value, it never changes in later executions of the query -- even though the entity manager has been recreated. i've tried telling it to not look in the cache. but it seems to ignore that. very puzzling, can you help please?

here is the method. i call this once every 3 seconds. the tgt.getTrlDlrs() println shows me i get the same value for this field on every call of the method ("value won't change"). even if i change the field in the database. when i query the record from outside java, i see the change right away. also, if i stop the java program and restart it, i see the new value printed out immediately:

public void exitChk(EntityManagerFactory emf, Main_1 mn){
    // this will be a list of the trade close targets that are active
    List<Pairs01Tradeclosetgt> mn_res = new ArrayList<Pairs01Tradeclosetgt>();

    //this is a list of the place number in the array list above
    //of positions to close
    ArrayList<Integer> idxsToCl = new ArrayList<Integer>();

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.clear();
    String qryTxt =
            "SELECT p FROM Pairs01Tradeclosetgt p WHERE p.isClosed = :isClosed AND p.isFilled = :isFilled";
    Query qMn = em.createQuery(qryTxt);
    qMn.setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache);
    qMn.setParameter("isClosed", false);
    qMn.setParameter("isFilled", true);
    mn_res = (List<Pairs01Tradeclosetgt>) qMn.getResultList();

    // are there any trade close targets we need to check for closing?
    if (mn_res!=null){
        //if yes, see whether they've hit their target
        for (int i=0;i<mn_res.size();i++){
            Pairs01Tradeclosetgt tgt = new Pairs01Tradeclosetgt();
            tgt = mn_res.get(i);
            System.out.println("value won't change:" + tgt.getTrlDlrs());

here is my entity class (partial):

@Entity
@Table(name = "pairs01_tradeclosetgt", catalog = "blah", schema = "public")
@NamedQueries({
    @NamedQuery(name = "Pairs01Tradeclosetgt.findAll", query = "SELECT p FROM Pairs01Tradeclosetgt p"),
   @NamedQuery(name = "Pairs01Tradeclosetgt.findByClseRatio", query = "SELECT p FROM Pairs01Tradeclosetgt p WHERE p.clseRatio = :clseRatio")})
public class Pairs01Tradeclosetgt implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    @SequenceGenerator(name="pairs01_tradeclosetgt_id_seq", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "pairs01_tradeclosetgt_id_seq")
    private Integer id;

and my persitence unit:

<?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="testpu" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>mourv02.Pairs01Quotes</class>
    <class>mourv02.Pairs01Pair</class>
    <class>mourv02.Pairs01Trderrs</class>
    <class>mourv02.Pairs01Tradereq</class>
    <class>mourv02.Pairs01Tradeclosetgt</class>
    <class>mourv02.Pairs01Orderstatus</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://192.168.1.80:5432/blah"/>
      <property name="javax.persistence.jdbc.password" value="secret"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="noone"/>
    </properties>
  </persistence-unit>
</persistence>

© Stack Overflow or respective owner

Related posts about java

Related posts about jpa