ObjectDB ObjectDB

Query can't see recently persisted object

#1

When using container managed transactions (EJB) in Payara 4.1.3 with ObjectDB embedded, ObjectDB does not appear to present a read consistent view of persisted objects within the same transaction. e.g. in the following code the NoResultException is correctly thrown on the first occasion and incorrectly thrown for the same "oid" query parameter on subsequent occasions within the same transaction. However if I call flush() on the EntityManager after the persist() the object will be found.

Clearly I don't want to call flush() on the EntityManager, as I want the container to manage the transaction. Is there a way of using or configuring ObjectDB to ensure read consistency of objects persisted within the same container transaction? 


@NamedQueries(
  {
    @NamedQuery(
      name = "MyQuery.findByOid",
      query = "select m from MyClass m where m.oid = :oid"
    )
  }
)

@Indices(
  {
    @Index(
      name = "MyClass_ind_1",
      members = { "oid" },
      unique = "true"
    )
  }
)

@Entity public class MyClass {
  private @Id Integer id;

  private String oid;
..
  public MyObject(String oid) {
    super();
    this.oid = oid;
  }
...
}

...
    TypedQuery<MyObject> query = mem.createNamedQuery("MyObject.findByOid", MyObject.class);
    query.setParameter("oid", oid);

    if ("-62e6083d:1612795a01c:-6e65".equals(oid)) {
      logger.debug("here");
    }

    MyObject myObject;

    try {
      myObject = query.getSingleResult();
    } catch (NoResultException ex) {
      myObject = new MyObject(oid);
      mem.persist(myObject);

      // Flush is required for query to find object 
      // mem.flush();
    }

...
edit
delete
#2

You can change default flush mode on the EntityManager level or on the Query level.

See this manual page.

ObjectDB Support
edit
delete

Reply

To post on this website please sign in.