Hibernate database (maria-db) change detect - java

I need to understand, Is there anyway I can continuously look for change in Maria-DB table using Hibernate.
I have a spring boot application that is connected to Maria-DB. If some other application perform CURD on table, I want to catch that in spring.
If it's not possible using hibernate, suggest me alternative.
PS : This spring boot application is running is different Docker Container and Maria-DB is running is different Docker Container.

You can use Database Triggers, or better why don't you try enabling Hibernates 2nd level cache?

Solution to this specific problem is Spring Integration module as other ways not working here. I have create a sample application to demonstrate here you can find the source code
What this application does look database continuously.

Related

how to prevent jdbc from trying to connect to a mysql database during unit testing

I'm making an application for a school project, but I'm running into the issue that when I try to run the unit tests that it tries to connect to the database while starting up the application, which isn't required for the tests (because it will be mocked), and is not available in the CI/CD pipeline.
jdbc connection error
I'm building my project in Java Maven Springboot and would like to know how I can prevent it from trying to connect to the database when running my test.
here is a link to my repository: https://gitlab.com/kwetter_jack/Kwetter_posts/-/tree/ci_cd_setup
Your test classes have #SpringBootTest annotation which will start a Spring application context - as your application uses a database the tests will also try to setup and use a database connection.
The simplest solution is to remove the annotation so the tests no longer try to connect to a database. You'll probably need to mock some more dependencies as a result as Spring is no longer creating these for you. You could also have a look at https://www.baeldung.com/spring-boot-testing for some other ideas how you could alter your tests.
Alternatively if you do want / need the application context to run you can add a application.yaml for the tests that defines and uses a in memory DB so the tests have something to connect to - see https://www.baeldung.com/spring-boot-h2-database for details how to do this.
Just change value under spring.datasource to H2 database to prevent
The application connect the real database.
Test application.yml
FYI, You no need to copy all config from original application.yml, just only some config that you need to override.
while I was investigating the spring boot H2 in-memory database (as suggested by Chris Olive and Paranaaan) I also came across the option of using a test container. after looking into this I saw that this enables the project to create a temp docker container with a MySQL image that I can use during the testing of my project, considering I was planning on using docker anyway for the integration testing of my microservices project I attempted this and it worked as I had hoped it would.
if anyone is interested in the test container solution that I used, the information can be found here:
https://www.testcontainers.org/modules/databases/mysql/

Flyway, spring boot and application start without database

I'm trying to make Spring Boot application with Flyway (and Hikari pool) to start the server even when the DB is not available at that time.
I need to support cases when:
1. DB is not available when applicaition starts (it should run Flyway after DB starts, it can be up to 30 mins).
2. DB goes offline during the application lifetime and then goes back up.
I got a problem with the first case, Flyway always tries to do migrations even when DB is not available and application stops.
I tried adding spring.datasource.continue-on-error: true but Flyway ignores that, and I couldn't find any flyway configuration that would allow such operation.
Is it possible or should I wrap Flyway and do it myself?
Spring boot 2.1.4
A couple of points to consider
What is the desired behavior of the application when the DB is really not available when the instance of java application? Ok, so flyway won't start, but how the application will be able to handle requests that will have to reach the database?
Flyway itself relies on DataSource bean, maybe on hibernate if you use it, and these are much more complicated infrastructures than flyway itself?
Maybe if the database is not available the application won't need to start at all?
Instead it worth to rely on orchestrators (like kubernetes, ECS or whatever that will recognize that the application didn't start and will try to retrigger the start again, again, and again till the database will be ready)?
This is my recommendation in general.
Now, assuming find answers to all these questions and still, want to proceed with this path:
Spring Boot by itself works like this when it comes to flyway integration:
If the relevant classes (Flyway class) exist on classpath and spring.flyway.enabled=true then the bean of flyway starts and spring boot does its magic.
Technically the relevant auto configuration can be found in class org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration (org.springframework.boot:spring-boot-autoconfigure module)
I think the way to go is to disable flyway, and given that fact that beans like DataSource are available (somehow) - create a Flyway Bean by yourself and trigger the migration in some kind of loop in the background that will exit only if the migration actually succeeds (or already applied)

Integrating Hibernate to Spring running on embedded jetty

I have just returned to Spring after 5 years of gap and it seems lot is changed. I have a task to create a REST Service using Spring with hibernate as an ORM, So far I am able to run a basic Rest Service using embedded jetty and able to make GET/POST calls, the next is to integrate hibernate into it so that the data fetch/Sent operation actually use MySQL instead of sending hard-coded response(which I have done currently).
The issue is earlier I used to work on Spring MVC using Tomcat where we had web.xml to define the dispatcher servlet and application-context which in turn were used to define hibernate config and other beans declaration, but now having embedded jetty I am not finding a way to integrate hibernate to my REST app, Is the way to add configuration is changed from XML to class based config. I searched over internet but maybe I am out of words or not using correct keywords, in short, Have no luck finding some solution to integrate hibernate to my Spring app which is using embedded jetty.
Could some please breif me about the recent changes or point me to the right tutorial ?
Any help appreciated, thanks in advance !
ps - I have used this example to progress so far https://github.com/pethoalpar/SpringMvcJettyExample
Yes , lot of changes in these 5 years and one of the game-changer is spring-boot
If you want to build a brand new project especially if you want to run the web application on the embedded container such as Jetty , you can check out spring-boot.It already provides integration with Jetty , hibernate and Spring MVC REST service.
The end result is that you just need to change a little bit configuration (most probably the DB connection info) and you can quickly get a production-ready REST service backed by JPA/Hibernate which can just run without any XML configuration anymore.
There are tons of tutorials in Internet talking about how to do it . You should easily find them out such as this using the keywords something likes "springboot webservice hibernate jetty" etc.

Is Spring Data's #Transactional(Propagation.REQUIRES_NEW) supported in Vert.x?

From what I have read so far, Propagation.REQUIRES_NEW makes use of transaction suspension capabilities in most common Java EE containers (JBoss, Glassfish, etc.)
However, as we're running Spring Data inside Vert.x, which is container-less, I'd like to find a definitive answer as to whether REQUIRES_NEW is supported in this scenario, or if we will have to use another approach for this.
You can use Spring #Transactional annotations out of a JavaEE container. It will work well if you only use a database.
If you mix transactional components (a relational database and a message broker) then you need a transaction manager, which is often only available in JavaEE container.
I'm not a Spring or SpringBoot expert but I am pretty sure it's not difficult nowadays to add a transaction manager to a Spring/Tomcat project.
As for running Spring Data in Vert.x, it will work fine if you deploy this code in worker verticles. Check out the spring-worker sample in the vertx-examples repository on GitHub

Spring Multi-Tenancy - Adding new tenant without restart (using AbstractRoutingDataSource)

I'm implementing a multi-tenant system using Spring where each tenant has its own database. I have everything up and running.
I've extended "AbstractRoutingDataSource" and overridden "determineCurrentLookupKey" to determine which connection to use via the users domain/tenancy.
The "AbstractRoutingDataSource" is instantiated when the app loads and all the possible database connections are set there.
Here's my question -
Is there a way of dynamically adding additional connections to the AbstractRoutingDataSource? I want to be able to add additional tenants without restarting.
Any help would be greatly appreciated
Thanks
I am too late to this thread, but recently I needed to build a Saas style multi-tenant web app. Tenants needed to be added dynamically without requiring a server restart. So I shared my learnings here with a complete working example.
Yes You can do So, Here is complete project which explains very nicely about your requirement:
Multi-tenancy: Managing multiple datasources with Spring Data JPA

Categories

Resources