SQLAlchemy: does session creation create new connection? - database

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.

Related

Closed db connections are not released from pool

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.

Codeigniter Multi Tenant Takes long time in loading tenant database

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.

When accessing a database over JDBC/ODBC should you create and close the connection for each request?

If you have a class that services requests from other classes for database data when should you hold onto the databse connection and when should you close it and reopen it on the next request?
What if it's a service that responds to connections from external applications? (Web service, Ajax, rpc)
Is it a good idea to hold a singleton connection to the databse which is always open, and just reopen it on failure? Or should you open a new database connection for every request?
If maintaining a singleton database object that has an always open connection to the databse is a bad idea then are there any circumstances where it's a good idea? I've often seen it referenced as a justification for the Singleton pattern?
I'm not talking about a new connection per databse query, that would be silly.
You probably want to take a look at connection pooling.
In this scenario, N connections are opened and made available to clients. When you 'close' the connection, the connection itself is not closed, but returned to the pool for use by another client.
Apache DBCP is a useful library for managing this.

Connection pooling session settings

I have a small but significant query. I'll give a similiar use case but simplified, it does however cover my question.
Let's User A connects to the SQL Server 2000 database and we get a connection from the pool.
User A sets dateformat DMY. Finishes and the connections is released back to the pool.
User B comes along, connects with the same string and gets the same connection from the pool, sets the dateformat MDY, finished and returns the connection.
User A reconnects and gets the same connection, what is the dateformat ?
Is it re initialised on returning to the pool?
Do the connections retain there settings?
When the connection is removed from the pool where does a new connection take it's initial settings?
If you didn't program the pooling mechanism yourself. Its more likely that the connection goes back to its initial state before going into the pool. Moreover, there will be a configuration option where programmers can define or customize the behavior of the connection and the pool. And that is where the new connection gets its setting from. Otherwise, there must be a default settings given by the provider.

DAAB, the best approach to use Database instances is

Guys, I am going to use Enterprise Library (4.1) and especially DAAB. Here is I have questions:
What is the best approach and why:
Every time when I need to run a
DbCommand I create Database instance
using
DatabaseFactory.CreateDatabase();
I have a base class with instanced
Database (using the same
CreateDatabase() static method) and
something like public property which
returns the instanced database.
How it is “heavy” or fast/slow to create an instance of Database class? What if I do it every time when a DbCommand is needed?
Thank you.
It's not a problem. Creating the database class has low overhead.
However, actually creating the database connection has high overhead, which is why Windows does Connection Pooling. Briefly, the first time a process creates a DB Connection, it looks in the connection pool for an existing connection with exactly the same connection string. If it doesn't find one, it creates a new one (an expensive operation). When th process closes it and lets it go out of scope, it doesn't actually close the connection to the DB, it puts it in the Connection Pool. There is stays until the same process creates another connection with the same connection string. Then it gives you the already existing one from the Connection Pool.
You can turn off connection pooling (via a setting in the connection string) but that's usually a very bad idea.

Resources