oracle: oledb resource(session) pooling
by eric ma ema@ompus.jnj.com
database connectivity is a prerequisite for todays dynamically generated web pages. however, connection to databases is one of the most expensive operations one does from within an asp page, because of the overhead involved in database user authentication and allocation of database resources to handle user interaction with the database, among other things. all these can add significant latency to your web site. based on the recent discussions in this list, it is apparent that everyone is keenly aware of this issue and would like to minimize the negative impact on performance by having to connect to databases. for a programmer with a client/server programming background, it is natural for the person to design a solution based on using a database connection that persists through an entire user session. however, for a web-based application, this is a big no-no (see the article at /advice/dbsessionapp.asp for reasons why you should never do this).
the best practice for database connection (we limit our discussion to oracle here) from an asp application is to open the connection at late as you can, and close it as soon as you can, which means you open and close database connection on every asp page. the encouraged practice is to use just one set of oracle username/password for your entire application, not one set for each user as you may be used to do with client/server applications. access control is no longer performed by the database, but by your application. you can save the ado connection string in an application variable in the global.asa file. some other alternatives include saving it in the windows registry, or use an include file that has the ado connection information, and include that file in the pages where database connection is required.
opening a new database connection for each asp page may not be as bad as you think, because from ado 2.0 up you can utilize the “resource (session) pooling” feature offered by microsofts ole db provider for oracle. resource pooling is similar to odbc connection pooling, where a connection is returned to a pool instead of being destroyed immediately after it is closed and set to nothing in your code. see the following article for more details:
http://msdn.microsoft.com/library/techart/pooling2.htm.
the purpose of this faq is to demonstrate how to properly set the environment on your iis/nt server to take advantage of ole db resource pooling. unlike with odbc, where you can enable pooling and set the timeout parameter from the 32-bit odbc utility in the control panel, with ole db you have to directly work on the registry itself. again, this is not as scary as it sounds. to do so, you first determine the version of ole db provider you are using by running the script found at: http://www.learnasp.com/learn/connectioninfo.asp, then dependent on the version you have, you may do the following steps:
?if you are using the ole db 2.0 provider you can do resource pooling but you cannot modify the default timeout configuration (60 seconds). to set up resource pooling, add the following subkey in the registry:
hkey_classes_root\clsid\{e8cc4cbe-fdff-11d0-b865-00a0c9081c1d}\oledb_services. make sure you use the dword type, and enter a hexadecimal value of 0xffffffff.
however, i suggest that you dont use the 2.0 version of the provider any more because of potential memory leaks. see the following kb article for more:
http://support.microsoft.com/support/kb/articles/q194/3/87.asp
?if you use the ole db 2.1 or 2.5 provider, in addition to doing the above step, you can configure the pooling timeout parameter by adding the following subkey:
hkey_classes_root\clsid\{e8cc4cbe-fdff-11d0-b865-00a0c9081c1d}\sptimeout. again this entry is of a dword type and you enter a decimal value in seconds. it is up to you to decide the number of second you want to set, depending on how busy your site is and how frequent the pages need to access oracle data.
a kb article describing the above step can be found at:
http://support.microsoft.com/support/kb/articles/q237/9/77.asp
after you make the above changes, reboot your nt server to make the registry changes take effect. this is step is necessary.
congratulations, now you are using the wonderful feature of ole db resource pooling! i hope from today on you will never ever think about caching connection objects in session variables anymore! treat it as evil!
some side notes and observations:
?with oracle, in order to use resource pooling, you need to add the registry subkey. having only “ole db services = -1” in your connection string is not enough.
?i came across the following kb article in msdn saying ole db resource pooling is automatically enabled when you install mdac 2.1 and consequently the 2.1 ole db provider. this is true for ms sql server, but not true for oracle. you have to physically enter the oledb_services subkey in order to enable resource pooling.
see http://support.microsoft.com/support/kb/articles/q240/1/66.asp for the case of ms sql server.
?the resource pooling white paper above mentions that you need to leave at least one connection open per user session to utilize resource pooling. this seems to suggest that one should open a connection in session_onstart and dont close it and set it to nothing until session_onend. personally, i dont think it is necessary and you should not do it. ado takes care of creating the necessary initial connection and keep it in the pool, unlike in c++ where you have to do it yourself. one word of advise: explicit close every single one of oracle connections and set it to nothing after you are done!
if you really want to see resource pooling in action, fire up the oracle instance manager (part of oracle enterprise manager), and watch the number of user sessions go up and down as connections are made and terminated, when asp page requests are made. you will see the connections are reused before they are timed out, if you have resource pooling properly configured.