Database advanced application

xiaoxiao2021-03-06  69

First, connect pool

1. Features:

1, advantages: performance

2, Disadvantages: There may be multiple connections that are not used, resource waste

2, ADO.NET connection pool

1 Contains each .NET data provider in ADO.NET enables a connection pool.

2 Each connection pool is associated with a separate connection string and its transaction context. Each time a new connection is opened, the data provider will try to match the specified connection string with the string of the connection pool. If fails, create a new connection and join the connection pool.

3 After the connection pool is created, some connection objects are created and join them to the connection pool until the number of rated minimum connection objects is reached. Create a new connection as needed until the maximum number of connections is reached.

4.Net default is to use the connection pool. If you want to disable, you can use the following way:

I, when using the SqlConnection object, join in the connection string: POOLING = FALSE

II, when using the OLEDBConnection object, join: OLE DB SERVICES = -4

3, tips and skills

1 Open the connection should be late, close the connection should be early

2 Make sure to close all user-defined transactions before turning off the database connection

3 At least to ensure that there is a connection in the connection pool available

Second, caching

1, characteristics

1, advantages: Improve performance, stability, availability

2, ASP.NET cache

I, in ASP.NET, a Cache object specifically used to cache data is provided, and its application range is the application domain. The survival period is closely related to the application. Whenever the application starts, you recreate the Cache object whenever the application starts. It is the main difference between Application objects to provide performance, such as dependent and expiration strategies specifically for cache management.

II, CACHE object definitions in system.Web.caching namespace, you can use the cache property of the HTTPContext class or the cache property of the Page object to get the cache reference, the Cache object can store the object of the .NET framework in addition to the stored key value.

2, dependent and expiration strategy

1 File Policy: Forced to remove cache data when an (some) file changes on the hard disk

Cachedependency cdependency = new cachedependency (server.mappath ("authors.xml"));

Cache.insert ("CachedItem", Item, CDependency;

2 Key value reliance: Removes a data item change in the specified cache.

// Create a cache entry

Cache ["key1"] = "value1";

// make Key2 Dependent on Key1

String [] DependencyKey = new string [1];

DependencyKey [0] = "key1";

Cachedependency Dependency = New Cachedependency (Null, DependencyKey);

Cache.insert ("Key2", "Value2", Dependency);

3 Time-based expiration strategy: absolute and relative

// Absolute evbit

Cache.Insert ("CachedItem", item, null, datetime.now, addseconds (5), cache.noslidingexpiration;

// Sliding expiration

Cache.Insert ("", item, null, cache.noabsoluteExpiration, timeespan.fromseconds (5));

4 Database dependencies (recommended not to use): Data related to data changes, cache fail 3, use cache: Data must check the validity of data when using cache

String data = (string) cache ["myitem"];

IF (data == null)

{

Data = getData ();

Cache.Insert ("MyItem", DATA);

}

4, cache callback: automatically call when the cache is invalid

CacheItemRemoveCallback Onremove = New CacheItemRemovedcallack (this.removeCallback);

Cache.insert ("CachedItem", item, null, cache.noabsoluteexpiration, cache.noslidingexpiration, cacheitempriority.default, onremove;

// Implement the function to handle the expected of the cache.

Public Void RemovedCallback (String Key, Obejct Value, CacheItemRemonvedreason R)

{

// Test WHETHER THE ITEM IS EXPIRED and Reinsert It Into The Cache.

IF (r == cacheItemRemovedreason.expired)

{

// reinsert it in the cache again.

CacheItemRemovedCallback onremove == NULL;

OnRemove = New CacheItemRemoveCallback (this.RemovedCallback);

Cache.insert (Key, Value, Null, Cache.NoabsoluteExpiration, Cache.NoslidingExpiration, CacheItemPriority.Default, onremove);

}

}

5, Cache Priority: When the memory of the application server is insufficient, the data in the cache is automatically clear, called "Clear Scavenging"

Cache.Insert ("DSN", Connectionstring, Null, D, T, CacheItemPriority.High, Onremove;

6, use ASP.NET cache in non-Web projects

HttPruntime.cache objects can exist in each application domain other than ASPNET_WP.exe.

HTTPRUNTIME HTTPRT = New httpruntime ();

Cache cache = httpruntime.cache;

Third, affairs

1. Write directly to SQL: Use Begin Trans, Commit Trans, Rollback Trans, implementation during storage.

Begin Trans

Declare @orderdetailSerror Int, @ ProductError Int

Delete from "Order Details" Where ProductId = 42

SELECT @OrderDetailSerror = @@ error

Delete from Products Where ProductId = 42

SELECT @ProductError = @@ error

IF @orderdetailserror = 0 and @ProductUcTerror = 0

Commit transSelse

Rollback TRANS

Advantages: All transaction logic is included in a separate call

Have the best performance running a transaction

Independent application

Limit: Transaction context exists only in the database call

Database code is related to the database system

2. Use ADO.NET to implement: You can manage your transaction in the middle layer. SqlConnection and OLEDBConnection objects have a begintransaction method that can return SQLTransaction or OLEDBTRANSACTION objects.

Cn.open ();

Sqltransaction trans = cn.begintransaction ();

SQLCommand cmd = new sqlcommand ();

Try

{

Cmd.comMandtext = "delete [Order Details] where produter = 23";

cmd.executenonquery ();

CMD.Commandtext = "delete products where produter = 23";

cmd.executenonquery ();

TRANS.COMMIT ();

}

Catch (Exception E)

TRANS. ROLLBACK ();

Finally

Cn.close ();

Advantages: simplicity; and database transactions are almost fast; independent of database

Disadvantages: The transaction cannot across multiple database connections;

Transaction is executed on the database connection layer, so you need to maintain a database connection during the transaction;

Fourth, distributed affairs

1. Features:

To participate in the COM transaction, the .NET class must be inherited from the System.EnterpriseServices.serviceDComponent class.

By using the System.EnterpriseServices.ContextUtil class, you can get the COM object context information, which provides the SETCOMPLETE and SETABORT methods to display submission and rollback transactions separately.

Advantages: Only DTC or COM transactions can only be used in a resource running system that requires transactions across MSMQ and other recognizable transactions. DTC coordinates all resource manager involved in distributed transactions, also manages operations related to transaction.

Disadvantages: Decreased performance due to the presence of DTC and COM mutual operability overhead.

2. Transaction type:

1, automatic transaction: use the System.EnterpriseServices.AutoComplete property

[Transaction (TransactionOption.Required)]

Public class class1: servicedcomponent

{

[AutoComplete]

Public void example ()

{}

}

2, manual affairs

Public String TransferMoneyFrombtoa (Double M)

{

Try

{

Contextutil.enableCommit ();

THIS.TRANSFEROUTFROMB (M);

THIS.TRANSFERINTOA (M);

Contextutil.setComplete ();

}

Catch (Exception Err)

{

Contextutil.Setabort ();

}

}

3, mode choice

For the following cases, manual transaction is required: transaction processing for a single database;

For the following cases, automatic transaction processing should be used:

1. When you need to extend a single transaction to multiple remote databases;

2, when you need a single transaction to have multiple resource managers (such as databases and Windows2000 Message Queuing Resource Manager); Note: Avoid mixing transaction processing models, preferably only one of them.

转载请注明原文地址:https://www.9cbs.com/read-87831.html

New Post(0)