SpringBoot Redis integration testing - mysql

I have an application with MySQL and SpringBoot.
Redis is being used for caching with spring cache annotations.
Now, starting up Redis etc is not an issue as I am using Docker compose to dynamically allocate containers for testing.
But, what is the proper way of verifying that the data is actually being written and read from Redis cache not from Mysql?

You could prevent the client code to communicate with the MySQL database during the part of the integration test where you want to ensure that only Redis is used.
You don't precise the exact way to communicate with the MySQL database, so I cannot give you a specific advise.
But here some ideas :
rely on an MySQL backend service implementation that throws exception as any method is invoked
shutdown the MySQL database
use an empty MySQL database

To test it, I create the record from rest endpoint, then I remove it from the db using spring repository directly. Then, since it is cached, it is should still be accessible by rest endpoint from cache even it is not present in the db.

Related

Sharding configuration for mysql db

I want to test a sharding example using spring boot and mysql database. If I start my mysql database on jdbc:mysql://localhost:3306/test and jdbc:mysql://localhost:3307/test. Or I could also use the same port but different schemas for testing purpose. Can I use nginx to configure as a reverse proxy to connect to the db depending on the userid(which would be my key for sharding? Or what is the best way to test it out. Is it better to shard at the application level itself. I would be using a springboot application for this.
It turns out that I was overcomplicating things. I needed to use a custom version AbstractRoutingDataSource along with multiple db configuration in spring to get it working. The whole solution can be see here

How to avoid optaweb-employee-rostering rebuild persisted data on server restart

I'm running optaweb-employee-rostering in a dockerized Wildfly server, persisting data with MySql database running in a container too. The .war file is not built in sever's Docker image, it's manually deployed in it via Wildfly's admin interface. Every time container is stopped a restarted, the application rebuild sample data, deleting any data saved during usage, so that the final behavior is the same as ram based storage: the data is lost if the server stops.
Is there a way to avoid this behavior and keep saved data on server restart?
This is caused by the hbm2dll value here and due to the Generator's post construct. In the current openshift image there are environment variables to change that.
We're working on streamlining this "getting started" and "putting it into production" experience, as part of the refactor to react / springboot.

How do I achieve persistence even when I stop running Spring?

I have just completed a Spring Boot tutorial of 34 videos. Im looking online and everything seems to show me how to create persistence of my data when the spring application is running. However, once I stop the program and restart it, it doesn't have the data I want saved.
So what it sounds like is I need to store this information on a database. I think I've set up a MySql server on my laptop and I have the workbench app/interface.
What might be more appropriate is connecting to an online server that I have with phpmyAdmin. In any case, how do I connect my spring application to a database instead of to a localhost:8080
Please let me know where I can look and what resources I have here as I'm kind of new to this sort of thing. Much appreciated!
There 2 options
Either use persistent DB instead of in-memory storage
You are using spring.jpa.hibernate.ddl-auto=create-drop hibernate property that will recreate db schema every time you start the application - thus its "clear" of the start. Use update or validate instead.
Maybe you can set spring.sql.init.mode=never to not always initialize your database using scripts.
This way you'll find all data you saved even when re-run the app.

MySQL Cluster SQL node failover

I'm trying to setup a mysql cluster for a web application to avoid having a single point of failure. The Mysql documentation says:
MySQL Cluster does not provide any sort of automatic failover between
SQL nodes. Your application must be prepared to handle the loss of SQL
nodes and to fail over between them.
The docs say the data is still accessible via the NDB API, but if my app is configured to point to a mysql server, how can i setup some type of loadbalancing/failover for multiple SQL nodes.
The failover functionality is provided by client connector libraries. For example, if you're using Java, your JDBC connection string can provide a list of MySQL host names. Here's documentation. http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-config-failover.html
With ado.net and python, you'll need a setup called MySQL Fabric. This explains the connection string monkey business you'll need. http://dev.mysql.com/doc/connector-net/en/connector-net-programming-fabric.html
Or, you can write specific application connection code that detects connection timeouts, and then tries another MySQL server.

hibernate change database dynamically for maintenance

I have a java webapp using hibernate over MYSQL db. I need at least an hour of data maintenance daily, hence I need to bring down the db and switch to the backup db.
I don't see an elegant way to switch to another db from my app using hibernate. Is there?
Other totally different ways for database maintenance is welcome.
As you are using a Java WebApp, I assume you are using a container (like Tomcat) or an application server (like JBoss). In both cases, you should be using managed connections by the container, via JNDI. In such case, you can just use JMX (or the admin console) to change the actual datasource, to point to the backup DB, clean the pool and reopen new connections. When you are done, just perform the same steps, pointing the datasource to the actual database.
If you are not using managed connections by the container, you are out of luck. As the Hibernate configuration is static, you'll need to bring down your app, deploy a new version of it with your persistence.xml/hibernate.cfg.xml pointing to the backup, do the maintenance, and deploy the "old" version when you are done.