-->
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: Concurrent blocking question
PostPosted: Wed Aug 16, 2006 5:31 am 
Newbie

Joined: Wed Aug 16, 2006 4:58 am
Posts: 9
Hi,
I was just wondering about the nature of Hibernate. If I wrote the following code:

Code:
class SimpleDAO
{
    private Session session;
    private Transaction tx;

    public void create(Event event) throws DataAccessLayerException {
        try {
            startOperation();
            session.save(event);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }

...

    public static void main(String[] args)
    {
        SimpleEventDao eventDao = new SimpleEventDao();
        Event event1 = new Event();
        event1.setName("Events 1");

        eventDao.create(event1);

        Event event2 = new Event();
        event1.setName("Events 2");

        eventDao.create(event2);
    }
    class Event
    {
      private String name;
      public void setName(String _name){ this.name = _name; }
    }
}


(Please excuse the code, I haven't started using the Hibernate API yet, so it's probably very rough around the edges, if it indeed has any edges).

If for some reason creating each Event object took say 10 seconds, would hibernate wait for each object to finish being created before starting on the next one, or would it do some clever concurrency things?


Top
 Profile  
 
 Post subject: How I think about it
PostPosted: Wed Aug 16, 2006 7:29 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 7:04 pm
Posts: 26
Location: Denver, CO
It's a simplification, but I think of a session as being the same as a JDBC connection that merges updates.

So if you have a single session (like your sample code) that creates two objects to be stored, you will have to inserts. Under your numbers, it would take 2 seconds.

Now lets say that updating and reading a record also takes 1 second. The following code:
Code:
start TX at the top
function();
function();
function();
end TX

function() {
load object 1
update object 1
}


would take two seconds: one to read the object and one to update the object. This is were hibernate shines.

Also, if you are in a web application, you generally create a session per web request allowing many concurrent operations to happen at the same time.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 5:14 am 
Newbie

Joined: Wed Aug 16, 2006 4:58 am
Posts: 9
Excellent, thanks for that jliptak, that's really useful!

Can concurrent sessions also roll up those updates in that manner?

So say if I had three sessions each performing the same load and update function you described would it take 2 seconds or 6?


Top
 Profile  
 
 Post subject: depends on the database
PostPosted: Thu Aug 17, 2006 9:31 am 
Beginner
Beginner

Joined: Thu Dec 09, 2004 7:04 pm
Posts: 26
Location: Denver, CO
if the database is in serialized mode, it would take 6, otherwise it could take 2.

Now if the transactions are trying to update the same instances (i.e. rows in the database) you may get an exception on concurrent updates.


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.