-->
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: Hibernate performance sanity check
PostPosted: Wed Aug 15, 2007 9:32 am 
Newbie

Joined: Mon Aug 13, 2007 10:00 am
Posts: 8
I'm currently evaluating Hibernate as an option for a project and I've developed a sample application to get me through the learning curve as quickly as possible and also so I can measure some basic performance and scalability info. I'm seeing poor performance with Hibernate at the moment compared to plain JDBC DAO code and I wanted to check that I'm doing everything correctly.

Perhaps there are settings I need to tweak to get the best performance out of Hibernate?

Here's a sample method from the Hibernate version of my application's stateless session bean.

Code:
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public int createOrderItem(int orderId, int bookId) throws BookStoreException {
        Session session = null;
        try {
            // start transaction
            session = sessionFactory.openSession();
            // lookup customer order
            CustomerOrder o = (CustomerOrder) session.get(CustomerOrder.class, orderId);
            // lookup book
            Book b = (Book) session.get(Book.class, bookId);
            // create order item
            OrderItem item = new OrderItem();
            item.setCustomerOrder(o);
            item.setBook(b);
            item.setUnitPrice(b.getPrice());
            item.setQuantity(1);
            session.save(item);
            // update total amount on order
            o.setTotalAmount(o.getTotalAmount().add(item.getUnitPrice()));
            session.update(o);
            return o.getId();
        } catch (Exception e) {
            throw new BookStoreException(e);
        }
        finally {
            if (session != null) {
                session.close();
            }
        }
    }



And here's the JDBC DAO version of the same bean method:

Code:
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public int createOrderItem(int orderId, int bookId) throws BookStoreException {
        Connection conn = null;
        try {
            conn = ds.getConnection();
            // lookup order
            CustomerOrder o = CustomerOrderDaoFactory.create(conn).findByPrimaryKey(orderId);
            // lookup book
            Book b = BookDaoFactory.create(conn).findByPrimaryKey(bookId);
            // create order item
            OrderItem item = new OrderItem();
            item.setOrderId(orderId);
            item.setBookId(bookId);
            item.setUnitPrice(b.getPrice());
            item.setQuantity(1);
            OrderItemDaoFactory.create(conn).insert(item);
            // update total amount on order
            o.setTotalAmount(o.getTotalAmount().add(item.getUnitPrice()));
            CustomerOrderDaoFactory.create(conn).update(o.createPk(), o);
            return o.getId();
        } catch (Exception e) {
            throw new BookStoreException(e);
        }
        finally {
            close(conn);
        }
    }



I'm running both beans in the same JBoss server using the same DataSource. I'm assuming that this means both versions of the bean are using the exact same connection pool implementation behind the scenes so I don't currently understand why I'm seeing a large difference in performance between the two approaches.

My hibernate config is:

Code:
<hibernate-configuration>
  <session-factory>
    <property name="connection.datasource">java:/BookStoreDataSource</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">false</property>
  </session-factory>
</hibernate-configuration>



The data source is defined as:

Code:
<datasources>
  <local-tx-datasource>
    <jndi-name>BookStoreDataSource</jndi-name>
    <connection-url>jdbc:mysql://192.168.1.15/bookstore</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>test</user-name>
    <password>password</password>
  </local-tx-datasource>
</datasources>

_________________
Thanks,

Andy Grove


Last edited by andygrove on Wed Aug 15, 2007 1:09 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 11:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is called "A useless microbenchmark" and has been addressed a thousand times.

http://www.hibernate.org/15.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 12:04 pm 
Newbie

Joined: Mon Aug 13, 2007 10:00 am
Posts: 8
Hi Christian,

I'm afraid I have to disgree with that. I'm running the tests on a dedicated test lab with 3 quad-core machines and with a real-world size database (many millions of rows in the main tables).

I'm simulating a real-world business process with many concurrent clients.

I'm actually trying to find out how to make Hibernate scale well - I presume that is something that the community would be interested in helping me with?

_________________
Thanks,

Andy Grove


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So provide the necessary information and learn how to use code tags to format your messages.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 12:40 pm 
Newbie

Joined: Mon Aug 13, 2007 10:00 am
Posts: 8
OK. I've read through the link you provided. I understand your reference to micro benchmarks. However, the business process I am modelling does not fall into that category so I felt I should explain further.

I have been asked to recommend a persistence technology/approach for a web-based solution for a retail business that needs to scale well.

I'm interested in scalability rather than raw performance. I'm basically looking to validate that hibernate scales linearly (or close to) as the number of concurrent transactions increases.

The benchmark I am putting together is based on the actual business process that the solution needs to provide i.e. customer logging in, browsing products, adding products to cart etc. It's not just an insert. The method I provided in my original post is just an example of how I have implemented each of the methods in my session bean.

I expect I have something wrong in my configuration or the approach I'm taking and that's what I would like guidance on.

I hope that clarifies what I'm trying to achieve.

_________________
Thanks,

Andy Grove


Top
 Profile  
 
 Post subject: Micro-benchmarking
PostPosted: Sun Oct 14, 2007 6:01 am 
Newbie

Joined: Fri Apr 02, 2004 2:34 am
Posts: 8
Location: Germany
Hello Andy,

Please understand Crhistian's ignorance. The micro-benchmarking is not the benchmark executed on single PC, but benchmark executed on single oversimplified usecase.
Even if you would do it in the 300 PC cluster, it will still be a oversimplification because you operate here on 2 domain objects and your DAO operates on primary keys.

While it is definitely a valid case, it is not very valuable for Hibernate (and for majority of complex real-life systems) because usually we have a complex object tree or tree hierarchies that involve complex relations like M-to-M and inheritance. In case you have a plain structure it's definitely simpler and faster to use plain old JDBC, and to be honest there is not much Hibernate can help you there, threrefore it's only slows the application.

In case you would have a complex object tree and complex mappings, and you would also need to benifit from the 1-st and 2nd level caching then you would see why do people are using Hibernate, and where it is helpful.

As a tip you can think about lazy loading approaches in complex object tree and how much helpful it can be.

_________________
Br.
Renat


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.