Is it possible to add a new database connection to Django on the fly?
I have an application that uses multiple databases (django 1.2.1), and while running, it's allowed to create new databases. I'd need to use this new database right away (django.db.connections[db_alias]). Is it possible without server restart? Using module reload here and there?
Thank you for your time.
It is possible... but not recommended...
You can access the current connection handler...
Use something like this:
from django.db import connections
if not alias in connections.databases:
connections.databases[alias] = connections.databases['default'] # Copy 'default'
connections.databases[alias]['NAME'] = alias
Make sure you do not attempt to add a new alias to the databases dictionary while there is ANY database activity on the current thread.
An issue you need to overcome, is that this code will need to be placed somewhere were it will always be touched by the current thread before trying to access the database. I use middleware to achieve this.
Related
my plan of setup is to make the local database accessible to the local computers because of heavy manipulation of data and it needs a fast response, but at the same time, I wanted to access it via internet when I'm away from the local network. is this possible?
Currently Using MERN stack
I tried MLAB but the response of data is pretty slow
Thank you in advance
it looks like you're going down a dangerous path. MongoDB comes with local authentication and a standard 27017 port. To make it available online you need to
remove authentication (which is not on by default)
change or remove the bindIp option
ensure the port is not blocked by firewall
This can be done in the config file.
https://docs.mongodb.com/manual/reference/configuration-options/
However, what you really want to do, probably to create routes within express so that users can communicate with your mongo is a structured and safe manner. More information in this here http://mean.io/2017/10/31/getting-started-mean-io/
Can we add a list of domain like this :
www.google.*
into Remote Site Setting.
Google has a lot of domain :
google.com.ar
google.com.au
google.ca
....
I don't believe you can. See here for an old question that was left unanswered.
Remote Site Settings are only for HTTP calls you make from Salesforce out to another server, so if you know your contained list for outbound calls you shouldn't have to worry about new ones dynamically being needed. If you want a faster way to update this, take a look at the Metadata API, which should let you bulk change the values through underlying XML (with the caveat that you need at least one Remote Site defined in the org first).
Is this possible to prevent database connection while I requests concrete action in Zend Framework 2?
As Sam stated in a comment; if you do not wish to establish a connection to the database, then simply don't. If you do not have any code within your controller action that uses the database, then there won't be a database connection (provided that you are not doing funky stuff in a bootstrap method or similar).
If you are building a database connection in the standard way, then the actual connection will be lazily loaded, meaning that there won't be an actual connection before you try to use it.
In your comment you state that you believe the problem is caused by many database connections. I just want to clarify that there will only be one database connection per request (provided that you make use of the database).
If you have no code that calls a database, then your web server won't actually make a connection to your database. Either way, if you have 150+ images per page, then that would be a bigger concern and is probably the root cause for slow page loads. Perhaps consider pagination or if you do not display the pictures in their full sizes, then avoid scaling them in HTML as you would then be sending lots of unnecessary data from your web server to your visitors. You could resize the pictures in PHP when they are added, for instance. After that, you could even consider using a Content Delivery Network (CDN), but that is a different discussion...
I have a php class that requires a PDO instance to be passed to it's constructor. The code would be something like this:
$pdo = new PDO($host, $username, $password);
$myclass = new myClass($pdo);
The problem I've got is integrating this with CI. CI auto loads it's database class which handles all DB connections and is accessed with
$this->db->
It would be very simple if I was able to instantiate myClass with the CI database object, but it seems to contain a lot more stuff than just the PDO instance. I've tried
$myClass = new myClass($this->db);
and it doesn't like it at all. I realise I could rewrite myClass to access the CI db object directly (i.e. without having to have a new connection passed to it), but I don't really want to start that mammoth undertaking!
So, is there a way to use the CI db class for this purpose? Would it make any difference if I just created a new PDO instance as above if the CI database library is already loaded?
There isn't an easy way to get the current PDO connection from CI's database class. However, you don't necessarily need that. You can create your own PDO connection and pass that in to your custom class. You'll have two live connections to the DB, which isn't ideal, but you could probably work that out.
I have a webservice class that will be in a managed package and distributed to multiple clients. The class currently has a variable with the hardcoded value of the server it's hitting.
The problem: the server will be different for each client, so a hardcoded value will not work.
I figured since each client will have to add their server to their remote site settings, the easiest way might be to grab the correct URL from their setting. Is this possible? Or is there another "right" way to accomplish this? Thanks
The best way I've found to save configuration values is to use Apex Custom Settings. To set the Remote Site settings programmatically you could use the Metadata API.