Question regarding the new DataSource in TypeORM 0.3.7 - typeorm

After TypeORM 0.3.7 does not allow single connections anymore but only manages the pool via DataSource, there is no possibility to close a specific connection without closing the pool.
Therefore my question, do I have to close a DataSource connection in TypeORM or will it be closed automatically?
In Prisma for example the disconnect is called internally after the DataSource has been used, how is it in TypeORM, the documentation is not very informative.

Related

How to disable OLE DB from opening additional implicit non-pooled connections?

OLE DB has a sneaky feature where if your current connection is busy, it will silently open more database connections.
these happen without your knowledge
and they are not taken from, or returned to, the connection pool
With the SQL Server 2005 "native client", Microsoft introduced a feature where one connection could support Multiple Active Record Sets; so that you can officially have multiple recordsets active over a single connection. But they note it's a tricky thing to enabled, which is why it's been opt-in since beta 2 of the feature.
Secret connections? Really?
Microsoft notes this behavior:
Using ADO with SQL Server Native Client
In prior versions of the OLE DB provider, this code would cause an implicit connection to be created on the second execution because only one active set of results could be opened per a single connection. Because the implicit connection was not pooled in the OLE DB connection pool this would cause additional overhead. With the MARS feature exposed by the SQL Server Native Client OLE DB provider, you get multiple active results on the one connection.
It's also noted by John C. Gordon [MSFT] in the Microsoft forums: (archive)
In OLE DB, there is something called an "implicit connection" spawned when a query is performed while an active resultset is still pending. These are not apparent to the user, as the original poster noticed. I know that SQLNCLI, SQLNCLI10 implement these, but it I do not remember if SQLOLEDB does. What then happens is the server has 2 connections and each one has one pending resultset. This is unpleasant when the licensing model you choose is by connection.
It's also noted in a MARS announcement blog entry: (archive)
Using MARS with SQL Native Client [Chris Lee]
The second result set is using a new connection each time it is opened. This clearly has some overhead (and it turns out that the additional connections aren’t pooled, so the overhead is for a full server connection network protocol exchange each time). This is the default behavior for SQLOLEDB and SQL Native Client (OLE DB) – a new implicit connection is spawned when the main connection is busy with a default result set.
It's a feature deep inside OLE DB, designed to make data access easier.
Bonus Chatter
OLE DB is a low level, complicated, API
ADO is simplified wrapper around OLE DB
How to turn it off?
This automatic creation of secondary connections is obviously not good. It's so not good that the SQL Server ODBC driver does not do it (because it's a feature deep inside OLE DB, and comes to SQL Server's OLE DB drivers for free. It's not a feature inside ODBC).
I want to make sure i'm not doing it. To this end i want the driver to throw an error at the point in my code where i accidentally try to do this.
How can i turn off OLE DB implicit additional connections?
The solution is to disable to feature where OLEDB will open multiple connections behind your back.
You can set the Multiple Connections property of your ADO Connection to false to prevent it from implicitly opening 2nd connections when the main connection is busy with a results set.
//Set Connection.Properties["Multiple Connections"] = false
for int i = 0 to Connection.Properties.Count-1
{
Property prop = Connection.Properties.Item[i];
if (prop.Name == "Multiple Connections")
{
prop.Value = false;
break;
}
}
Note that turning off the ability to open a 2nd connection will then mean that code that used to appear to work will suddenly start to fail with the error "An object was open":
OleDbErr.h
//
// MessageId: DB_E_OBJECTOPEN
//
// MessageText:
//
// An object was open
//
#define DB_E_OBJECTOPEN ((HRESULT)0x80040E05L)
Warning
The Multiple Connections property doesn't apply to the ODBC OLEDB driver (MSDASQL), or the Active Directory driver. (ADsDSOObject)
In fact, they very act of querying for the "Multiple Connections" property of
the ADsDSOObject (active directory) provider causes it to throw an
"Item could not be found in the collection"
exception later on when running a SELECT query.
Even commenting out the code that sets:
prop.Value = false;
still causes the error.
It's the very act of calling asking for a property that doesn't exist that causes it to fail. That's why we iterate properties, and only set it if we find it: bugs in OLEDB drivers.

ConnectionPool ActiveRecord questions

I'm building an advanced RoR application with several instances of database connections running concurrently. However I'm still looking for answers for the following,
Does the connection handler automatically add a connection into the pool once its established? Like the following way,
ActiveRecord::Base.establish_connection(spec)
When would it disconnect the active connection if there's no activity for a while? Or should it be manually disconnected.
Are there any other alternatives to disconnecting the database connection like the following,
ActiveRecord::Base.remove_connection(connection_object)
How can I increase the pool size which could be for different database adapters.
Looking forward to some tips.

