About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

Hibernate Identity, Sequence and Table (Sequence) generator

Introduction

In my previous post I talked about different database identifier strategies. This post will compare the most common surrogate primary key strategies:

  • IDENTITY
  • SEQUENCE
  • TABLE (SEQUENCE)

 
 

IDENTITY

The IDENTITY type (included in the SQL:2003 standard) is supported by:

The IDENTITY generator allows an integer/bigint column to be auto-incremented on demand. The increment process happens outside of the current running transaction, so a roll-back may end-up discarding already assigned values (value gaps may happen).

The increment process is very efficient since it uses a database internal lightweight locking mechanism as opposed to the more heavyweight transactional course-grain locks.

The only drawback is that we can’t know the newly assigned value prior to executing the INSERT statement. This restriction is hinderingthe “transactional write behind” flushing strategy adopted by Hibernate. For this reason Hibernates disables the JDBC batch support for entities using the IDENTITY generator.

For the following examples we’ll enable Session Factory JDBC batching:

properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
properties.put("hibernate.jdbc.batch_size", "2");

Let’s define an Entity using the IDENTITY generation strategy:

@Entity(name = "identityIdentifier")
public static class IdentityIdentifier {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
}

Persisting 5 entities:

doInTransaction(new TransactionCallable<Void>() {
	@Override
	public Void execute(Session session) {
		for (int i = 0; i < 5; i++) {
			session.persist(new IdentityIdentifier());
		}
		session.flush();
		return null;
	}
});

Will execute one query after the other (there is no JDBC batching involved):

Query:{[insert into identityIdentifier (id) values (default)][]} 
Query:{[insert into identityIdentifier (id) values (default)][]} 
Query:{[insert into identityIdentifier (id) values (default)][]} 
Query:{[insert into identityIdentifier (id) values (default)][]} 
Query:{[insert into identityIdentifier (id) values (default)][]}

Aside from disabling JDBC batching, the IDENTITY generator strategy doesn’t work with the Table per concrete class inheritance model, because there could be multiple subclass entities having the same identifier and a base class query will end up retrieving entities with the same identifier (even if belonging to different types).

SEQUENCE

The SEQUENCE generator (defined in the SQL:2003 standard) is supported by:

A SEQUENCE is a database object that generates incremental integers on each successive request. SEQUENCES are much more flexible than IDENTIFIER columns because:

  • A SEQUENCE is table free and the same sequence can be assigned to multiple columns or tables
  • A SEQUENCE may preallocate values to improve performance
  • A SEQUENCE may define an incremental step, allowing us to benefit from a “pooled” Hilo algorithm
  • A SEQUENCE doesn’t restrict Hibernate JDBC batching
  • A SEQUENCE doesn’t restrict Hibernate inheritance models

Let’s define a Entity using the SEQUENCE generation strategy:

@Entity(name = "sequenceIdentifier")
public static class SequenceIdentifier {
	@Id
	@GenericGenerator(name = "sequence", strategy = "sequence", parameters = {
			@org.hibernate.annotations.Parameter(name = "sequenceName", value = "sequence"),
			@org.hibernate.annotations.Parameter(name = "allocationSize", value = "1"),
	})
	@GeneratedValue(generator = "sequence", strategy=GenerationType.SEQUENCE)
	private Long id;
}

I used the “sequence” generator because I didn’t want Hibernate to choose a SequenceHiLoGenerator or a SequenceStyleGenerator on our behalf.

Adding 5 entities:

doInTransaction(new TransactionCallable<Void>() {
	@Override
	public Void execute(Session session) {
		for (int i = 0; i < 5; i++) {
			session.persist(new SequenceIdentifier());
		}
		session.flush();
		return null;
	}
});

Generate the following queries:

