Grails: changing dataSource url at runtime to achieve multi tenant database separation - mysql

I'm building a multi tenant application with Grails and I want to keep separate databases.
I need to change the url dynamically at runtime to point GORM to different database.
I have a front-end acting as a balancer distributing requests to a cluster of backend hosts. Each backend host runs a Grails 2.3.5 instance and a mysql-server with several databases (one per tenant). I would like to change dataSource dynamically so that GORM can access domain entities on the right database.
Any ideas ?
Thanks

You can configure multiple data source in your DataSource.groovy, have a look in the blog.
In your domains: add which data source your domain can interact, eg.,
static mapping = {
datasources(['dataSource1', 'dataSource2'])
}
or "ALL" for all datasources, eg.,
static mapping = {
datasource 'ALL'
}
and then you can make queries with data source name to which you want to get/set data, eg.,
def userClass = User.class
User user = userClass.dataSource1.findByName('username')
Ref:- multipleDatasources, Querying on multiple datasource in grails

Related

How to read a table from database at pyramid application startup?

I have a pyramid game server app that uses sqlalchemy to read/write to postgres database. I want to read a certain table(call it games) from the database at the time this app is created. This games data will be used by one of my wsgi middleware, that is hooked in the app, to send statsd metrics. To do this, I added a subscriber in the main function of my app like:
config.add_subscriber(init_mw_data, ApplicationCreated)
Now, I want to read the games table in the following function:
def init_mw_data(event):
...
...
Anybody know how I can read games table inside the function init_mw_data ?
It based how you configured your application.
Default sqlalchemy template from pyramid-cookiecutter-starter have dbsession_factory.
So, you can do something like this:
def init_mw_data(event):
registry = event.app.registry
dbsession = registry['dbsession_factory']()
dbsession.query(...)
...

How to find out that external database is updated in Django (while manage=False)

I have a project that is connected to an external database and just have view access to it. So I created my models with managed=False flag.
I was wondering how can I find out in django that any change in that database is happened. Is there any solution in django or I should find a method to communicate between that database and my django app. like socket, database triggers and ...?
More details:
Image my models is like this:
class Alert(models.Model):
key = models.CharField(max_length=20)
class Meta:
managed = False
Now i want to be notified in django each time the database is updated. I want a signal to capture database updates and do something in django?

Grails auto-generate the database from the domain model

I am trying to create a simple application using Grails(3.2) and MySQL in Netbeans.
I have configured MySql in Grails project and also created a database, named "third" . After creating the domain class model, the controller (scaffolded) and the views for CRUD, my project is running but whenever I am trying to do any operation -
screenshot of error
this error is occuring. I am unable to solve this problem and also tried manually creating "Person" table, but same error occuring.
Your stacktrace clearly states that your db with name third doesn't have proper table person. But you shouldn't create this table manually (if you already had - please remove all tables from your db)
Connection seems fine but you should take a look at dbCreate value in your application.yml.
dbCreate - Whether to auto-generate the database from the domain model
- one of 'create-drop', 'create', 'update' or 'validate'
https://docs.grails.org/latest/guide/conf.html
If you are using just simple command grails run-app to start server, then you should check out the value in the environments: development block.
Paste your application.yml content (without db credentials) if you need a personalized solution.

MySQL proxy redirect Read/Write

We have a system where we have a Master / Multiple Slaves .
Currently everything happens on the Master and the slaves are just here for backup .
We use Codeigniter as a development platform .
Now we decided to user the slaves for the Reads and the Master for the Write queries .
I have been told that this is not doable without modifying the source code because proxy can't know the type of the query .
Any idea how to proceed with this without causing too much damages for a perfectly working system ...
We will use this : http://dev.mysql.com/downloads/mysql-proxy/
It does exactly what we want :
More info here :
http://jan.kneschke.de/2007/8/1/mysql-proxy-learns-r-w-splitting/
http://www.infoq.com/news/2007/10/mysqlproxyrwsplitting
http://archive.oreilly.com/pub/a/databases/2007/07/12/getting-started-with-mysql-proxy.html
something i was also looking, few month back i did something like this but i added 3 web server with master slave mysql servers, first web server enabled with mod_proxy to redirect request to read and write server all request will come to this server, if post,put or delete request come to server it will go to write server, all get or normal request will go to read server
here you can find mod_proxy setting which i used
http://pastebin.com/a30BRHFq
here you can read about load balancing
http://www.rackspace.com/knowledge_center/article/simple-load-balancing-with-apache
still looking for better solution with less hardware involved
figure out another solution through CI, create two database connections in database.php file keep save mysql server as default database connection and other connection for write only server
you can use this base model extend
https://github.com/jamierumbelow/codeigniter-base-model
you need to extend your models with this model and need to extend you model with this, it has functionality for callbacks before and after insert,update, delete and get queries, only you need to add one custom method or callback change_db_group
//this method in MY_Model
function change_db_group{
$this->_database = $this->load->database('writedb', TRUE)
}
no your example model
class Example_Model extends MY_Model{
protected $_table = 'example_table';
protected $before_create = array('change_db_group');
protected $before_update = array('change_db_group');
protected $before_delete = array('change_db_group');
}
you database connection will be changed before executing insert,update or delete queries

Spring3, Security3, Hibernate, MYSQL - How to install user tracking into database

First Project: Spring3, Security3, Hibernate, MYSQL - How to install user tracking into database
I am working on my first project with Spring3, Security3, Hibernate, MYSQL.
I have the system working great I use Spring3 and Security3 goign to MySQL for the login and
using Spring3 MVC, Hibernate and MYSQL for system data.
I have a number of questions. Once I login does Spring Security save the user object somewhere that I can have
Hibrernate access it. I want Hibernate to put the user name or role into each insert to the database so as
I do my searches the system knows to only show data for that user and only that user?
this somes like it should be easy. Spring should be saving the user somewhere the hibernate can access.
please help me out
Once the user is authenticated, you can access the user's authentication session details:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
SecurityContext will allow you to grab the Authentication object, and from that you can retrieve the principal (an object representing the authenticated user), roles, etc. You could inspect this information and determine what data should be stored/displayed for each user.
If you can add a request filter or interceptor (the vocabulary may vary between frameworks), you could probably make these security checks abstract/generic enough to be applied across your entire web app (instead of adding a few lines of code to every resource method you're attempting to secure). Either way, SecurityContext should get you closer to what you want.