简体   繁体   中英

Hibernate query is not returning updated result

In my project, I have two parts UI and Engine. Both are accessing same Database but different hbm.xml files and hibernate connections. If I try to update some table value from UI, the Engine side query does not return updated value.

Is it some general problem? Below is my code. I checked it online and found something about Hibernate Cache. I do not know how to remove it. Please help.

public CgUssdGatewayConf getGwConfig(Long gwId)
{
    logger.info("GW ID : "+gwId);
    Criteria criteria = getSession().createCriteria(CgUssdGatewayConf.class);
    criteria.add(Restrictions.eq("gwId", gwId));
    //Now checking while cache loading time.
    //criteria.add(Restrictions.eq("status", Constants.GW_STATUS_ENABLE));

    List<CgUssdGatewayConf> list = (List<CgUssdGatewayConf>) criteria.list();
    if(list != null && !list.isEmpty())
    {
        CgUssdGatewayConf cgUssdGatewayConf = list.get(0);
        logger.info("GW ID : "+cgUssdGatewayConf.getGwId()+ " :: Name : "+cgUssdGatewayConf.getGwName() + " :: Status : "+cgUssdGatewayConf.getStatus());
        return cgUssdGatewayConf;
    }
    return null;
}

and my hibernate config is-

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <!-- Mysql Config -->
            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/consentgateway</prop>
            <prop key="hibernate.connection.username">cg</prop>
            <prop key="hibernate.connection.password">cg123</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

            <prop key="hibernate.connection.autoReconnect">true</prop>
            <prop key="hibernate.connection.autoReconnectForPools">true</prop>
            <prop key="hibernate.connection.is-connection-validation-required">true</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            <prop key="hibernate.c3p0.min_size">4</prop>
            <prop key="hibernate.c3p0.max_size">50</prop>
            <prop key="hibernate.c3p0.timeout">0</prop>
            <prop key="hibernate.c3p0.max_statements">0</prop>
            <prop key="hibernate.c3p0.idle_test_period">10800</prop>
            <prop key="hibernate.c3p0.acquire_increment">3</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.c3p0.maxAdministrativeTaskTime">0</prop>
            <prop key="hibernate.c3p0.acquireRetryAttempts">5</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>CgUssdGatewayConf.hbm.xml</value>
        </list>
    </property>
</bean>

First level caching is default thing in Hibernate and always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.

for reflecting cached queries's effect Hibernate Transaction needs to be committed.

I don't know whether you have committed or not but check for it.

Transaction trnsction = session.beginTransaction();

update queries

transaction.commit();

The first level cache cannot be disabled at all. However you can evict the object from the session before loading it. As the result the object will be reread from the database.

Session.evict(entity);

Or refresh object. Also loads the latest object state from the database and rewrites the existing one in the session.

Session.refresh(entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM