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>