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.

Support advanced customization of Hibernate settings

See original GitHub issue

After upgrade from Spring Boot 2.0.0 M2 to 2.0.0 M6 my Hibernate interceptor implementation don’t work anymore.

My old implementation:

@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration {

    private HibernateStatisticsInterceptor hibernateStatisticsInterceptor;

    public HibernateConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, ObjectProvider<List<SchemaManagementProvider>> providers, HibernateStatisticsInterceptor hibernateStatisticsInterceptor) {
        super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers, providers);
        this.hibernateStatisticsInterceptor = hibernateStatisticsInterceptor;
    }

    @Override
    protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
        vendorProperties.put("hibernate.session_factory.interceptor", hibernateStatisticsInterceptor);
    }
}

But with M5 or M6 the HibernateJpaAutoConfiguration class changed and extends JpaBaseConfiguration no more.

I try to add my interceptor per YAML-Configuration file, but it’s not working.

My Interceptor:

@Component("hibernateStatisticsInterceptor")
public class HibernateStatisticsInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 5278832720227796822L;

    private ThreadLocal<Long> queryCount = new ThreadLocal<>();

    public void startCounter() {
        queryCount.set(0l);
    }

    public Long getQueryCount() {
        return queryCount.get();
    }

    public void clearCounter() {
        queryCount.remove();
    }

    @Override
    public String onPrepareStatement(String sql) {
        Long count = queryCount.get();
        if (count != null) {
            queryCount.set(count + 1);
        }
        return super.onPrepareStatement(sql);
    }

}

It’s possible to add the interceptor as property “spring.jpa.properties.hibernate.session_factory.interceptor”, but this is a hibernate instance and not accessible in the spring context.

It would be nice, if in future versions it’s possible again, to use a spring controlled bean as hibernate interceptor, with auto configuration.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
wilkinsonacommented, Nov 30, 2017

This was prompted by a short discussion on Twitter. It feels similar to what we did in M7 for the naming strategies. It feels like a more general purpose callback for customising the Hibernate properties may be in order here. We could, perhaps, revert the bean-based naming strategy support and document the use of the callback instead.

1reaction
RizziCRcommented, Jan 9, 2018

Works fine… Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use hibernation to extend battery life on Windows 10
Click the "Change advanced power settings" option. Change advanced power settings. (Image credit: Future).
Read more >
How to Configure Hibernation Time on Windows 11 - groovyPost
Now, click the Change advanced power settings option on the following screen. Configure Hibernation Time on Windows 11. When the Power Options ......
Read more >
Surface Pro 4 or Surface Book doesn't hibernate in Windows 10
You deploy a custom Windows 10 image to a Surface Pro 4 or Surface Book. ... Click Change advanced power settings, and then...
Read more >
Shut down, Sleep, Hibernate, or Change the Power Plan ... - Dell
Select Balanced (recommended), Power saver, or select Create a power plan on the left side of the screen. Customize your plan settings as...
Read more >
Windows 10 Power and Sleep Settings - Business News Daily
Windows 10 offers several settings to help you manage how your ... a custom power plan with your own settings based on a...
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