-->
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.  [ 12 posts ] 
Author Message
 Post subject: READ_UNCOMMITED not working. Please help
PostPosted: Tue Nov 24, 2009 9:15 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
Hi there!

I've been looking for a solution for this, on the Internet and in the board but I haven't found it so I'd really apreciate if anyone could give me any hint.

I need to make an application work with READ_UNCOMMITED isolation but it ignores that config (as the second transaction never reads uncommited data). The flow would be:

(I'm debugging the application to test it)

Step a) T1:Gets obj1
Step b) T1:Updates obj1
Step c) T2:Gets obj1
Step d) T1:commits changes to obj1.

hibernate.cfg.xml:

Code:
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/banco2</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">admin</property>
      <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
      <property name="hibernate.connection.isolation">1</property>


Transaction1:

Code:
public static void main( String[] args )
    {
       SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction trans = session.beginTransaction();
        //Step a)
        Cuentabancaria cuenta =(Cuentabancaria)session.get("com.josesa.hibernate.Cuentabancaria", new Integer(61));
        //Step b)
        cuenta.setDinero(new Float(cuenta.getDinero().floatValue()+100));
        logger.debug(cuenta.getDinero());
        //Step d)
        trans.commit();
        session.close();
       
    }


Transaction 2:
Code:
public static void main( String[] args )
    {
       SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction trans = session.beginTransaction();
        //Step c)
        Cuentabancaria cuenta =(Cuentabancaria)session.get("com.josesa.hibernate.Cuentabancaria", new Integer(61));
        logger.debug(cuenta.getDinero());
       
        trans.commit();
        session.close();
       
    }


I've done the same example using plain JDBC and it works... any idea?? (BTW my DB is MySQL5)

Jose


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Tue Nov 24, 2009 11:51 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
I've found something interesting (maybe some guy who knows Hibernate's inside could explain...):

In T1 I add one new line: I flush the session but I stop the execution before commiting:

Code:
cuenta.setDinero(new Float(cuenta.getDinero().floatValue()+100));
        logger.debug(cuenta.getDinero());
        //Step d)
        session.flush();
        trans.commit();


Now T2 does read the uncommited value and if I change the isolation level it works ok.

Then, instead of using session.flush() I've tried changing the FlushMode of the session (session.setFlushMode()...) using the values AUTO (the default) and ALWAYS. However neither worked.

1) Isn't there any automatic way of letting another transaction read uncommited values?
2) Is is always neccesary to flush the session to allow that?
3) What's the point on letting the user set de isolation level if your configuration won't work unless you manually flush the session in the middle of the transaction?
4) This also makes useless using Spring's annotations such as @Transactional(isolation=Isolation.READ_UNCOMMITTED) since it depends on having manually flushed the session in the other transaction.

Jose


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 7:26 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
I've also tried launching another thread with a new Transaction within T1 so that they share the same SessionFactory, just in case it was something related to that, but I got the same behaviour (Unless T1 flushes the session, T1-subthread won't notice the change).

What do you think?


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 7:35 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
What kind of db-engine do your tables use? InnoDB?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 7:49 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
That's it, innoDB, as far as I know it supports ACID transactions...


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 7:58 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, just because you change a property of your entity hibernate does not immediately fire an update statement, only your in memory object changes. So there is no uncomitted state change in the db, which another transaction could read. If you flush the session statements are issued, so you now can read uncommitted state. you could enhance your test by updating the value via HQL instead of your setter. that way the statement is fired immediately.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 8:30 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
Hi mmerder,

Firts of all, thanks for your answer. You were right, using a Query the second thread immediately reads the uncommited value.

However... this doesn't solve my problem completely. The actual environment in which I'm using this code is a Spring MVC webapp. So as you can guess I'm using the HibernateTemplate.

The point is that I could always use a Query to make it work, but I'd prefer to take advantage of the Spring annotations & Hibernate session management provided by the template. Do you know if there's any way of making it work using HibernateTemplate?

Thanks again,

Jose


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 8:39 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
What do you need that behaviour for? I mean uncommitted read is a very dirty thing. If you want to share object state within your application think of using a cache.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 8:55 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
Hi again,

Yeah, I know it's not very advisable to use that he he. The point is that I need that diffent users notice the changes (although they aren't uncommited) which are being done by another user.

I've never used Hibernate's cache mechanisms such as EHCache... do you think that I could do something like that using it? I hope it's not difficult to configure - this transaction issue is going to make my brain blow up :-)

Jose


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 9:28 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Yes, have a look at caching.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 11:37 am 
Newbie

Joined: Tue Nov 24, 2009 8:54 am
Posts: 7
Unfortunately, it does the same, if I don't flush the session it keeps seeing unchaged data despite the second level cache. That makes HibernateTemplate useless...any other idea?

.cfg.xml:

Code:
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache ">true</property>


class mapping:

Code:
<cache usage="read-only" />


I've tried both "read-only" and "read-write".

Debugging I can see that the cache isn't updated until I commit the transaction


Top
 Profile  
 
 Post subject: Re: READ_UNCOMMITED not working. Please help
PostPosted: Wed Nov 25, 2009 5:54 pm 
Beginner
Beginner

Joined: Thu Jun 21, 2007 9:24 pm
Posts: 20
Location: Lansing, Michigan, USA
HibernateRulez wrote:
Yeah, I know it's not very advisable to use that he he. The point is that I need that diffent users notice the changes (although they aren't uncommited) which are being done by another user.


The problem is that what you're trying to do breaks the whole concept of a transaction, which is bad ... especially for bank-accounts!

What about using JMS messaging to distribute information about pending transactions?

Another possibility would be an in-memory Map from account ID to "uncommitted" account balance; although just using a Hashtable would probably be a performance bottleneck, and wouldn't scale to multiple server instances.


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