Iii. Database Connection
The previous chapter describes the core part of the system, the meta-data driver. This chapter describes the database connection.
The design goal of this system is to support multiple types of databases. Therefore, we should consider this during design.
When it comes to multi-database support, it is also called "database-independent" in some places, that is, it can be supported no matter what database the customer chooses. In fact, my system can only support MSSQL and Oracle at present, because these two databases belong to enterprise-level databases, and the number of users in China should be the largest, because access is too weak, so I didn't do much processing on it. If I want to add support for other databases, I just need to do some processing in this module.
To support multiple databases, there are currently two popular practices:
U uses standard ODBC or oledb
U writes different SQL statements for different databases
In the first way, the database expansion capability is quite strong, because it basically uses standard SQL syntax, and tries to avoid the unique features of each database; the second method, regardless of this set, allows you to use the characteristics of each database. The first method is flexible while also compromising the performance of the database to varying degrees. The second method is enhanced in this respect, but it is troublesome to expand the database. My system adopts the second method.
Although different components need to be written for each database, when calling other modules, you should not consider the type of the database. at least in most cases, you do not need to know, this is what I want to talk about. It is implemented using the interface technology.
Interface idata
This interface defines several methods:
Int executenonquery (string commandtext );
Int executenonquery (string [] sqls );
Dataset executereader (string SQL );
String executescalar (string commandtext );
Then create two classes to implement this interface, namely oracledata and sqldata, and re-implement all the above methods. In these two classes, oracleclient and sqlclient are used, this shows that the features of each database can be used for development.
No matter how it is "unrelated to the Database", the system still needs to know what database is used in the end. I use a dedicated configuration file to store all the information, instead of using the web. in this way, make sure that my class can be used in various scenarios, instead of being limited to the bsstructure. The file is named config. xml and its content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Config>
<Connection>
<Connection_name> test </connection_name>
<Type> Oracle </type>
<Server> </Server>
<Database> </database>
<User> test </user>
<Password> wejhxrh71ds = </password>
<Minpoolsize> 10 </minpoolsize>
</Connection>
</Config>
This file contains the following parts:
|
connection_name |
connection name, which is only an identifier and is not used by the System |
|
type |
used to distinguish oracle from MSSQL |
|
server |
specifies the source of the database. Oracle uses the TNS name. If it is null, it indicates the local machine. MSSQL uses the connection name, such as (local) and so on |
|
database |
only valid for MSSQL, indicating the database to be operated |
|
User |
connection user name |
|
password |
the encrypted format exists to prevent files from being easily cracked, the system will reverse the password to obtain the real password for use |
|
minpoolsize |
used to configure the connection pool, standard Asp.net functions |
ProgramThere is a specialized external class called dataaccessor, which is responsible for external calls to the database. This class will first look for config. XML file. Check the database type. The connection is directed to oracledata or sqldata based on different types. Dataaccessor also encapsulates several same functions as idata, which is convenient for external calls. In addition, the search for config. XML is completely implemented internally without external intervention. For system performance, after the configuration file is read for the first time, the content of the configuration file will be written into the memory, so that no IO operation will be generated during the next read. The negative effect is that after the configuration file is changed, IIS must be restarted to take effect.
The encrypted storage of the password mentioned above adopts des standard encryption instead of MD5 one-way encryption.AlgorithmTo ensure that the system can reverse find the original password, and DES is encrypted in binary form, so I made another base64 conversion to ensure that it can be stored in XML files.
Each database has its own special syntax. For example, to judge whether a function is null, The isnull function is used in MSSQL, the nvl function is used in Oracle. In this way, when an external reference is made, the nvl function will first determine the Database Type of the data source and then write different SQL statements. In this case, this practice is definitely unacceptable, and it is too troublesome. So I made a special class called dbconverter, which has several static functions, specifically responsible for compatibility between different databases. If the above function is empty, there is a special static function as follows:
Public static string nvl (string servertype)
{
Switch (servertype. toupper ())
{
Case "oracle ":
{
Return "nvl ";
}
Case "MSSQL ":
{
Return "isnull ";
}
Default:
{
Return "nvl ";
}
}
}
In this way, during external calls, you only need to pass the dataaccessor database type as a parameter to obtain the correct expression without writing different SQL statements for processing, this has played a significant role in multi-database support. Similarly, you can also write the top syntax as a function for processing. When this method cannot cover all databases, it is necessary to write a separate SQL statement to implement specific functions.
It can be seen from the above that if new database support is required, the main modification is to implement a new I data interface and extend the dataaccessor class.
Let's talk about performance issues.
Int executenonquery (string commandtext) has a commit action in the end. This is of course to ensure data integrity, however, when there are a large number of DML operations, this function will be called repeatedly, resulting in a sharp decline in performance, while int executenonquery (string [] sqls) this function exists to solve this problem. Within this function, a transaction is first processed, and then the specified SQL statement in sqls is run in sequence, finally, a unified commit action will improve the efficiency a lot.
Although the above method can improve a lot of efficiency, after all, each SQL submission will have a client-to-server submission action, even if IIS and Oracle are on the same server, the burden of this action is also very heavy, so the best way is to do more stored procedures or packages (exclusive to Oracle) to minimize the interaction between the database and the outside world, in this way, we can achieve real efficiency. For this skill, I willArticle.
There is another performance problem here. dataset executereader (string SQL) is returned in the form of a result set. dataset is characterized by taking all the returned result sets out in the memory. In this case, it is its advantage to maintain data integrity as much as possible, but if the table is large, it will take a long time to load the memory, the memory usage is much larger than that of the original data table. Therefore, when using this function, minimize the number of returned records, such as limiting the number of returned rows. A large number of returned results do not make much sense for front-end display. In my front-end display control, there is a special filter that allows you to easily search records. This improves the performance in essence.
So far, the database connection is basically done. The following describes how to implement multiple languages.