Use case is:
1. Start transaction
2. Persist other classes (Domain, User, etc) which has @Id @GeneratedValue
3. Persist class Message which has @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "KEYSTORELCALL") @TableGenerator(name = "KEYSTORELCALL", ...)
4. Rollback transaction
Result is that the data in step 2 is still in the database.
I've cranked up logging in both hibernate and the database and I believe this is what's happening:
1. The TableHiLoGenerator is selected for step 3
2. This id generator creates a new transaction (both code and comments confirm his is required)
3. When the commit happens for the id it also commits other data in the session, in this case classes in step 2.
Reasons I think this is true
1. When I change the Message class to use the default strategy (like User) this problem doesn't happen (database is clean after rollback)
2. When I look in the logs in mysql all the data in the session is being commited with the id and only the data persisted after the id is getting rolled back.
Quote:
23 Query /* mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} ) */SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' OR Variable_name = 'init_connect'
23 Query SHOW COLLATION
23 Query SET NAMES latin1
23 Query SET character_set_results = NULL
23 Query SET autocommit=1
090612 11:03:09 23 Query SET autocommit=0
23 Query insert into domain (name, owner) values ('mydomain', null)
23 Query insert into userdb (activeProfile, db, domain_id, guid, lastmodified, login, flag, password, pin) values ('day', 'userdb', 2, '402527e8-50d5-45b8-91b2-5c0500fb3f98', null, 'aLogin', 0, 'aPassword', '1234')
23 Query insert into userdb.useraccountinfo (country, firstname, lastname, nickname, uid) values (null, '', '', '', 3)
23 Query insert into userdb.user (callerid, createdbyappid, createtime, dialingplan, firstname, lastname, name, profeatures, pwdstatus, syncoption, flag, uid) values (null, null, null, '', null, null, '', null, null, 0, 0, 3)
23 Query insert into infradb.transcription_status_code (text) values ('Pending')
23 Query select value from infradb.keystorel where keyid = 'JSECallId' for update
23 Query update infradb.keystorel set value = 2 where value = 1 and keyid = 'JSECallId'
23 Query commit
23 Query insert into userdb.messagelog (duration_in_millis, event_time, external_reference, folder, media_location, message_text, message_type, notes, parent_id, `read`, source, source_device, tags, title, transcribe, transcription_status, urgent, uid, message_id) values (100, '2009-06-12 11:03:09', 'anExternalReference', 'aFolder', 'aMediaLocation', 'aMessageText', 'InboundSms', 'aNotes', null, 0, 'aSource', 'aSourceDevice', 'aTags', 'aTitle', 0, 5, 1, 3, 50)
23 Query insert into userdb.message_destination (carrier, destination, destination_contact_id, destination_device, external_reference, message_id, message_status, normalized_number) values (null, 'destination', 1, 'destinationDevice', null, 50, 'SENT', 'normalizedNumber')
23 Query insert into userdb.message_destination (carrier, destination, destination_contact_id, destination_device, external_reference, message_id, message_status, normalized_number) values (null, 'destination', 1, 'destinationDevice', null, 50, 'SENT', 'normalizedNumber')
23 Query rollback
23 Query SET autocommit=1
23 Quit
Environment info:
Hibernate 3.2.6 GA (I tried with 3.2.7 and it had the same behavior)
Hibernate Annotations 3.3.1 GA
Hibernate EntityManager 3.3.2 GA
Spring 2.5.6
MySql 5.1.34
mysql-connector 5.1.6