Dynamically connect to many databases using Spring boot - mysql

I have an application that is supposed to be connected to many identical databases (same tables with the same columns), or even connects to a new database (also identical) that is created while running the application.
Before the user gets into the app's functionalities, he chooses (in an input for exemple or a drop down menu) an existing database then the app connects to it and shows the data in the selected database. Or the user gives the name of a new database and the app creates it and connects to it.
I am using Mysql and Spring boot to do this. And I can't find something practical on how to use variables in the application.properties file, just how to declare many datasources (something like this : https://medium.com/#paulushc/multi-database-application-with-spring-boot-777aaf5a1e4e). But I have lots of databases and I need to create other ones while the app is running.

Related

Obtain the mysql database handler using the database name

We have two applications that will create different databases and inserts information into it. The data is inserted into the databases using the connection handler that we obtain at the time of database creation.
Suppose database "X" is created by application 1. Is the database connection handler of database "X" available in application 2? Do we have any API in C for that?
There is a MySQL C API.
Yes, mysql stored databases according to the name, which is shared between all applications.

How to dynamically use different existing database in django

i have around ten to twenty databases with same structure in mysql,
and how can i load those database dynamically in django?
(there will be a table storing those databases name, which may add/delete during the programme)
edit:
the dynamically way means that while running my website, create the orm and do somethings of another database by data input by users or which stored in the database
those database are:
1)with the same structure
2)only the name of database is different
3)not define in my setting.py when the web is started
Django has a built-in feature to support multiple databases check it out here https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#multiple-databases

Google App Maker - Reference multiple tables from External MySql Database

I recently began using Google App Maker and in order to reference multiple tables from an external MySql Database, do I have to create multiple Data Models for each table or do I have to create multiple datasources inside one Data Model and adding all the fields in it? The database has 15 tables in total.
You need to create model per table.
You can connect your existing database to App Maker (in application settings) and import tables as models (create model -> existing model). So you don't need to re-create fields.
Please be aware that as you change models in App Maker the database from application settings changes accordingly. So I would recommend use prototype database in application settings and production one in production deployment.

Create Database (not tables) on remote host using vb.net in asp.net

I am building a CRM app using vb.net in asp.net. My app is still on my local machine, but I intend to deploy it later when it's finished.
At the other hand, I have a hosting plan that gives me unlimited MySQL databases where I created a database for my app to use.
My app connects to the database, writes and reads info from it with no problem. it also creates new tables for each new Customer (each user has 9 tables in database that contain his info).
After adding a few customers, the database gets messy, containing dozens of tables in one place.
As a solution for that, I thought it will be more clear, tidy and secure if I can create a seperate DB for each user/customer from my app.
Is there a way to do this using vb.net and asp.net? If so, could you provide some sample code that can achieve this?

How to establish_connection with more than one database in parallel in Rails?

Context
I'm building a SaaS where users can create their own websites (like Wix or SquareSpace).
That's what happens behind scenes:
My app has its main database which stores users
When a user creates his website, an external database is created to store its data
SQL file runs in this external database to set default settings
Other users shall create their websites simultaneously
Approach
To create a new database and establish connection I do the following:
ActiveRecord::Base.connection.execute("CREATE DATABASE #{name}")
ActiveRecord::Base.establish_connection(<dynamic db data>)
Then I execute sql code in the db by doing:
sql = File.read(sql_file.sql)
statements = sql.split(/;$/)
statements.pop
ActiveRecord::Base.transaction do
statements.each do |statement|
connection.execute(statement)
end
end
Then I reestablish connection with main db:
ActiveRecord::Base.establish_connection :production
Problem
Establishing connection to dynamic database makes application's main database inacessible for a while:
User A is creating a website (establishes dynamic database connection)
User B tries to access his user area (which requires application's main db data)
Application throws an error because it tries to retrieve data of app-main-db (which connection is not established at the moment)
How can I handle many users creating their websites simultaneously without databases conflict?
In other words, how can I establish_connection with more than one database in parallel?
NOTE:
It is not the same as connecting to multiple databases through database.yml. The goal here is to connect and disconnect to dynamic created databases by multiple users simultaneously.
This gem may help. However,you may need to rename some of your models to use the external database namespace instead of ApplicationRecord
https://github.com/ankane/multiverse
I admit that this doesn't answer the core of your initial question but IMO this probably needs to be done via a separate operation, say a pure SQL script triggered somehow via a queue.
You could have your rails app drop a "create message" onto a queue and have a separate service that monitors the queue that does the create operations, and then pass a message with info back to the queue. The rails application monitors the queue for these and then does something with the information.
The larger issue is decoupling your operations. This will help you down the road with things like maintenance, scaling, etc.
FWIW here is a really cool website I found recently describing a lot of popular queuing services.
Probably not the best approach but it can be achieved by calling an external script that creates the database, in a separated ruby file:
Create create_database.rb file in lib folder
Put db creation script inside this file
ActiveRecord::Base.connection.execute("CREATE DATABASE #{name}")
ActiveRecord::Base.establish_connection(<dynamic db data>)
Execute with Rails Runner
rails runner lib/create_database.rb
or with system, if you want to call it from controller
system("rails runner lib/create_database.rb")
This way you can create and access multiple databases without stopping your main database.
Passing arguments
You can pass arguments to your script with ARGV:
rails runner lib/create_database.rb db_name
And catch it inside the script with ARGV[0]:
name = ARGV[0]
puts name
> db_name