Change uncertainty to confirmation ~ Is the program really dispose?

Source: Internet
Author: User

Back to directory

First, let's take a look at what dispose is. When we use unmanaged resources, we need to implement the dispose method by ourselves. Its meaning is to release the memory space used.

For example, stream is an unmanaged type, which implements an idisposable interface to implement the dispose method.

Like transactionscope ,. net transaction. It is also an unmanaged transaction. That is to say, after using the transaction, we need to perform the dispose () operation on our own. The following question comes: Where can this dispose be written?

Pay attention to this sectionCode:

 

 Using ( Transactionscope Trans = New  Transactionscope ()){ Try { This . Update (order ); New Webaccountrecordsrepository (). Insert ( New  Webaccountrecords {}); New  Webaccountbalancesrepository (). Update ( New  Webaccountbalances {});} Catch ( Exception E ){ // VM. additem (E. Message );  Throw ;}Finally {
 
Trans. Dispose ();}}

This is a very standard writing, complete an order processing process, it will process orders, website payment details and the total balance of the website written in a transaction, of course, this is no problem, note that the location of dispose is written in finally {}, which is correct. When try {} is complete, the finally part will be executed, but note the catch {} segment, it captures all exceptions and throws them. Okay. If an exception occurs in this try {} segment, will the finally {} segment be executed? That is, will dispose be executed?

After my tests, it has been executed, but because you have used throw, the webpage has a direct yellow screen, so it is best to process the catch segment, you can write the exception to the log, but please note that do not write code that may cause exceptions in the finally {} block. Otherwise, your transaction resources will never be released!

For example:

 Using ( Transactionscope Trans = New  Transactionscope ()){ Try { This . Update (order ); New Webaccountrecordsrepository (). Insert ( New  Webaccountrecords {}); New  Webaccountbalancesrepository (). Update ( New  Webaccountbalances {});} Catch ( Exception E ){ // VM. additem (E. Message );  Throw ;}Finally {Insert (list. First (). Id ); // If the list set is empty, an exception will occur, and the following code cannot be executed. Trans. Dispose ();}}

 

Of course, this is generally caused by programming habits. You can pay attention to it later. When releasing resources in finally, you should consider exceptions for some necessary judgment.

If you have to write it in finally, if your object cannot be sure whether an exception will occur, try, catch, and check the Code:

Finally

{

Try{

Insert (list. First (). Id );// If the list set is empty, an exception will occur, and the following code cannot be executed.

}

Catch(ExceptionE)

{

Throw;

}

Finally

{

Trans. Dispose ();

}

}

 

In this way, trans. Dispose () will also be executed, that is, for the same try, catch, and finally, finally will always be executed.

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.