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

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.

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 can one connect Prisma/GraphQL to an existing MySQL DB (specifically WordPress)?

I've been teaching myself GraphQL using Prisma -- very much a beginner at both.
I just got a programming test where the brief is to connect a React Native frontend to a WordPress backend. I thought this might be a fun opportunity to put some Prisma and GraphQL into practice. My thought was to connect Prisma to the WP MySQL DB and then use Prisma-GraphQL to connect to React Native.
However, it seems that connecting Prisma to MySQL isn't so straightforward -- at least for me, a beginner. I'd welcome any info as to how one might go about this -- not detailed code, of course, but more whether it's reasonable to do what I'm thinking of, and what packages/route would be the best way forward. Thanks!
An easy answer would be: All you need to connect Prisma to any MySQL database is a Host and a port(3306 because this is MYSQL) of where the DB is running.
And this leads us to where do you have your Prisma Server running?
if you are just a front-end dev and you don't know or don't wanna deal with stuff like docker then you better off using something like Prisma Cloud which will provision a Prisma server for you and can also host the MySQL Database for you or you can host it yourself and choose the have an existing database option and provide the hostname(URL) where you have it during creating your Prisma server.
on the other hand, if you comfortable running it locally or on a cloud provider with Docker(Local Prisma Setup) then you just provide the host and port as you can see in the docs link above in your docker-compose configuration file to Prism.
update:
Regard connecting to an existing DB, If you tried to do a prisma deploy on a DB with a different schema it probably gonna blow and complain about the schema conflict, that's why you need introspection for before but as you mentioned in the comment it's only for PostgreSQL at the moment, they are working on creating a tool for MySQL but it's no one knows when it could be out you can check for updates on the open issue with this feature here
hope this can help simplify things a bit.
For learning project you can do choose any solution. You will have fun and pain with MySQL shared between Prisma/ReactNative and WordPress.
For real project, you should avoid sharing one DataBase between different applications. In your case right architect decision is using WPGraphQL instead Prisma. It will provide you GraphQL from the WordPress Plugin.

SpringBoot Redis integration testing

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.

How do I migrate a SQLAnywhere 9 db running in a remote server into a mysql server on my machine?

I am working at a company that has some CRM software running in a remote Windows XP server that uses a SQLAnywhere 9 db to store its data; I have access to this remote server with an administrator account.
I would like to extract the db into a .sql file so that I can run the db locally on my machine without affecting the running db in the server (since it is key for the company's day to day operation).
The reason I need this is that we are going to test some BI Software and we need data from this database to test it, but we don't know the structure of the database since the developers of the CRM software didn't give us any documentation on it. So we need to have the database locally so that, without affecting the running CRM, we can:
understand the structure by looking at the DDL
make queries to it to get sample data
I researched a bit, and the most common solution to my problem was to use dbunload on the remote server to unload the db into a reload.sql file that contained what I needed. But most tutorials on the subject mention that I have to stop the db first (which would be catastrophic). If this is the only option, then I guess I am willing to do it on the weekend when the CRM is not used, but I wanted to know if there was another solution first.
If there is no other solution, can you point me to where I can find the proper and safer way to do this?
I have researched a lot, but prior to this day I have never even heard of SQLAnywhere, so I really need all the help I can get. My main concern is doing something that impacts negatively the CRM software.
Thank you.
You can run dbunload across the network, you just have to tell it to do an "external" unload. The default is to do an internal unload which would only work from the machine where the database server is running.
I don't have SQL Anywhere 9 documentation right now to look up the exact switch, but dbunload -? should show you all the possible switches.
Edit:
-an will create a new database and load the data and schema from another data
-xi switch will do external unload and internal reload.
-c parameters to connect to your remote database

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.