-->
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.  [ 6 posts ] 
Author Message
 Post subject: Insert not visible from session
PostPosted: Tue Feb 02, 2010 8:17 am 
Newbie

Joined: Mon Dec 28, 2009 9:34 am
Posts: 4
Hi,

I'm having a problem when working with threadpools. An insert is executed in the database, session is closed after that, and another thread (that executes AFTER the mentioned session is closed) with his own session tries to read the row inserted, by id. But I get no results for the mentioned id...

If I execute an SQL query in the DB, the row appears to be inserted correctly

I found no references to this issue.
I'm using hibernate 3 with mySQL.

The config file is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <!-- URL Base de datos -->
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cardalesDesa?autoReconnect=true</property>
      <property name="hibernate.connection.username">user</property>
      <property name="hibernate.connection.password">*****</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


      <!-- Use the C3P0 connection pool.  -->
      <!-- c3p0 config http://www.hibernate.org/214.html -->
      <property name="c3p0.acquire_increment">1</property>
      <property name="c3p0.idle_test_period">100</property> <!-- seconds -->
      <property name="c3p0.max_size">100</property>
      <property name="c3p0.max_statements">0</property>
      <property name="c3p0.min_size">10</property>
      <property name="c3p0.timeout">100</property> <!-- seconds -->


      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>

      <!-- Disable the second-level cache  -->
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

      <!-- Echo all executed SQL to stdout -->
      <property name="show_sql">true</property>
      <property name="hibernate.format_sql">true</property>

      <!-- Drop and re-create the database schema on startup -->
      <property name="hbm2ddl.auto">update</property>
   </session-factory>
</hibernate-configuration>



Thanks in Advance.

Rodrigo


Top
 Profile  
 
 Post subject: Re: Insert not visible from session
PostPosted: Tue Feb 02, 2010 8:55 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
When did the session of the second thread start? Maybe it's state was initialized before the first thread updated the database, i.e. with a stale state?

Have you tried running a session.clear() before searching for the insert row?


Top
 Profile  
 
 Post subject: Re: Insert not visible from session
PostPosted: Tue Feb 02, 2010 9:32 am 
Newbie

Joined: Mon Dec 28, 2009 9:34 am
Posts: 4
Yes, I've tried it, and the problem still appears. This would be the code.

Code:
private static final ThreadLocal<Session> session = new ThreadLocal<Session>();

public static synchronized Session getSession() throws HibernateException {
    if (sessionFactory == null) {
   init();
    }
    Session s = session.get();
    if (s == null || !s.isOpen()) {
         s = sessionFactory.openSession();
         session.set(s);
    }
    s.clear();
    return s;
}


Top
 Profile  
 
 Post subject: Re: Insert not visible from session
PostPosted: Wed Feb 03, 2010 7:01 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Do you commit also the transaction which does the insert?

Maybe, If you execute the SQL query in the DB, you are using Dirty-Read isolation level,
whilst in your Hiberante application you use Commited_Read isolation level?


Top
 Profile  
 
 Post subject: Re: Insert not visible from session
PostPosted: Wed Feb 03, 2010 7:55 am 
Newbie

Joined: Mon Dec 28, 2009 9:34 am
Posts: 4
Thanks for the answer pb00067.

Yes, the transaction is committed, and the session closed (the one which generated the insert).

Lets say, as an example, that the code is something like this:

Code:

// This operation does the insert, opening a new session, creating the object,
// saving, and committing. After that, the id generated for the object, is sent to
// the next method.
long id = createNewObject(data);

// This method publishes a task in a ThreadPool, but when the thread that gets the task,
// tries to access the object inserted, by 'id', its not there.
publishTask(id);



The strange thing is that sometimes it works, and sometimes not. If I put a sleep() between the two operations, lets say of 500 ms, it works ok, so apparently is like there is a delay AFTER the commit for the data to become 'visible'.

Any other ideas?


Top
 Profile  
 
 Post subject: Re: Insert not visible from session
PostPosted: Wed Feb 03, 2010 9:05 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to activate jdbc-statement logging with P6Spy (http://www.p6spy.com/ it is free and open source),
so we will see what exactly (and in which sequence) in therms of sql is executed against the database...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.