Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support advanced customization of Hibernate settings #11211

Closed
RizziCR opened this issue Nov 29, 2017 · 12 comments
Closed

Support advanced customization of Hibernate settings #11211

RizziCR opened this issue Nov 29, 2017 · 12 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@RizziCR
Copy link

RizziCR commented Nov 29, 2017

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 29, 2017
@snicoll
Copy link
Member

snicoll commented Nov 30, 2017

@RizziCR extending an auto-configuration is a smell, please don't do that. Rather, it would have been nice to report the feature here so that we implement it properly.

I'll give it some thoughts, thanks for reporting it!

@snicoll snicoll added priority: normal type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 30, 2017
@snicoll snicoll changed the title Hibernate Interceptor as Spring Bean no more possible with HibernateJpaAutoConfiguration Support advanced customization of Hibernate settings Nov 30, 2017
@wilkinsona
Copy link
Member

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.

@httpdigest
Copy link

I think the only change needed to support this use-case (and in general customization of vendor jpa properties for the EntityManagerFactory) is to make the org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration class public so that people can simply extend from it and override customizeVendorProperties() - just what @RizziCR did with the auto configuration class, only that now the functionality moved from the auto configuration class into the HibernateJpaConfiguration class.

@snicoll
Copy link
Member

snicoll commented Dec 15, 2017

@httpdigest I disagree. This class is internal and was made private for a very good reason.

@httpdigest
Copy link

Alright then. You of course have more insight into the matter. All I'm saying is that people have a use-case which is not supported by Spring Boot right now and I was proposing a working workaround.

@snicoll
Copy link
Member

snicoll commented Dec 15, 2017

I am not following. This issue is going to provide a first-class support for what you're trying to do as far as I understood #11357. If that does not, feel free to comment there.

@snicoll snicoll self-assigned this Jan 4, 2018
@snicoll snicoll closed this as completed in 59d5ed5 Jan 5, 2018
@RizziCR
Copy link
Author

RizziCR commented Jan 9, 2018

Works fine.. Thanks!

@Pissarra
Copy link

Works how? I can not solve this with Spring Boot 2.0.0.RELEASE...

@wilkinsona
Copy link
Member

The commit that closed this issue Includes an update to the documentation that describes how the new functionality works. If you need some help beyond that, please ask on Stack Overflow or Gitter.

@Vespira

This comment has been minimized.

@snicoll

This comment has been minimized.

@Vespira

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

7 participants