How to use connection pooling for Monetdb?

Can some tell me how to use connection pooling with MonetDb? Do I just append these args to the URL "poolminimum=1;poolmaximum=5;"?
No, by itself, the driver does not pool connections. You could use c3p0 (http://www.mchange.com/projects/c3p0/) to create a connection pool for MonetDB JDBC connections. However, please be aware that MonetDB uses optimistic concurrency control and table-level locking, so firing updates at a single table from multiple connections will have an increasing risk of transaction aborts. For reading however, this should be fine.

How do I make connection pooling work in DBX?

Well, I managed get the delegate driver to set up properly, but connection pooling is still giving me a lot of trouble.
From the descriptions given in the documentation, it appears that connection pooling should work like this:
Set up the DBXPool driver delegate on a single, global Connection object
Use this Connection object for all calls into the database
Each DB call will get automagically routed through the delegate driver into a connection that's unique to its thread, owned by the connection pool.
In practice, I'm finding that everything seems to still be handled by the global Connection object, leading to all sorts of strange race conditions and crashes when I try to run DB queries from multiple concurrent threads.
In case it's relevant, I don't have many DBX-specific components defined; I generally run queries by calling the Connection.Execute method.
Any idea what I'm doing wrong? Am I missing a step somewhere, or do I have a bad understanding of how the DBXPool delegate driver works?
Delphi comes with a sample project DelegatesSample. On my machine it is located under \Documents\RAD Studio\8.0\Samples\Delphi\Database\dbExpress\Delegates.
Each connection in the pool has it's own TDBXConnection object. In sample source code each connection is created by call to TDBXConnectionFactory.GetConnectionFactory.GetConnection().
As you know of course, each query in a thread should have it's own connection object.
Felix Colibri has also great article about Delphi DBX4 programming. There he describes amongst other topics also pooling database connections using a pooling delegate driver.
At least these samples helped mine understanding how connection pooling works!

Which strategy about connection management should we use when developing an application?

Which use of connection management is better while developing a windows based application which uses a Database as its data store? What about web-based applications?
when user loads the first form of an application, the global
connection opens and on closing the last form of the application
the connection closes and disposes.
for each form within the application, there is a local connection
(form scope) and when user wants to perform an operation like
insert, update, delete, search, ... the application uses the
connection and by unloading the form the connection also closes and
disposes.
for every operation within a form of an application, there is a
local connection (procedure scope) and when user wants to perform
an operation like insert, update, delete, search, ... the
application uses procedure connection and at the end of every
procedure within the form, the connection also closes and disposes.
Go with #3
You should try to only ever keep connections open for just as long as is required.
Also have a look at
Understanding Connection Pooling
SQL Server Connection Pooling
(ADO.NET)
Connecting to a database server
typically consists of several
time-consuming steps. A physical
channel such as a socket or a named
pipe must be established, the initial
handshake with the server must occur,
the connection string information must
be parsed, the connection must be
authenticated by the server, checks
must be run for enlisting in the
current transaction, and so on.
In practice, most applications use
only one or a few different
configurations for connections. This
means that during application
execution, many identical connections
will be repeatedly opened and closed.
To minimize the cost of opening
connections, ADO.NET uses an
optimization technique called
connection pooling.
Connection pooling reduces the number
of times that new connections must be
opened. The pooler maintains ownership
of the physical connection. It manages
connections by keeping alive a set of
active connections for each given
connection configuration. Whenever a
user calls Open on a connection, the
pooler looks for an available
connection in the pool. If a pooled
connection is available, it returns it
to the caller instead of opening a new
connection. When the application calls
Close on the connection, the pooler
returns it to the pooled set of active
connections instead of closing it.
Once the connection is returned to the
pool, it is ready to be reused on the
next Open call.
This is quite a broad question. But usually, for any database server and application environment, opening and keeping a new connection is an expensive operation. That's why you definitely don't want to open multiple connections from a single client, and should stick to process-scope for connections.
In a desktop application using a database server, strategy for handling it's single connection depends a lot on the DB usage pattern. Say, if the app reads or writes something a lot within 5 minutes, and then just does nothing with the DB for hours, it makes no sense to keep the connection open all the time (assuming there are many other clients). You may introduce some kind of time-out for closing a connection.
The Web server situation depends a lot on the used technology. Say, in PHP every request is a "fresh start" WRT database connection. You open and close a connection for each mouse click. While popular Java application servers have DB connections pool, reusing the same connection instances for many HTTP request handling threads.

Resources