Home > Back-end >  Multi-threaded database write operations, how to realize the transaction function?
Multi-threaded database write operations, how to realize the transaction function?

Time:09-26

Operation of the database in the main process and the thread, the child thread is to create a connection to deal with, in this case, all want to achieve as long as an error rollback, which is the function of the transaction,

Or maybe the question also has nothing to do with multithreading, multiple connections is created in a program, in the case of using multiple connections at the same time operating database, how to realize the transaction function?

CodePudding user response:

Your connection is DataSnap C/S, or directly connected to the local database,

CodePudding user response:

C/s database using ado to connect

CodePudding user response:

Transaction processing needs using the Delphi associated control or database management software, it should be composed of a number of SQL statements, requires proper execution at the same time, as long as there is a mistake, cancel all,
The building plan, in a different thread connection (different) code, constitute a transaction, if not, what haven't seen a instance,
Constitutes a several SQL statements, transaction is a means of implementation, not good segmentation, suggest using ADO to provide transaction processing function
 adoconnection1. Begintrans;//transaction try adoconnection1. Execute (sqlstr1); Adoconnection1. Execute (sqlstr2); . Adoconnection1. Execute (sqlstrN); Adoconnection1.com mitrans;//to commit the transaction showmessage (' successful '); Except adoconnection1. Rollbacktrans;//transaction rollback showmessage (' fail '); end; 

If you need thread, can the above code into a thread,

CodePudding user response:

 adoconnection1. Begintrans;//transaction 
Try
Adoconnection1. Execute (sqlstr1);
Adoconnection1. Execute (sqlstr2);
.
Adoconnection1. Execute (sqlstrN);
Adoconnection1.com mitrans;//to commit the transaction
Showmessage (' successful ');
Except,
Adoconnection1. Rollbacktrans;//transaction rollback
Showmessage (' fail ');
end;

CodePudding user response:

Thank you upstairs, the method I know, I recently in considering the use of RTC in the application server, also on the application server must give each client connection to build a lasting connection, the way to use the connection pool, when performing multiple execsql, this means that the client may each execution not with the same connection, this time, don't know how to handle affairs!

CodePudding user response:

Under normal circumstances, the c/s mode, the client can use to manage affairs as follows a similar statement:

Begintran;
Execsql (... );
Execsql (... );
Committran;

But, if it is connected to the application server (or in the use of multi-threading implementation execsql), because every time can't ensure execsql all use the same connection, which creates a begintran can't use, so I ask this question, if more than one connection can't use the same transaction, only to find a way to avoid, right?

CodePudding user response:

The connection pool?
Zha don't FireDAC, DataSnap technology

CodePudding user response:

Multithreading is independent of the session connection, can also be executed in parallel or query SQL statement, just can't use,

CodePudding user response:

Would you like to have the function of the transaction, each connection can only be run cycle within the main thread,

CodePudding user response:

Out a SAO idea, you can locate a SQL structure, structure of the body with logo, etc., have been circulating within the main thread query flags so as to realize the concurrent of fake, but it can use transactions, have idea the flavor of the thread pool, only do it within the main thread, and other business to the child thread, thread and the main thread through the wm_copydata Windows message passing data, or in the form of event notification,

CodePudding user response:

This ah, you put the connection object under the front cover, and whether an execution success attributes, write their own an exec method, open an account in before they are allowed to execute SQL transaction (open transaction here, and to commit the transaction can separate written method), and is returned to the connection object, and the value of the change (whether execution success attributes) (now suppose to false failure);
Call;
Con1:=the exec (sql1);
Con2:=the exec (sql2);
Con3:=the exec (sql3);
Con4:=the exec (sql4);
B:=con1. The flag and con2. The flag and con3. The flag and con4. Flag;
//the following can be written as general method, are arrays, collections, unified handling,
If b, then
The begin
Con1.com MIT;
Con2.com MIT;
Con3.com MIT;
Con4.com MIT;
End
The else
The begin
Con1. The rollback ();
Con2. The rollback ();
Con3. The rollback ();
Con4. The rollback ();
end;

CodePudding user response:

Does not affect, transaction commit yourself by the database, exposed to the client interface is connected, as for does not affect your own client programming model,

CodePudding user response:

Do you have a problem of structure, the multithreading connection is in order to improve the speed, multiple connections to a user use, set up a thread, the user requests to join the thread task queue, ensure that the same logic process by the same thread processing, of course, also can use a transaction,
  • Related