Using Spring to connect to a database that dynamically changes - java

I have seen many solutions which all make you first configure statically via XML the different datasources and then use AbstractRoutingDataSource to return back a key which you consume while defining the datasource.
As here: dynamic datasource routing
But my case is different. I dont know how many databases there could be in my web application. I am building an app where each user uploads a small h2 db dump from a desktop app. The web app will download the h2 db dump and then connect to it.
So to make things simple to understand. Each user will have his/her own database file that I need to connect to once the user logs in. Since the number of users are not fixed, I dont know how many databases I will need to connect to, hence I cannot statically configure them in an XML file.
How to go about doing this in Spring? Also, not sure if it helps, these h2 dbs are read only. I am not going to write to them.
This is my configuration.
Maven, Spring MVC, JOOQ, H2 DBs

If you like to change the database changes dynamically, you have to write the UI for database source information and set to the spring config files in version-4.0.

Related

Create new database connections in Quarkus during runtime

I have a java REST API application using Quarkus as the framework. The application uses a PostgreSQL database, which is configured via the application.properties config file for hibernate entities (using "quarkus-hibernate-orm" module) etc. However, there are cases where i will have to dynamically connect to a remote database (connection info will be supplied by parameters) to read and write data from during runtime as well. How do i go about this the best way with Quarkus? For simplicity reasons we can assume that the remote databases are of the same type (PostgreSQL) so we don't have to worry about whether the correct driver is locally available or not.
Is there something provided by Quarkus or the environment to establish these connections and read/write? i dont need an ORM layer here necessarily, as i may not know the structure beforehand either. Simple queries are also sufficient. When i try to research this subject i can only get information about static hibernate or datasource configurations in Quarkus, but i won't know what they look like beforehand. Basically, is there some kind of "db connection provider" etc. i should use or do i simply have to manually create new plain JDBC connections in my own code for it?

Spring Boot: Is it possible to have an h2console connect directly to a MySQL DB?

I am intending to have a console on my web app so I can run queries directly from my browser. I can only find guides on how to connect the h2console to an in-memory DB instance. Is this possible? Security isn't an issue, this is strictly for testing purposes, only my ip address will be allowed to connect to the site (for now).
I think you are confusing some things here: h2 is an in-memory-database. There is NO persistent storage. MySQL is a proper RDBMS. I would not expect you to be able to connect to mysql through that interface.
If you just need to be able to execute queries from your web application, and it is not going to go public, simply create a page with a textarea, send that to the backend using JDBC. If I have misunderstood your question, please add additional details to it so we cn provide a better answer.

How to best decouple data base from application?

We have a command and control system which persists historical data in a database. We'd like to make the system independent of the database. So if the database is there, great we will persist data there, if it is not, we will do some backup storage to files and memory until the database is back. The command and control functionality must be able to continue uninterrupted by the loss or restoration of the database; it should not even know the database exists. So the database and DAO functionality needs to be decoupled from the rest of the application.
We are using RESTful service calls, Spring framework, ActiveMQ, JDBCTemplate with SQL Server database. Currently following standard connection practices using Hikari datasource and JTDS driver. The problem is that if the database goes down or the database connection is lost we start to have data issues as too many service calls (mainly the getters) are still too dependent on the database existence for processing. This dependence is what we'd like to eliminate.
What are the best practices/technologies for totally decoupling the database from the application? We are considering using AMQ to broadcast data updates and have the DAO listen for those messages and then do the update to the database if it is available or flat files as a backup. Then for the getters, provide replies based on what is available either from the actual database or from the short-term backup.
My team has little experience with this and we want to know what others have done that works well.

Multiple Schema or Single Schema for Cloud App?

I've a question about how to manage database for clients for a Cloud App. I want to create a Cloud ERP on Spring and Hibernate but I'm not sure how to manage the database. Someone says me that should create a clone schema for every client to manage properly and secure the data, but I'm not sure that should be that 'cause that mean have a powerful database server. In the other hand exist the posibility to have a single schema to all users but that could be a lots of problem in management.
Please give me some advices.
If the data you are dealing with are totally different from customer to customer, then it is better to use cloned schemas for every customer.

How to go about creating a first startup wizard with Wicket? (Database table creation etc.)

I'm working on a Java Web Application with Wicket, Spring and Hibernate. The web application is not particularily large (I have one DAO and one service), but I'd like to offer the users that deploy the application a startup wizard much like the ones found in Wordpress.
When the webpage is accessed for the first time (no database tables are created / no users), I want the user to be able to enter database settings (username, password, database name, database type) and then I want the web application to create all the tables that it will use.
As I'm new to Wicket and Java Web Development I'm not sure how one would go about achieving this. Usually, when interacting with the DAO (such as creating a user) the database table is created on demand (if it didn't already exist) -- at least that's what it looks like to me.
Is there a way to extract the SQL for mye domain objects that my application will use via the service->DAO layer?
Right now I configure database access via filters; src/main/config/application-DEV.properties for example. If I want to use a wizard such as described I guess I would need to move away from using property files?
Any help is greatly appreciated.
I have often contemplated this, as it is standard practice in many PHP / Perl systems, but seems complicated in java / spring etc.
First of all: use wicket-extensions for the wizard functionality
What I would do then is simple, I'd declare all my spring beans as lazy and use system properties to configure them (with a PropertyPlaceHolderConfigurer). I'd use the wizard to get those properties the first time and then write them to a well-known location in the file system (DB would be better, but that's a chicken / egg problem). Then I'd initialize the application context using the system properties.
The problem here is: I don't think there is a portable way to access the file system from a web application, I think every app server may handle file system access differently, so you need to be careful there.

Categories