-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: insert and select of the same data in the same session
PostPosted: Mon Jan 28, 2013 2:06 pm 
Newbie

Joined: Mon Jan 28, 2013 1:54 pm
Posts: 2
Hi,
i have a problem. if I run this code:

Code:
public void insertContext(final int time, final int day, final String weather, final String events) {
       this.getHibernateTemplate().execute(
            new HibernateCallback() {
               public Object doInHibernate(Session session)
                     throws PersistenceException {
                  session.flush();
               
                  String sqlString = new StringBuffer("insert into context (time_slot, day, weather,events) values (").append(time).append(",").append(day).append(",'").append(weather).append("','").append(events).append("');").toString();
                                 
                          System.out.println(sqlString);

                  Query qry = session.createSQLQuery(sqlString);
                  qry.executeUpdate();
               
                  return null;


and then execute this code:

Code:
public boolean existContext(final int time, final int day) {
      return (boolean) this.getHibernateTemplate().execute(
            new HibernateCallback() {

               public Object doInHibernate(Session session)
                     throws PersistenceException {

                  session.flush();
                  String sqlString = new StringBuffer("select count(*) from context where time_slot=").append(time).append(" and day=").append(day).append(";").toString();
                  System.out.println(sqlString);

                  Query qry = session.createSQLQuery(sqlString).addScalar("count", Hibernate.INTEGER);
                  qry.setCacheable(true);
                  List result = (List)qry.setResultTransformer(Transformers.TO_LIST).list().get(0);
                  int count = (Integer)result.get(0);
                  System.out.println(count+"<------------------------count");
                  if (count==0) {
                  ...


if everything is performed in the same run (JAVA) and in the same session, the data entered by the first query is not found in the second query. why?


Last edited by dicero on Thu Jan 31, 2013 9:36 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: insert and select of the same data in the same session
PostPosted: Thu Jan 31, 2013 4:58 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Are you sure that both operations (insert-query, select-query) are acting within the same session?
Eventually I suggest you to log all jdbc-activities with a JDBC-Logger like p6spy, so you can effectively check if it happens is different sessions (transactions) or not.
If it happens in different sessions, then the select-query might not see the inserted records because of Isolation-settings (READ_COMMITED or higher) due to the fact that the inserted data isn't commited yet.


Top
 Profile  
 
 Post subject: Re: insert and select of the same data in the same session
PostPosted: Thu Jan 31, 2013 8:05 am 
Newbie

Joined: Mon Jan 28, 2013 1:54 pm
Posts: 2
pb00067 wrote:
Are you sure that both operations (insert-query, select-query) are acting within the same session?
Eventually I suggest you to log all jdbc-activities with a JDBC-Logger like p6spy, so you can effectively check if it happens is different sessions (transactions) or not.
If it happens in different sessions, then the select-query might not see the inserted records because of Isolation-settings (READ_COMMITED or higher) due to the fact that the inserted data isn't commited yet.


thanks for the reply. I installed p6spy and gave me back the log file. But how do I know if it uses the same session?
these are the strings of my interest:
Code:
1359633434916|2|1|statement|select count(*) from context where time_slot=34 and day=0;|select count(*) from context where time_slot=34 and day=0;
1359633434916|-1||resultset|select count(*) from context where time_slot=34 and day=0;|count = 0
1359633434922|3|2|statement|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;
1359633434922|-1||resultset|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|
1359633434950|26|3|statement|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;
1359633434951|-1||resultset|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|
1359633434955|2|1|statement|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;
1359633434956|-1||resultset|insert into context (time_slot, day, weather,events) values (34,0,'sunny','nothing') returning id;|

As you can see the insert of the same context is executed multiple times.
In practice, before I do the insert I check if the entry is already present with the "count" and if "count == 0" proceed with the insert but if i do all this in the same run java the count is always ==0.

PS: i have found a solution. If I set this string <property name="hibernate.cache.use_query_cache">true</property> to false in the hibernate configuration the problem is solved but the execution time increases considerably. Can I disable the cache only for a specific query?


Top
 Profile  
 
 Post subject: Re: insert and select of the same data in the same session
PostPosted: Mon Feb 04, 2013 3:09 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
But how do I know if it uses the same session?


The third parameter in the p6spy-log-line shows the session-id.
Looking at your p6-spy output, we can see that 3 different sessions (1,2,3) are used to do the operations.
Instead to disable the query-cache. I would try to get all working with one unique session.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.