I am developing a multi tenant app in CodeIgniter, where every tenant has its own db. At run time I find the tenant name and then load its db info from my master database. In My_Model a function establishes connection with slave database
function getDbConFig() {
$dsn = 'mysql://'.$this->dbs_user.':'.$this->dbs_pwd.'#'.$this->dbs_dbhost.'/'.$this->dbs_dbnam;
if(!empty($this->dbs_user) && !empty($this->dbs_dbhost) && !empty($this->dbs_dbnam)){
$this->db_slave = $this->load->database($dsn, TRUE);
}
}
Every thing is working fine, but problem is it take very long time in establishing slave database connect.
Any help will be appreciated.
IMHO, rather than use the getdbconfig at the model level, you should be considering using at the Data access layer level. Did you check out the connection from the connection pools. You can consider increasing the connection pool recycling frequency. Also, you should not be handling two connections at any point of time.
Post your details here for more detailed discussion.
Related
We are having dropwizard application using default configurations provided by dropwizard-jdbi for connecting to database.
Using the following to get sql connection object
Connection dbConnection = handle.getConnection();
Did a code walk-though and verified that the connections that are opened are closed.
But when i check v$session, I can see some inactive-sessions still present and are not getting released for long time.
I am using default connection pool provided by dropwizard.
Please let me know how to get the inactive sessions released.
What are your settings in the configuration file for Dropwizard?
If you have a look at http://www.dropwizard.io/1.3.0/docs/manual/configuration.html#database and then the service configuration there is an option for connections to keep alive.
# the minimum number of connections to keep open
minSize: 10
But most of the times you want to have some connections open this will speed up your application. Your application doesn't have to validate and connect to the database again and again for every call. That's one of the purposes of a connection pool.
I'm working on logs. I want to reproduce a log in which the application fails to connect to the server.
Currently the commands I'm using are
db2 force applications all
This closes all the connections and then one by one I deactivate each database using
db2 deactivate db "database_name"
What happens is that it temporary blocks the connections and after a minute my application is able to create the connection again, due to which I am not able to regenerate the log. Any Ideas how can I do this?
What you are looking for is QUIESCE.
By default users can connect to a database. It becomes active and internal in-memory data structures are initialized. When the last connection closes, the database becomes inactive. Activating a database puts and leaves them initialized and "ready to use".
Quiescing the database puts them into an administrative state. Regular users cannot connect. You can quiesce a single database or the entire instance. See the docs for some options to manage access to quiesced instances. The following forces all users off the current database and keeps them away:
db2 quiesce db immediate
If you want to produce a connection error for an app, there are other options. Have you ever tried to connect to a non-estisting port, Db2 not listening on it? Or revoke connect privilege for that user trying to connect.
There are several testing strategies that can be used, they involve disrupting the network connection between client and server:
Alter the IP routing table on the client to route the DB2 server address to a non-existent subnet
Use the connection via a proxy software that can be turned off, there is a special proxy ToxiProxy, which was designed for the purpose of testing network disruptions
Pull the Ethernet cable from the client machine, observe then plug it back in (I've done this)
This has the advantage of not disabling the DB2 server for other testing in progress.
I'm developing an express-based application that makes some queries to different (different by user!) SQL Server 2008 and 2014 databases. It's different because each user belongs to a different company and each company has its own SQL Server. My app uses an own SQL Server to manage companies and their SQL Server connection string (my app has access to their database servers). I'm using the mssql module.
I've not found a best practice regarding "should I use one SQL Server connection per user session or one connection for each user request".
Coming from a .NET world we had a rule: "one query/function - one connection".
First, the app has to query the own app database to get the SQL Server connection string for the database of the user's company. The user then can retrieve some data from their company's SQL Server (in my app) - like getAccounts(). Each of these functions (each function - not each request in that function!) opens a new connection and closes it after query completion:
let connection = new mssql.Connection(conStr, (err) => {
request.query(queryString, (err, result) => {
if (err)
throw new Error('...');
resolve(result)
connection.close();
});
})
As far as I understand, it should make no (negative) difference if 100 users open and close connections per request (assuming just one request per user at the same time) or if 100 user have 100 opened connections (one per user) for the whole session. At first glance it seems that my approach is less resource hungry since connections are only opened when they are needed (i.e., a few seconds per request).
Am I missing something? What if 200 users access my app at the same time - will I get in trouble somehow?
Thanks in advance!
[EDIT]
As far as I understand,
let connection = new mssql.Connection(...)
will create a new connection pool which will open a new connection when I use something like
connection.connect()
and close all active connections with:
connection.close()
So I'm guessing that best practice in my scenario would be to create one connection pool (new mssql.Connection(..)) per active user, save it in some kind of session store and then reuse it throughout the lifetime of the session.
Is this a good approach?
I just want to avoid one thing: a user gets an error because a connection can't be created.
I'm trying to understand what is the right way of using Session. If creating the session with
session = Session()
creates new connection to db each time, then I must try to reuse my session for several transactions, otherwise I can create it frequently.
Can you help me with this?
SQLAlchemy has built-in connection pooling for the engine that you make (a connection is reused if already available).
The "session" itself is bound to an engine:
# create a configured "Session" class
Session = sessionmaker(bind=some_engine)
# create a Session
session = Session()
Therefore, the session will use the default connection pooling automatically; you don't need to do anything special to gain the benefits of this feature.
You can read more about how it works (and, if you wish, how to tune the connection pool to modify values such as connection timeout and pool size) in the documentation.
I have 3 servers set up for SQL mirroring and automatic failover using a witness server. This works as expected.
Now my application that connects to the database, seems to have a problem when a failover occurs - I need to manually intervene and change connection strings for it to connect again.
The best solution I've found so far involves using Failover Partner parameter of the connection string, however it's neither intuitive nor complete: Data Source="Mirror";Failover Partner="Principal" found here.
From the example in the blog above (scenario #3) when the first failover occurs, and principal (failover partner) is unavailable, data source is used instead (which is the new principal). If it fails again (and I only tried within a limited period), it then comes up with an error message. This happens because the connection string is cached, so until this is refreshed, it will keep coming out with an error (it seems connection string refreshes ~5 mins after it encounters an error). If after failover I swap data source and failover partner, I will have one more silent failover again.
Is there a way to achieve fully automatic failover for applications that use mirroring databases too (without ever seeing the error)?
I can see potential workarounds using custom scripts that would poll currently active database node name and adjust connection string accordingly, however it seems like an overkill at the moment.
Read the blog post here
http://blogs.msdn.com/b/spike/archive/2010/12/15/running-a-database-mirror-setup-with-the-sqlbrowser-service-off-may-produce-unexpected-results.aspx
It explains what is happening, the failover partner is actually being read from the sql server not from your config. Run the query in that post to find out what is actually being used as the failover server. It will probably be a machine name that is not discoverable from where your client is running.
You can clear the application pool in the case a failover has happened. Not very nice I know ;-)
// ClearAllPools resets (or empties) the connection pool.
// If there are connections in use at the time of the call,
// they are marked appropriately and will be discarded
// (instead of being returned to the pool) when Close is called on them.
System.Data.SqlClient.SqlConnection.ClearAllPools();
We use it when we change an underlying server via SQL Server alias, to enforce a "refresh" of the server name.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.clearallpools.aspx
The solution is to turn connection pooling off Pooling="false"
Whilst this has minimal impact on small applications, I haven't tested it with applications that receive hundreds of requests per minute (or more) and not sure what the implications are. Anyone care to comment?
Try this connectionString:
connectionString="Data Source=[MSSQLPrincipalServerIP,MSSQLPORT];Failover Partner=[MSSQLMirrorServerIP,MSSQLPORT];Initial Catalog=DatabaseName;Persist Security Info=True;User Id=userName; Password=userPassword.; Connection Timeout=15;"
If you are using .net development, you can try to use ObjAdoDBLib or PigSQLSrvLib and PigSQLSrvCoreLib, and the code will become simple.
Example code:
New object
ObjAdoDBLib
Me.ConnSQLSrv = New ConnSQLSrv(Me.DBSrv, Me.MirrDBSrv, Me.CurrDB, Me.DBUser, Me.DBPwd, Me.ProviderSQLSrv)
PigSQLSrvLib or PigSQLSrvCoreLib
Me.ConnSQLSrv = New ConnSQLSrv(Me.DBSrv, Me.MirrDBSrv, Me.CurrDB, Me.DBUser, Me.DBPwd)
Execute this method to automatically connect to the online database after the mirror database fails over.
Me.ConnSQLSrv.OpenOrKeepActive
For more information, see the relevant links.
https://www.nuget.org/packages/ObjAdoDBLib/
https://www.nuget.org/packages/PigSQLSrvLib/
https://www.nuget.org/packages/PigSQLSrvCoreLib/