0

Technologies: EJB 3.1, JSF, JBoss 7.1.1

There is the project with working CRUD. Problem:

In one browser the table with list of entities is opened. In another is too. Each table are up to date. One entity is edited and persist. In DB we can see correct data state.

In another browser we make refresh and reload data, but with out last change. On screen are previous data (data is not up to date). But after log in/log out we can see last edited data (data is up to date).

I think the reason is JPA - caching.

I need an advise how get an actual data from DB during current session.

Pesistense.xml

 <persistence-unit name="MyEntityManager">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <jta-data-source>java:jboss/datasources/MydataSource</jta-data-source>
        <properties>

            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.dialect"value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.transaction.manager_lookup_class"
                      value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
            <property name="hibernate.connection.autocommit" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="utf8"/>

            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.timeout" value="1800"/>
            <property name="hibernate.c3p0.max_statements" value="50"/>

        </properties>

    </persistence-unit>

DAO -class

@Named("userDao")
@Stateful
@TransactionManagement(TransactionManagementType.BEAN)
public class UserDaoBean implements UserDAO, Serializable {

    private static final long serialVersionUID = -2486674295685504650L;

    @PersistenceContext(name = "MyEntityManager", type = PersistenceContextType.EXTENDED)
    private EntityManager em;

   private User getUser(String user) {
        try {
            return (User) em.createNamedQuery(Queries.GET_User).setParameter("User", user.No).getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    @Override

    public void addUser(User user) throws Exception {
        em.persist(user);
        em.flush();
    }

    @Override
    public void update(User user) throws Exception {
        User tmpUser = getUser(user.getUser());
        if (tmpUser == null) {
            addUser(user);
            return;
        }
        tmpUser.setUser(user.getUser());
        tmpUser.setUsere(user.getUser());
        em.persist(tmpUser);
        em.flush();
    }

I tried add extra

 userTransaction.begin();
  //removal or updating
  userTransaction.commit();

and em.flush(); but it doen't work.

Thanks for in advance!

Olga
  • 3,705
  • 1
  • 20
  • 23

1 Answers1

1

You're using an extended persistence context (type = PersistenceContextType.EXTENDED). This is what the spec says about them:

A container-managed extended persistence context can only be initiated within the scope of a stateful session bean. It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound to the stateful session bean. The dependency on the extended persistence context is declared by means of the PersistenceContext annotation or persistence-context-ref deployment descriptor element. The persistence context is closed by the container when the @Remove method of the stateful session bean completes (or the stateful session bean instance is otherwise destroyed).

So, the first-level cache of this persistence context is kept alive for the life-time of your stateful session bean. I see no reason to use an extended persistence context in this case. Use a stateless session bean, and a normal persistence context, and the user will be reloaded from the DB instead of being reloaded from the first-level cache of the persistence context.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255