I'm trying to merge to php code bases which each use a different DB. Can I call DB::connect which changing which DB is considered active? Or can I save the active DB and restore it after calling connect?
Edit: The problem is one code base uses mysql_query() without providing $link_identifier, and I don't want to change all the calls, but I still want to be able to open a 2nd DB connection. Right now it works depending on the order in which I connect to the DBs, which is a pain.
DB::connect creates a connection resource. You can connect to many different databases/servers at the same time. The connection resource contains the database being used. Just store the returned connection reference in different variables and you will be fine.
Connecting to more than one database at the same time is actually very common. For example, connecting to a slave DB for SELECTs and master DB for INSERTs, UPDATEs and DELETEs.
When you create a new connection to the DB, a resource link is usually returned or stored as a member of the DB wrapper class or whatever. You MUST use this link (stored in a variable, for instance) each time you call query functions, etc. If you do not specify a link, the last opened link will be used instead which could be disastrous. If you are diligent about using the correct created link, however, then there will be no problem at all.
Related
Is there a way to change Laravel DB connection, say, through API call, and the data returned before and after that API call will be different for every Models?
The rough concept is:
I have 2 DBs, say first_database and second_database, a connection called mysql, and an API to change the connection's database. Everytime that API gets called, mysql connection's database will be changed based on the given parameter. After that, when I get the Models' data, it will be different since it has different database.
Is there a way to do that as efficient as possible? I've looked into:
DB::connection('...')->select('...')
,
config()->set('database.connection', 'second_mysql') (I tried using different connections)
and $models = new Model(); $models->setConnection('second_mysql');.
Both DB::connection('...')->select('...') and $models = new Model(); $models->setConnection('second_mysql'); is not very efficient in my opinion because I have a lot of models/tables and I don't want to change each of their connection, so these two will be my last resort. What I tried is config()->set('database.connection', 'second_mysql') and it works, but also not. It works because when I run this code:
config()->set('database.connection', 'second_mysql');
dd(Item::all());
(I have items table, for the first_database it has 3 records and 0 records for second_database)
It returned null(this works). But when I run the config()->... part and get all items as 2 different APIs, get all items API returned 3 records from first_database(this doesn't work).
Is there anything wrong with that approach? Or do I need other approach to do so?
I will leave my not working code just in case:
API method for changing connections (the dump returned second_mysql when I passed 1)
API method to get all items (the dump returned mysql even after I called the API above)
You can define database connection in Eloquent itself
protected $connection = "your database connection name";
Laravel Eloquent Database Connections
In your way you are creating one more connection instead setting connection to default
'default' => env('DB_CONNECTION', 'mysql')
You are required to set value of default on the fly. So it should be
config()->set('database.default', 'second_mysql');
Is it possible to execute a query every time a new connection to the database is open. This query needs to be run immediately after opening a database connection because this query calls a stored procedure that sets up various access control on the tables using the details of the user who requested to run the query. I have tried to call this stored procedure in various places and they are all successful but I would like some feedback.
I have put this query in an abstract mapper class which other mappers extend. Each time a new mapper is instantiated the stored procedure is called.
Another place to put this call to the stored procedure is before calls to other mapper methods. The downside of this is that there will a lot of duplicate code (code that calls the stored procedure).
The final place to put this call is in Bootstrap.php. Each _init method in this file is suppose to run only once each time the application is loaded. I have decided to put this call to the stored procedure in one of the Bootstrap.php for one of our modules. This is so far the best place I can think of because I only have to write the code that calls the stored procedure once and each time the application is accessed the initialize method will run. The downside of this is that I do not know the side affects of putting things in Bootstrap.php.
One of the main downsides of all these places is that the stored procedure gets called many times. For the access control to work, the stored procedure needs to be called only once per database session. This will not cause any issue other than speed due to unnecessary calls.
Are there any better places to put this call to the stored procedure? Does Zend FM have this feature implemented somewhere? Is there anything I need to take into consideration if I put this in the Bootstrap file.
Thanks for reading this and any help provided.
DB2 Version 10.5
Linux Platform
Zend FM Version 2
As an alternative, you can configure this on the database side by setting the database configuration parameter connect_proc to the name of your stored procedure. Note that the procedure will then be called for all connections, remote and local, including those made by the database administrator.
If you need to pass extra information from the client to the connect_proc routine, you could use the client accounting string connection parameter, which you could then read in the procedure using the MON_GET_CONNECTION() function:
SET acct_string = (
SELECT client_acctng FROM TABLE (
MON_GET_CONNECTION(MON_GET_APPLICATION_HANDLE(),-1)
)
)
I'm using the following code to connect to my Database - PHP7 to MySQL
<?php
?>
The connection works fine.
My question is I'm calling this code around 5 times for each page request. This is because its included different functions.
Does this mean I'm making 5 DB connects for each page request? Or is it persistent and only called once?
If it is calling multiples times I could use a global to keep it to once.
thanks
You are using object, which creates connection in constructor. You will have as many connections as instances of database will be created.
If you create database instance, set it to some variable and use connection from this instance - there will be only one connection.
If you are creating database instance for each query - there will be many connections.
I have an issue where an instance of Solr is querying my MySQL database to refresh its index immediately after an update is made to that database, but the Solr query is not seeing the change made immediately prior.
I imagine the problem has to be something like Solr is using a different database connection, and somehow the change is not being "committed" (I'm not using transactions, just a call to mysql_query) before the other connection can see it. If I throw a sufficiently long sleep() call in there, it works most of the time, but obviously this is not acceptable.
Is there a PHP or MySQL function that I can call to force a write/update/flush of the database before continuing?
You might make Solr use SET TRANSACTION ISOLATION LEVEL = READ-COMMITTED to get more prompt view of updated data.
You should be able to do this with the transactionIsolation property of the JDBC URL.
I'm wondering how slow it's going to be switching between 2 databases on every call of every page of a site. The site has many different databases for different clients, along with a "global" database that is used for some general settings. I'm wondering if there would be much time added for the execution of each script if it has to connect to the database, select a DB, do a query or 2, switch to another DB and then complete the page generation. I could also have the data repeated in each DB, I just need to mantain it (will only change when upgrading).
So, in the end, how fast is mysql_select_db()?
Edit: Yes, I could connect to each DB separately, but as this is often the slowest part of any PHP script, I'd like to avoid this, especially since it's on every page. (It's slow because PHP has to do some kind of address resolution (be it an IP or host name) and then MySQL has to check the login parameters both times.)
Assuming that both databases are on the same machine, you don't need to do the mysql_select_db. You can just specify the database in the queries. For example;
SELECT * FROM db1.table1;
You could also open two connections and use the DB object that is returned from the connect call and use those two objects to select the databases and pass into all of the calls. The database connection is an optional parameter on all of the mysql db calls, just check the docs.
You're asking two quite different questions.
Connecting to multiple database instances
Switching default database schemas.
MySQL is known to have quite fast connection setup time; making two mysql_connect() calls to different servers is barely more expensive than one.
The call mysql_select_db() is exactly the same as the USE statement and simply changes the default database schema for unqualified table references.
Be careful with your use of the term 'database' around MySQL: it has two different meanings.