Question

I am encountering sequencing issues when writing BDD tests with our API (Spring+Hibernate). We use JBehave as our BDD runner and a HSQL db.

Basically I have created the following case to explain the issue I am encountering.

I have two stories. Both stories do inserts to a table called DUMMYPRODUCT. We specify that before each scenario we clear all data and reset the schema using the following,

"TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK"

And then we reinitialise Table Data using a predefined sql script which includes 2 inserts to DUMMYPRODUCT,

And Resets the sequence (supposedly) using

DROP SEQUENCE DUMMYPRODUCT_SEQ; CREATE SEQUENCE DUMMYPRODUCT_SEQ START WITH 3 INCREMENT BY 10;

But I am encountering extremely strange behaviour as the sequences are not reset between both stories and scenarios. Instead they seem to continue on to the next story/scenario and then reset within,i.e. not going into the next block of sequences, causing the constraint violation to occur.

exception=java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10216 table: DUMMYPRODUCT

Behaviour experienced inserts into DUMMYPRODUCT for DUMMYPRODUCTID

(DUMMYPRODUCTID)
First story
Scenario
3
4

next story
Scenario
5
6
7
8
9
10
11
12
3
4
5 exception
7 exception
9 exception
Scenario
11
12
3
4
5
6
7
8
9
10
11 exception
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Scenario
27
28
29
30
31
32
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 exception
29 exception

Have you encountered such issues in the past with your HSQL implementations ?

Was it helpful?

Solution 2

After running the db as a seperate instance and changing to H2db we found the source of our issue. The db's dont like sequences being dropped and recreated or even altered on each scenario run. We seperated our scripts into a data insert script and a sequence alter script. The data is cleared using dbunit and this is followed by the insert script on each run. The sequences are only altered on startup and the bdd stories sequentially increment as expected.

OTHER TIPS

The statement restarts the IDENTITY sequence declared inside the table:

TRUNCATE TABLE T RESTART IDENTITY AND COMMIT NO CHECK

This statement does the above for all the tables in the schema, as well as resetting the sequences that are outside the tables:

TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK

The statement to drop the sequence drops it permanently. There is no trace of it left when you create it again.

All the above can be tested with the latest HSQLDB 2.3.X It is possible that you are using an older version, or possibly one of your statements is not actually executed. Note you should drop the sequence with one statement and create it again with another statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top