question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to prevent SELECT queries from SequenceGenerator

See original GitHub issue

If I use the pooled SequenceGenerator

 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
 @SequenceGenerator(name = "my_seq", sequenceName = "my_seq", allocationSize = 10)

then my query assertions fail sometimes, because due to the pooling, the statement(s)

select nextval ('my_seq')

are sometimes called before and sometimes during the test.

It works, when I change this property to none

spring.jpa.properties.hibernate.id.optimizer.pooled.preferred=none

Is this approach recommended or is there a better way to assert query count when using pooled sequence generator?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kruptcommented, Jun 3, 2020

For our project, I wrote an exclusion listener. Works like a charm:

...
ProxyDataSource proxyDataSource = new ProxyDataSource(dataSource);
proxyDataSource.addListener(new ExcludeSelectSequenceFromStatisticQueryCountListener());
class ExcludeSelectSequenceFromStatisticQueryCountListener extends DataSourceQueryCountListener {

    private static final Pattern SELECT_SEQUENCE_PATTERN =
        Pattern.compile("select\\s.*nextval\\s\\('.*'\\)",
                        Pattern.CASE_INSENSITIVE & Pattern.MULTILINE);

    @Override
    public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
        final List<QueryInfo> newQueryInfoList = queryInfoList.stream()
            .filter(it -> !SELECT_SEQUENCE_PATTERN.matcher(it.getQuery()).matches())
            .collect(Collectors.toList());

        super.afterQuery(execInfo, newQueryInfoList);
    }
}
1reaction
vladmihalceacommented, Jun 3, 2020

It’s not a good idea to disable identifier optimizers. The same select queries are executed in production too, so you should check the actual statement count.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generate sequence NexVal without execute select query
My class is not Entity there is code fragment @SequenceGenerator(name="seqUniqueKeyGenerator",sequenceName ...
Read more >
How to use Hibernate identifier sequence generators properly
Using a SEQUENCE generator is a better alternative since the identifier can be generated prior to executing the INSERT statement.
Read more >
Hibernate Tips: How to use a custom database sequence
First of all, you have to annotate the primary key attribute with the @GeneratedValue annotation and set GenerationType.SEQUENCE as the strategy.
Read more >
JPA + Hibernate - @SequenceGenerator Examples - LogicBig
SELECT * FROM MyEntity2 5 6. The above output shows that generated sequence has first three values consistent with our SequenceGenerator:.
Read more >
Sequence Generator transformation
We want to hear from you! You can now add comments to any guide or article page. To provide feedback and suggestions, log...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found