SOAP schema how distributed transactions?

 First, what is a transaction (Transaction)
  
  all operations incorporated into an indivisible transaction execution unit will provide a mechanism involved in an activity, all operations consisting of affairs can only be submitted only in the case of all normal operations can be performed, as long as either operation fails, it will lead to a rollback of the entire transaction. Simply put, the transaction provides an "either do nothing, or do a full (All or Nothing)" mechanism. Four ACID properties of transactions has four properties, according to its initials, we generally referred to as a transaction:
  
  atomicity (Atomicity): the original meaning of "atomic" The word is indivisible meaning, the meaning of atomic transactions is: all operations of a transaction are bundled as a whole, all operations performed either all or none performed;
  
  consistency (Consistence): atomic transaction ensures that a transaction will not break ring consistent data, if the transaction is successful submission, status data is composed of the results of all operations performed in accordance with prior transactions organized manner, consistent with the data state; intermediate step if any transaction error, the entire transaction is rolled back and return the data to its original state, the data state remains consistency. Therefore, only the transaction data from one consistent state to another consistent state state;
  
  Isolation (Isolation): view from the outside of the transaction, the transaction achieves consistency between the two data coherency state of conversion, but the view from within a transaction, a transaction consisting of the individual operations are performed in a logical sequence, the data located in two coherency state with the "intermediate state." However, this intermediate state is isolated from the interior of the transaction, the external transaction is not visible;
  
  persistent (Durability): Persistence means once successfully submitted, based on persistent data resources (such as a database) will be persisted , changes to the data is permanent.
  
  The transaction was originally derived from a database management system (DBMS), it reflects the operating data stored in the database. In addition to mainstream relational database management systems, such as SQL Server, Oracle and DB2 to provide support for transactions, transaction-based data mode of operation can also be applied to other data storage resources, such as MSMQ. Since the beginning of the Windows Vista file system (NTFS) registry that included transactional resources (Transactional Resource) category.
  
  Second, the explicit control of affairs
  
  , although transactional resources, more and more family members, but it is undeniable that our database is the most frequently used transactional resources. For developers a little with some experience, you should have to write a stored procedure (Stored Procedure) Over-transaction-based SQL, or write code to ADO.NET based transactions, further information of the transaction from the start with here.
  
  1, SQL transaction processing in
  
  both SQL Server-based T-SQL, or is based on Oracle's PL-SQL both transaction provides native support is interesting that T-SQL in the T itself refers to a transaction (Transaction ). With T-SQL, for example, we can achieve by following three SQL statement that initiated the transaction, submission and rollback:
  
  BEGIN TRANSACTION: the beginning of a transaction;
  
  COMMIT TRANSACTION: submit operation between all located TRANSACTION BEGIN TRANSACTION and COMMIT;
  
  ROLLBACK TRANSACTION : rollback all operations positioned between the BEGIN TRANSACTION and COMMIT TRANSACTION.
  
  We give a very typical example based on transactional operations: bank transfer, and this example will always run through this chapter. To this end, we first create one of the most simple to store account data sheet: T_ACCOUNT, nearly the entire table only includes three fields (ID, NAME and BALANCE), which represent the ID, name and bank account balances. Creating the table T-SQL as follows:
  
  . 1: the CREATE TABLE [the dbo] [T_ACCOUNT] (.
  
  2: [ID] VARCHAR (50) a PRIMARY KEY,
  
  . 3: [NAME] NVARCHAR (50) the NOT NULL,
  
  . 4: [the BALANCE] the NOT NULL FLOAT)
  
  . 5: the GO
  
  bank transfer complex is a simple operation, consists of two basic operations: storage and retrieval, i.e., extract the appropriate amount from one account to another account access. Requirements for data integrity is that we have these two operations into the same single transaction. If we complete the transfer process by a stored procedure, specific SQL should be used the following wording:
  
  . 1: P_TRANSFER the CREATE Procedure
  
  2: (
  
  . 3: @fromAccount VARCHAR (50),
  
  . 4: @toAccount VARCHAR (50),
  
  . 5: FLOAT @amount
  
  . 6:)
  
  . 7: the AS
  
  . 8:
  
  . 9: - ensure the existence of the account
  
  10: IF NOT eXISTS (SELECT * FROM [dbo] [T_ACCOUNT] WHERE ID = @fromAccount).
  
  11: BEGIN
  
  12: RAISERROR ('AccountNotExists',16,1)
  
  13: RETURN
  
  14: END
  
  15: IF NOT EXISTS(SELECT * FROM [dbo].[T_ACCOUNT] WHERE ID = @toAccount)
  
  16: BEGIN
  
  17: RAISERROR ('AccountNotExists',16,1)
  
  18: RETURN
  
  19: END
  
  20: --确保余额充足性
  
  21: IF NOT EXISTS(SELECT * FROM [dbo].[T_ACCOUNT] WHERE ID = @fromAccount AND BALANCE >= @amount)
  
  22: BEGIN
  
  23: RAISERROR ('LackofBalance',16,1)
  
  24: RETURN
  
  25: END
  
  26: --转帐
  
  27: BEGIN TRANSACTION
  
  28: UPDATE [dbo].[T_ACCOUNT] SET BALANCE = BALANCE - @amount WHERE ID = @fromAccount
  
  29: IF @@ERROR <> 0
  
  30: BEGIN
  
  31: ROLLBACK TRANSACTION
  
  32: the END
  
  33 is:. The UPDATE [the dbo] [T_ACCOUNT] = the BALANCE the SET + @amount the BALANCE @toAccount the WHERE ID =
  
  34 is: the IF @@ ERROR <> 0
  
  35: the BEGIN
  
  36: TRANSACTION ROLLBACK
  
  37 [: the END
  
  38 is: a COMMIT TRANSACTION
  
  39: GO
  
  2, ADO.NET transaction control
  
  both T-SQL, or PL-SQL, a database management system, or is it an extension to the SQL standard, not just to provide a standards-based SQL DDL (Data Definition Language) and DML (Data Manipulation Language), also provides support for functions, stored procedures and process control. Since SQL Server 2005, even enables integration with CLR (Common Language Runtime), so developers can use any .NET language to write functions or stored procedures. There is no exaggeration to say that you can implement any business logic using SQL.
  
  However, in most cases we do not do this, we will be more or SQL as the most basic data manipulation language in use. For .NET developers, we still used the complex logic and flow control implemented in a program written in object-oriented programming such as C # or through VB.NET. The reason, I think there are two points:
  
  Object-oriented language is more easily implement complex logic: This SQL-based language than a collection of records, object-oriented language that we are closer to the real world, closer to human specific logic simulation by way of the object-oriented way of thinking. In addition, the relative number of features of the language itself surface, we can more easily apply various design patterns and ideas;
  
  will perform logical operations on the database too much is not conducive to the application of extensions: From the perspective of subordinates, data manipulation computational load to a specific server to a typical distributed Web application, for example, Web server (Web applications bearer), application server (bearer services) and the database server can be the final operation of the logic bearer. However, from the viewpoint of scalability (or scalable), the first two on the main computing advantages than on the database. If we intensive operations (such operations require more CPU time and memory) to migrate to the Web server or application server, we can load balancing (Load Balance) to be split into multiple servers above, the server farm can dynamically configured according to the load. However, the database server is not so easy to load balancing support.
  
  Because of this, to control transactions, compared with using SQL implementation, we use the most or the use of object-oriented programming language based on the way. For .NET developers, we can directly use ADO.NET based on multiple operations into a single database connection in the same transaction. Similarly to the above bank transfer transactions, for example, the entire time we will transfer as a service (BankingService) an operation (Transfer). The following code regardless of a specific type of database programming model where the entire ADO.NET bank transfer operation, the final transfer is done by calling a stored procedure:
  
  . 1: public class BankingService: IBankingService
  
  2: {
  
  . 3: // other operations
  
  4: public void Transfer(string fromAccountId, string toAccountId, double amount)
  
  5: {
  
  6: string connectionStringName = "BankingDb";
  
  7: string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
  
  8: string providerName = ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;
  
  9: DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(providerName);
  
  10: using (DbConnection connection = dbProviderFactory.CreateConnection())
  
  11: {
  
  12: connection.ConnectionString = connectionString;
  
  13: DbCommand command = connection.CreateCommand();
  
  14: command.CommandText = "P_TRANSFER";
  
  15: command.CommandType = CommandType.StoredProcedure;
  
  16:
  
  17: DbParameter parameter = dbProviderFactory.CreateParameter();
  
  18: parameter.ParameterName = BuildParameterName("fromAccount");
  
  19: parameter.Value = fromAccountId;
  
  20: command.Parameters.Add(parameter);
  
  21:
  
  22: parameter = dbProviderFactory.CreateParameter();
  
  23: parameter.ParameterName = http://zzdxjyzd.comBuildParameterName("toAccount");
  
  24: parameter.Value = toAccountId;
  
  25: command.Parameters.Add(parameter);
  
  26:
  
  27: parameter = dbProviderFactory.CreateParameter();
  
  28: parameter.ParameterName = BuildParameterName("amount");
  
  29: parameter.Value = amount;
  
  30: command.Parameters.Add(parameter);
  
  31:
  
  32: connection.Open();
  
  33: using (DbTransaction transaction = connection.BeginTransaction())
  
  34: {
  
  35: command.Transaction = transaction;
  
  36: try
  
  37: {
  
  38: command.ExecuteNonQuery();
  
  39: transaction.Commit();
  
  40: }
  
  41: catch
  
  42: {
  
  43: transaction.Rollback();
  
  44: throw;
  
  45: }
  
  46: }
  
  47: }
  
  48: }
  
  49: }
  
  NOTE: To make the code above can be simultaneously used for different types of databases, such as the Oracle and SQL Server, I connect string configuration database providers (DbProvider covers) by extracting a name, whereby the corresponding DbProviderFactory created object. All ADO.NET objects, including DbConnection, DbCommand, DbParameter and DbTransaction are created by DbProviderFactory, and it is not tied to a specific database type. In addition, the named parameters of different types of database stored procedures based on different, parameters such as SQL Server will add "@" prefix, and I will resolve the name of the parameters achieved in a single method (BuildParameterName).
  
  3, the control transaction is explicitly defined in a single access to resources
  
  by controlling the transaction in SQL, can be incorporated into a single transaction based on a period of operation of the SQL statement; if the control data based on ADO.NET , it is incorporated into the same transaction operations limited to a database connection. In other words, both explicit control transactions described above are only limited to the control of a single local resources.
  
  We will introduce the concept of transaction services, if we will be a single service operation as a transaction, if the above manner explicit transaction control, then the entire service operation can only involve a single transaction resource. Service to access resources so that the relationship shown in Figure 1.
  
  SOAP architecture, how to achieve a distributed transaction?
  
  Figure 1 for a single local transaction control resources
  
  above such a service-based access to local resources of a single transaction, known as the Local Affairs (Local Transaction), based on a distributed SOA application environment, and we need to be able to more resources, more services distributed transaction unified collaboration (distributed transaction). Next, we introduce some typical applications of distributed transaction scenario.
  
  Third, the distributed transaction (Distributed Transaction) scenario
  
  for a distributed transaction (Distributed Transaction) is concerned, the participants Affairs distributed network environment different nodes. In other words, we can incorporate multiple transactional resources into a single transaction, and such transaction resources can be distributed to different machines. These distributed resources carrying machine may be out of the same network, it may be in a different network. Even said, on a transaction resource is essentially a simple Internet resources via HTTP access.
  
  Standing SOA point of view of a distributed transaction, a service operation means that the service is treated as a single transaction. The service operation may visit more than one transaction resource (such as access to two different database servers), it may call another service. Here are three typical scenarios distributed transaction, starting with the easiest to start.
  
  1, will have access to multiple resources into the same transaction
  
  on a distributed transaction simplest scenario, namely a service operation and does not call another service, but the service operations involve access to multiple transactional resources. When a different server to access the database service operations, such as two SQL Server, and SQL Server or a an Oracle Server; a service operation when accessing the same database, but different data connection based on the corresponding database access; when a service processing operation to access the database resources, the transaction also need access to resources from other parts of the database, we need to use distributed transactions to collaborate on all of the participants in the transaction. Figure 2 reflects the distributed application scenarios.
  
  SOAP schema how distributed transactions?
  
  2 single service access to multiple transactional resources
  
  2, calls to the various services included in the same transaction
  
  For distributed application scenarios described above, although a service operation will access multiple transactional resources, but after all, the entire transaction or control in a single internal services. If a service operation needs to call another service, this is it spans multiple transactions served. In this case, starting a service business in another call to the service, need to be some mechanism for transfer to another service, access to the resources of the service is invoked automatically join. Figure 3 reflects such a distributed transaction across multiple services.
  
  SOAP architecture, how to achieve a distributed transaction? 3 transactions across multiple services Figure
  
  3, will have more access to resources and services into a single transaction
  
  if the above two scenarios (a service can be invoked multiple transactional resources, can invoke other services) together, which extends, the entire transaction participants, will form a tree topology as shown in FIG. 4. In a call to the service-based distributed transaction, the initiator and commit transactions were all the same, it may be the entire call the client, can also be a service client that first call.
  
  SOAP architecture, how to achieve a distributed transaction? 4 SOA-based distributed transaction topology
  
  implementation mechanism of local transactions, distributed transactions based on a single resource access than the much more complex. Windows-based platform provides DTC distributed transaction infrastructure in the next article I will how to work when the architecture model for distributed transaction details.

Guess you like

Origin www.cnblogs.com/dakunqq/p/11360585.html