users@glassfish.java.net

JPA2 usage problem

From: emiddio-frontier <emiddio_at_frontier.com>
Date: Wed, 23 Feb 2011 15:45:00 -0800

i am learning JPA -- an example from Core JavaServer Faces 3rd edition,
with JPA2.0 examples
is not reading the database before each update -- it does the first time.

a 2nd app using JDBC from the same book -- does read the database each
time the query
is submitted.

both web applications do the same thing functionally

there are 4 versions of the Application in the book, jdbc, jpa, jpa with
stateless session bean, jpa
with stateful session bean.

all 4 web apps read a row from the db selected by "user", read a
"password" field and if the password
check passes, update the "login count" field.

when running any of the 4 web apps -- while having different apps open
in different web browser tabs,
each successful login increments the loginCount column field-- as it
should. i use netbeans and sql queries
to see the database values after each web page click. using Derby with
netbeans.

But only the jdbc app will always "see" the updated loginCount field
before it updates it -- while the JPA apps
only see "their" last updated value -- it seems JPA is using some kind
of cache and not reading the db when told
to with the query command -- as if assuming only1 app (itself) is using
the db.

again -- i am learning but have been unable to find any info in books
i've been studying addressing
this particular issue. -- Core JavaServer Faces 3rd edition, Beginning
Java EE6 Platform with Glassfish V3, or
The Java EE6 Tutorial Basic Concepts 4th edition.

code that does update in JPA snipped below

public void doLogin() throws NotSupportedException, SystemException,
             RollbackException, HeuristicMixedException,
HeuristicRollbackException {
         EntityManager em = emf.createEntityManager();
         try {
             utx.begin();
             em.joinTransaction();
             boolean committed = false;
             try {
                 Query query = em.createQuery(
                         "SELECT c FROM Credentials c WHERE c.username =
:username").
                         setParameter("username", name);
                 @SuppressWarnings("unchecked")
                 List<Credentials> result = query.getResultList();

                 if (result.size() == 1) {
                     Credentials c = result.get(0);
                     if (c.getPasswd().equals(password)) {
                         loggedIn = true;
                         count = c.incrementLoginCount();
                         //geb
                         em.merge(c);
                     }
                 }
                 utx.commit();
                 committed = true;
             } finally {
                 if (!committed) {
                     utx.rollback();
                 }
             }
         } finally {
             em.close();
         }
     }

and

  public int incrementLoginCount() {
         loginCount++;
         return loginCount;
     }
contained in the Entity class Credentials.

can someone help educate me on what i'm not understanding here.

thanks

gary