There appears to be an issue be related to closing connections between calls to NEXT VALUE FOR that causes the MAX sequence value to be ignored. I have found scenarios when I am allocating sequences near the MAX whereby the MAX is not honored and value greater than the max are returned by NEXT VALUE FOR.
It appears to be related to the logic to return all sequences on connection close. It looks like if you close the connection between each invocation when you hit the max value instead of the expected error being thrown sequence values continue to be doled out. It looks like for some reason the limit_reached_flag is not being set correctly on the SYSTEM.SEQUENCE table for the sequence in this case.
I added the test below to SequenceBulkAllocationIT that repros the issue.
If I either a) remove the nextConnection() call that keeps recycling connections in the test below or b) comment our the code in PhoenixConnection.close() that calls services.removeConnection() the test below starts to pass.
I wasn't able to repro in Squirrel because I guess it doesn't recycle connections.