How to access datasource information in spring boot with spring data jpa and hibernate - spring

I need to perform an health check on my application that uses spring boot with spring data jpa and hibernate.
I need to do this with jpa and not make specific to any implementation.
In EclipseLink we can use EntityManagerFactoryDelegate as shown below, but I dont know How to do this with spring data jpa and hibernate.
EntityManagerFactoryDelegate delegate = entityManager.getEntityManagerFactory().unwrap(EntityManagerFactoryDelegate.class);
PersistenceInfo = delegate.getSetupImpl().getPersistenceUnitInfo();
DataSourceImpl dataSource = (DataSource) info.getNpnJtaDataSource();
return dataSource.getName();
Can anyone suggest me how to do this in spring data jpa using hibernate.

You can use spring boot actuator dependency for healthcheck doesn't need to configure externally. Once you define Datasource Bean it will auto pick database healthcheck.
if you want to enable/disable database health check you can use the below property,
management.health.db.enabled=<boolean, true || false>
implementation reference :https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

Related

How to reproduce Spring Boot Configuration POJO creation but in Spring Core (No boot)

The Context
As described in the Spring Boot documentation, one of the ways to create a POJO that holds configuration properties values is:
//On the configuration class
#EnableConfigurationProperties(value = {
POJO_CLASS_HERE.class
})
//On the POJO Class
#ConfigurationProperties(prefix = "PROPERTIES_PREFIX_HERE")
#ConstructorBinding
It works fine - when you have Spring boot in the project.
Now, I have another project that has only Spring V4 (core + batch) - No boot.
The Question
How to use/reproduce the Configuration POJO creation of Spring Boot in Spring core ?
Spring Boot - Type Safe Configuration properties

How to register a hibernate typedescriptor in a "classic" Spring 5 application with jpa entitymanager

I have a classic spring 5 application (NO spring boot). For a custom basic type i have created a typedescriptor by subclassing AbstractTypeDescriptor according to Hibernate 5.2 Documentation - 2.3.5. Explicit BasicTypes. I am using hibernate via an jpa entitymanager. I have two spring beans configured for that: a LocalContainerEntityManagerFactoryBean and a HibernateJpaVendorAdapter.
Now i am a bit lost, how to register the typedescriptor during startup of the application. The docs mention an approach using a hibernate configuration object. But i have no idea, how to get this? Any help appreciated!

circular dependency error for dataSource after adding spring DATA JPA

I am working in spring boot security with Oauth2. Oauth2 to makes use of jdbcAuthentication in AuthorizationServerConfigurerImpl. But in my webSecurityCofigurer implementing class i want to use Spring data JPA to implement userDetailsService. After enabling #EnableJpaRepositories it is throwing circular depenency error and not able to load dataSource which i am defining in AuthorizationServerConfigurerImpl.
Please help me resolving issue

spring boot could show sql even use JdbcTemplate directly

spring boot project, used JdbcTemplate, and want to show sql which is executed, the configuration is as below
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
but nothing output, it seems above configuration only support spring data jpa, So I'd like to know does exist some manner could show sql even used JdbcTemplate directly?
There is a reason why the property is named spring.jpa something: it is to indicate you that it relates to JPA in some form.
If you're using JdbcTemplate, you're not using JPA hence that property can't have any effect. You can enable logging for the org.springframework.jdbc.core.JdbcTemplate class
logging.level.org.springframework.jdbc.core.JdbcTemplate=debug

Transaction in Spring with datasources create in runtime

I have a problem with transaction in spring, because in my project datasources are created in runtime from side files and according to documentation:
I should inject to TransactionManager dataSource and made it visible for annotation #Transactional using <tx:annotation-driven transaction-manager="txManager"/>.
So my question is how can I do it when I want to use annotation?
First of all how are you creating the datasources at runtime.
if directly as Datasource=new datasource... I will suges use BeanDefinitionBuilder in Spring 3.2 to Create the Datasourcebean and then register it via BeanDefinitionRegistry.
and then get the bean from Spring context and it will be considered using the transaction.

Resources