views:

115

answers:

2

I'm trying to get a batch insert working with Hibernate into Oracle, according to what i've read here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html , but with my benchmarking it doesn't seem any faster than before.

Can anyone suggest a way to prove whether hibernate is using batch mode or not? I hear that there are numerous reasons why it may silently drop into normal mode (eg associations and generated ids) so is there some way to find out why it has gone non-batch?

My hibernate.cfg.xml contains this line which i believe is all i need to enable batch mode:

<property name="jdbc.batch_size">50</property>

My insert code looks like this:

List<LogEntry> entries = ..a list of 100 LogEntry data classes...
Session sess = sessionFactory.getCurrentSession();
for(LogEntry e : entries) {
  sess.save(e);
}
sess.flush();
sess.clear();

My 'logentry' class has no associations, the only interesting field is the id:

@Entity
@Table(name="log_entries")
public class LogEntry {
  @Id @GeneratedValue
  public Long id;
  ..other fields - strings and ints...

However, since it is oracle, i believe the @GeneratedValue will use the sequence generator. And i believe that only the 'identity' generator will stop bulk inserts.

So if anyone can explain why it isn't running in batch mode, or how i can find out for sure if it is or isn't in batch mode, or find out why hibernate is silently dropping back to slow mode, i'd be most grateful.

Thanks

A: 

Solution was to go to log4j and enable debug level logging, then look for the:

AbstractBatcher - Executing batch size: *

line in thelog/console. In my case, it was running in batch mode, it's just that Hibernate is really slow!

Chris
A: 

(...) but with my benchmarking it doesn't seem any faster than before.

Before what? What are you measuring? What did you change? How are we supposed to guess that?

Can anyone suggest a way to prove whether hibernate is using batch mode or not?

Activate logging in DEBUG mode, the o.h.j.BatchingBatcher class - which is an implementation of the Batcher interface that actually uses batching - logs entries like this:

log.debug( "Executing batch size: " + batchSize );

You should maybe consider using the StatelessSession instead.

Pascal Thivent