connection| Object | Optimization in this database connection. We used the session object, first, to create a Conn connection object, and then connect to the database Data.mdb, get the connection handle, save it in the sessions ("conn") this conversation variable, before opening the recordset, from the "Conn") to remove the handle from the session object. We can make different ASP pages share a connection object, reduce the cost of server memory, and we do not need to worry about, when a customer because of the wrong operation caused the server can not extract the occupied session connection object, Because we know that each client's session object has a certain lifetime, after this deadline, the server will automatically analyze it out.
In ASP's database programming, the connection object is an object that we can not leave, in any operation to the database, such as updating records, inserting, deleting, retrieving and so on, must use the connection object to complete. Figuratively speaking, the connection object is the pipeline that the program communicates to the database, all operations on the database must pass through it, therefore, no matter how we connect to the database, we always have to execute the following code.
Set conn = Server.CreateObject ("ADODB. Connection ")
Create an instance variable of a Connection object, and then build the Recorderset object or Command object on it to manipulate the database. Since the connection object is so important, how to optimize and manage the connection object is extremely important to the database program, it is related to the performance of the program.
Every time a customer performs a database operation, a connection object is needed, and each connection object consumes a portion of the server's resources, while the number of concurrent connections to the database cannot be unlimited, so. When filtering to provide High-performance database Web programs, we need to test how to reduce the cost of the server. Generally speaking. Creating a Connection object in each ASP page creates a connection to the database in the server. The connection objects of different pages are not shared. So. Can we share a connection connection channel with the different pages that the same user accesses?
You may wish to think of our ASP six building object Session object, he can for a specific user to save private data, if we put the connection object in the Session object, can we make different ASP pages use the same connection channel? Take a look at this procedure below.
$#@60;%
Set conn = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("/")&"/news/data/data.mdb"
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
session("conn")=conn
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data"
cn=session("conn")
rs.open sql,cn,3,2
%$#@62;
d width="100%">
在这个数据库连接中。我们使用了session对象,首先,建立一个conn的连接对象,然后连接到数据库data.mdb中,取得连接句柄后,把它保存在session("conn")这个session会话变量中,在打开记录集前,从session("conn")中取出句柄,借助于session对象。我们可以使不同的ASP页面共用一个连接对象,减少了对服务器内存的开销,而我们也不需要担心,当一个客户因为错误操作导致服务器不能析放该被占用的session连接对象,因为我们知道每个客户的session对象是有一定的生存期限的,过了这个期限,服务器就会自动把它析放掉。
而ODBC3.0所提供的连接池connection pooling。也能有效地提高数据库的运行性能。我们知道,在众多的asp页面中生成的connection对象,总是处在不断建立连接,解除连接,析放连接中。
如何能高效的管理和使用好这些被废弃的连接,也可以在一定程序上大大提高运行效率。而ODBC3.0则提供了这样的功能,它能聪明的把需要新建立的连接引导至即将废弃的connection对象上,反复地使用它。这样,就能节省了不好服务器的开销。