Static keyword Analysis in sqlhelper in pet shop 4.0

Source: Internet
Author: User

 

The methods and fields in sqlhelper will not be mentioned. Let's talk about the static keyword usage in it.

 

Static keywords use methods and fields in the class to directly access them using the class name. I think everyone knows this basic syntax. next we will discuss why we should use the static keyword in sqlhelper. What are the advantages of doing so?

 

1. first, if the common field is static, such as public static readonly string. so it is often used in the future. it is especially called in pet shop. static statement can help reduce system overhead. another point: methods or fields unrelated to instances can be declared as static to indicate the characteristics of their classes.

 

2. Check whether the sqlhelper static method supports multithreading.

 

The sqlhelper method is static. Why? It is used to facilitate the call and they are irrelevant to the instance.

 

The above only explains the advantages of using static methods and fields: You don't need to instantiate them. You can call them directly with class names and reduce system overhead. however, another new question is introduced: Does the static method support multi-thread synchronization?

 

Let's take a look at whether static methods will cause non-synchronization when the pet shop project is inserted with multiple threads. The answer is that non-synchronization will not occur when multiple threads call static methods at the same time.

 

Because the static method can only access static class fields and static class methods, if you want to use non-static methods, classes, fields, and so on in static classes, it is necessary to instantiate a class in the static method (provided that the class can be instance) and call a non-static method or non-static field.

For example, a static method in sqlhelper

Public static intExecutenonquery(String connectionstring, commandtype parameter type, string cmdtext, Params sqlparameter [] commandparameters)

 

In it, a sqlcommand cmd = new sqlcommand (); Class variable

Therefore, when multiple threads call this static method executenonquery, multiple corresponding sqlcommand cmd = new sqlcommand () will be created in the memory, so that each thread does not interfere with each other. therefore, using sqlhelper in multiple threads to insert data to the database will not interfere with each other.

 

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.