Query:{[call next value for hibernate_sequence][]} 
Query:{[call next value for hibernate_sequence][]} 
Query:{[call next value for hibernate_sequence][]} 
Query:{[call next value for hibernate_sequence][]} 
Query:{[call next value for hibernate_sequence][]} 
Query:{[insert into sequenceIdentifier (id) values (?)][1]} {[insert into sequenceIdentifier (id) values (?)][2]} 
Query:{[insert into sequenceIdentifier (id) values (?)][3]} {[insert into sequenceIdentifier (id) values (?)][4]} 
Query:{[insert into sequenceIdentifier (id) values (?)][5]}

This table the inserts are batched, but we know have 5 sequence calls prior to inserting the entities. This can be optimized by using a HILO algorithm.

TABLE (SEQUENCE)

There is another database independent alternative to generating sequences. One or multiple tables can be used to hold the identifier sequence counter. But it means trading write performance for database portability.

While IDENTITY and SEQUENCES are transaction-less, using a database table mandate ACID, for synchronizing multiple concurrent id generation requests.

This is made possible by using row-level locking which comes at a higher cost than IDENTITY or SEQUENCE generators.

The sequence must be calculated in a separate database transaction and this requires the IsolationDelegate mechanism, which has support for both local (JDBC) and global(JTA) transactions.

  • For local transactions, it must open a new JDBC connection, therefore putting more pressure on the current connection pooling mechanism.
  • For global transactions, it requires suspending the current running transaction. After the sequence value is generated, the actual transaction has to be resumed. This process has its own cost, so the overall application performance might be affected.

Let’s define a Entity using the TABLE generation strategy:

@Entity(name = "tableIdentifier")
public static class TableSequenceIdentifier {

	@Id
	@GenericGenerator(name = "table", strategy = "enhanced-table", parameters = {
			@org.hibernate.annotations.Parameter(name = "table_name", value = "sequence_table")
	})
	@GeneratedValue(generator = "table", strategy=GenerationType.TABLE)
	private Long id;
}

I used the newer “enhanced-table” generator, because the legacy “table” generator has been deprecated.

Adding 5 entities:

doInTransaction(new TransactionCallable<Void>() {
	@Override
	public Void execute(Session session) {
		for (int i = 0; i < 5; i++) {
			session.persist(new TableSequenceIdentifier());
		}
		session.flush();
		return null;
	}
});

Generate the following queries:

Query:{[select tbl.next_val from sequence_table tbl where tbl.sequence_name=? for update][default]} 
Query:{[insert into sequence_table (sequence_name, next_val)  values (?,?)][default,1]} 
Query:{[update sequence_table set next_val=?  where next_val=? and sequence_name=?][2,1,default]} 
Query:{[select tbl.next_val from sequence_table tbl where tbl.sequence_name=? for update][default]} 
Query:{[update sequence_table set next_val=?  where next_val=? and sequence_name=?][3,2,default]} 
Query:{[select tbl.next_val from sequence_table tbl where tbl.sequence_name=? for update][default]} 
Query:{[update sequence_table set next_val=?  where next_val=? and sequence_name=?][4,3,default]} 
Query:{[select tbl.next_val from sequence_table tbl where tbl.sequence_name=? for update][default]} 
Query:{[update sequence_table set next_val=?  where next_val=? and sequence_name=?][5,4,default]} 
Query:{[select tbl.next_val from sequence_table tbl where tbl.sequence_name=? for update][default]} 
Query:{[update sequence_table set next_val=?  where next_val=? and sequence_name=?][6,5,default]} 
Query:{[insert into tableIdentifier (id) values (?)][1]} {[insert into tableIdentifier (id) values (?)][2]} 
Query:{[insert into tableIdentifier (id) values (?)][3]} {[insert into tableIdentifier (id) values (?)][4]} 
Query:{[insert into tableIdentifier (id) values (?)][5]}

The table generator allows JDBC batching but it resorts to SELECT FOR UPDATE queries. The row level locking is definitely less efficient than using a native IDENTITY or SEQUENCE.

So, based on your application requirements you have multiple options to choose from. There isn’t one single winning strategy, each one having both advantages and disadvantages.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


one × 8 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books