Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
3.0.8
-
None
Description
For instance, given the following data:
Id | Event_Date_Time |
---|---|
0 | 2022-09-01T00:00:00Z |
1 | 2022-09-01T01:00:00Z |
2 | 2022-09-01T02:00:00Z |
3 | 2022-09-01T03:00:00Z |
4 | 2022-09-01T04:00:00Z |
5 | 2022-09-02T00:00:00Z |
6 | 2022-09-02T01:00:00Z |
7 | 2022-09-02T02:00:00Z |
8 | 2022-09-02T03:00:00Z |
9 | 2022-09-02T04:00:00Z |
If I insert these rows with 2 threads in parallel (one thread bulk inserting the 5 first rows and another thread bulk inserting the 5 last rows), when I select the inserted rows, the values are randomly scrambled, for instance:
Id | Event_Date_Time |
---|---|
0 | 2022-09-01T00:00:00Z |
1 | 2022-09-02T01:00:00Z |
2 | 2022-09-02T03:00:00Z |
3 | 2022-09-02T04:00:00Z |
4 | 2022-09-01T04:00:00Z |
5 | 2022-09-01T00:00:00Z |
6 | 2022-09-02T01:00:00Z |
7 | 2022-09-02T02:00:00Z |
8 | 2022-09-02T03:00:00Z |
9 | 2022-09-02T04:00:00Z |
If I insert these rows in bulk without concurrence, there's no issue.
In your example, the behavior is expected :
ORM code
entityManager.flush();
entityManager.clear();
}
entityManager.persist(demo);
i++;
}
Will result in hibernate internally executing 5 * JDBC prepareStatement.execute().
Since there is 2 paralleles runner, this result with INSERT with mixed IDs, like you describe.
There is nothing wrong in MariaDB Server or connector there.
This is just how hibernate send the data.
There is some solutions hibernate side, like for example adding batch_size :
spring:
jpa:
hibernate:
jdbc:
In that case, hibernate will not run some PrepareStatement.execute, but will execute those in batch (preparestatement.addBatch() x times, then preparestatement.executeBatch)
MariaDB connector and Server will then receive only one command with all the insert at a time, then IDs will be orderered.